0

This is how I upload an image for a user, it works fine:

if (isset($_POST["button"])) {
$naziv = trim($_POST["naziv"]);
$kategorija = trim($_POST["kategorija"]);
$opis = trim($_POST["opis"]);

$ime = time()."_".$_FILES["slika"]["name"];
$target = "../slike/".$ime;

move_uploaded_file($_FILES["slika"]["tmp_name"], $target);

Zadatak::dodaj($naziv, $korisnik, $kategorija, $opis, $ime, $konekcija));
}

This is how I display the image, it works fine as well:

<div class="slikacontainer">
    <div class="slikawrap">
        <img src="../slike/<?=$podaci->slika?>"/>
    </div>
</div>

But, when I try to use this code to console.log the image, it logs only the beginning before the value I sent it ("../slike/"):

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

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

$podaci = Zadatak::vratiPodatke($zadatak, $konekcija);

?>

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>

<script>
     function popuniKategorije() {
        $.ajax({
            url: '../funkcije/popuniKategorije.php',
            success: function (data) {
               $("#kategorija").html(data);
               postaviKategoriju();
               console.log("../slike/<?=$podaci->slika?>");
            }
        });
    }
popuniKategorije();

    function obrisi() {
        let zadatak = <?=$zadatak?>;
        let slika = <?=$podaci->slika?>;
        $.ajax({
                url: '../funkcije/obrisiSliku.php',
                data: {
                    zadatak: zadatak,
                    slika: slika
                },
                success: function (data) {
                    console.log($zadatak);
                }
            });
    }
</script>

The vratiPodatke function looks like this:

public static function vratiPodatke($zadatakID, mysqli $konekcija) {    
        $upit = "SELECT * FROM zadatak z JOIN kategorija k ON k.kategorijaID = z.kategorija WHERE zadatakID = " . $zadatakID;
        $resultset = $konekcija->query($upit);
        $zadatak = $resultset->fetch_object();
        return $zadatak;
    }

What is the problem here? I need that value so I could use it in a different function and I think this is where the problem lies because I'm essentially sending the incorrect one.

  • Remember any PHP you put into your main script runs on the server, before your page loads into the browser. So whatever's in `$podaci->slika` was defined _before_ your AJAX code ran and just hard-coded into the JS script (check the View Source feature in your browser, if you want to see). If you want to know where the file was stored, get the `popuniKategorije.php` to return the URL as part of its response (e.g. if that script returns JSON, it could be contained in one of the properties) – ADyson Apr 20 '23 at 15:45
  • Relevant background reading: [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) and [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ADyson Apr 20 '23 at 15:47
  • But why am I able to console.log other fields such as `$podaci->opis` which is a String and `$podaci->kategorija` which is an integer, but not that one (also a String)? – Mihailo Mitrović Apr 20 '23 at 15:52
  • Presumably that field was already populated in your $podaci object (I don't know where it gets its data from?), before the image upload took place. – ADyson Apr 20 '23 at 15:53
  • Sorry I just noticed you have two AJAX requests there. Is it actually `obrisiSliku.php` which handles the file upload? You didn't make clear which PHP script your first snippet is taken from. – ADyson Apr 20 '23 at 15:54
  • I updated the post if it helps, it should contain all relevant information, but let me know if I should add anything else – Mihailo Mitrović Apr 20 '23 at 15:55
  • Or maybe it's neither of those requests which is relevant? Neither AJAX request looks like it would handle a file upload actually, now that I look at them more closely. The whole process and structure is not obvious, it seems we have information missing. When does the file upload happen relative to the other code you've shown? What are those AJAX requests supposed to do? – ADyson Apr 20 '23 at 15:55
  • This is code on the edit page where a user is allowed to delete the photo from the task he's made. The file is uploaded on a different page (the first batch of code I put in the post) using a different function. And `obrisiSliku.php` handles how the file is deleted, which is the function that's not working because I cannot pass the value I'm trying to log. – Mihailo Mitrović Apr 20 '23 at 16:00
  • Hm ok. In `let slika = =$podaci->slika?>;` I'd expect you need quote marks around the value, if it's a string. e.g. `let slika = '=$podaci->slika?>';`. That might help. Don't you have a console error about it? Still doesn't explain why `$podaci->slika` is empty though. have you checked there is actually even a value in the database in that field, before you run this code? – ADyson Apr 20 '23 at 17:43
  • @ADyson The quotation marks were missing and for some reason even though I had them Chrome wasn't showing them until a couple of hours later when I had to log in again. Sending the value is fine now, thank you. – Mihailo Mitrović Apr 20 '23 at 22:08
  • Maybe your file was cached by the browser or something. Glad you got it working anyway. – ADyson Apr 20 '23 at 22:17

0 Answers0