Instead of a function displaying a line of text because the result set is empty, I get an empty select field

72 Views Asked by At

The section of page I'm working on:

 <div class="containerwrapper">
    <h3 id="crvenah3">Izmena zadatka</h3>
    <div class="containerwrappersub" id="containerwrappersub">
    <form method="post" action="" enctype="multipart/form-data" class="container_izmena" id="formaizmena">
        <div id = "levo">
        </div>

        <div id = "desno">
        
        <div id="skriveno" name="skriveno" class="skriveno">
        </div>
        </div>
        
        <input type="text" value="" id="provera" name="provera" hidden="hidden">
        <input type="text" value="" id="obrisi" name="obrisi" hidden="hidden">
    </form>
    </div>
</div>

function popuniZadatke() {
    let korisnik = "<?php echo $korisnik; ?>";
    $.ajax({
        url: '../funkcije/popuniOpcije.php',
        data: {
            korisnik: korisnik
        },
        success: function (data) {
            $("#levo").html(data);
        }
    });
}
popuniZadatke();

The popuniOpcije.php file:

<?php
require "../konekcija/konekcija.php";
require "../modeli/zadatak.php";

$korisnik = trim($_GET["korisnik"]);

$zadaci = Zadatak::vratiZadatkeZaOpcije($korisnik, $konekcija);
?>

<?php if (!empty($zadaci)) { ?>     
    <h4>Zadatak</h4>    
    <select id="zadatak" name="zadatak" onchange="prikazi(this.value); popuniDetalje(); sakrij();"required></select>

    <div class="prikaziizmeni" id="prikaziizmeni">      
        <h4 id="prikaziizmenitekst">Zadatak je uspešno izmenjen<h4>     
    </div> 
<?php } else { ?>   
    <h4>Nema zadataka za izmenu</h4> 
<?php } ?>

When I open the page, this works fine - I get the line Nema zadataka za izmenu displayed when there aren't any tasks to be displayed, or the select box with all the tasks if there are.

When I click on a task, I get the details displayed, which I can then edit and submit, or simply delete the entire task:

<div id="buttonwrapperizmena">
            <div class="buttoncontainerizmena">
                <button type="submit" class="buttonizmena" name="button" onclick="izmeniZadatak();"><span class="puntekst">Izmeni zadatak</span><span class="krataktekst">Izmeni</span></button>
            </div>
            <div class="buttoncontainerizmena">
                <button type="submit" class="button2izmena" name="button2" onclick="obrisiSliku(); obrisiZadatak();"><span class="puntekst">Obriši zadatak</span><span class="krataktekst">Obriši</span></button>
            </div>
        </div>

function obrisiZadatak() {
        $("#formaizmena").ajaxForm(function() {
            $("#prikaziizmeni").css("display", "flex");
            $("#prikaziizmenitekst").text("Zadatak je uspešno obrisan");
            $("#prikaziobrisi").css("display", "flex");
            $("#skriveno").css("display", "none");
            popuniZadatke();
        })
    }

How the task is deleted:

if (isset($_POST["button2"])) {
    $zadatak = trim($_POST["zadatak"]);

    $podaci = Zadatak::vratiPodatke($zadatak, $konekcija);
    if ($podaci->slika != "") {
    unlink("../slike/".$podaci->slika);
    }
    Zadatak::obrisi($zadatak, $konekcija);

public static function obrisi($zadatakID, mysqli $konekcija)
    {
        $query = "DELETE FROM zadatak WHERE zadatakID = " . $zadatakID;
        $odgovor =  $konekcija->query($query);
        return $odgovor;
    }

How the tasks are returned:

public static function vratiZadatkeZaOpcije($korisnik, mysqli $konekcija) {    
    $upit = "SELECT * FROM zadatak WHERE korisnik = " . $korisnik;
    $resultset = $konekcija->query($upit);
    $zadaci = [];

    while($zadatak = $resultset->fetch_object()) {
        $zadaci[] = $zadatak;
    }

    return $zadaci;
}

The function obrisiZadatak() is creating issues for me. It will refresh the select box properly (display all the tasks left without the one I deleted) and display the proper message that the task has been deleted up until I have to delete the last one. When I delete the last one, as you can see, I'm still calling the same function, but instead of the aforementioned line I want, I just get an empty select box. Only when I refresh the page does that section change and I get the line.

How do I fix this?

0

There are 0 best solutions below