0

I am a complete beginner and I hope that you can help me.

In my php file I make two different database queries and I need their results for calculations in a js function, which in turn should be displayed in the browser. If I try to display the results from both queries, only "undefined" is returned. However, they are displayed to me in the console, where I also have the numbers output via consol.log (data). I am trying this with the help of ajax. If I remove the part from the second DB query in my function, it works. Unfortunately not all together. What am I doing wrong? Is what I plan to do even possible or do I have to take a detour? If yes, which one?

Here is my previous code: Javascript:

function clickPHPtoJS(){
$.ajax({
  url: "index.php",
  type: "POST",
    success: function(data) {
        console.log(data);
        clickPHPtoJSResponse(data);
     },
    error: function (data) {
        console.log("Daten nicht erhalten");
    }
});
}

function clickPHPtoJSResponse(data) {
// Antwort des Server ggf. verarbeiten
var response = JSON.parse(data);
var einer = response.einer;
var zwoelfer = response.zwoelfer;
var anzahl = response.nr;
document.getElementById("lab1er").innerHTML = einer + " " + zwoelfer + " " + anzahl;
}

PHP:

<?php

require 'inc/db.php';

$erg = $db->query("SELECT id, sender FROM packaging_log WHERE sender='test' AND packaging='1er'")
or die($db->error);

$gesteigereiner = $db->query("SELECT * FROM geliefert WHERE 1")
or die($db->error);

while ($zeile = $gesteigereiner->fetch_object()) {
    $einer = $zeile->test1er;
    $dreier = $zeile->test3er;
    $sechser = $zeile->test6er;
    $zwoelfer = $zeile->test12er;
}


$geliefert = array ( "nr"        => $erg->num_rows,
                     "einer"     => $einer,
                     "zwoelfer"  => $zwoelfer);

print_r (json_encode($geliefert));
?>

Unfortunately, that's not how it works. But if I completely remove the result from the first DB query in the JS.

Many thanks in advance. I am grateful for any information, clarification and tips.

  • The variables inside the while loop seem weird to me. If every row has those 4 columns, the variables will only contain values from the last row. – RatajS Oct 27 '21 at 17:21
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Oct 27 '21 at 17:26
  • @RatajS: Thank you for your comment. The table 'geliefert' has just 4 columns and 1 row. With the while loop I try to get the content of each column. Would there be a better way for it? – Marley2009 Oct 27 '21 at 17:31
  • Is the `response` variable string? Isn’t it already object? I think jQuery can parse JSON automatically in AJAX under some conditions. – RatajS Oct 28 '21 at 09:49
  • @RatajS: If I remove the response variable and JSON.prase I get this console message: {"nr":1612,"einer":"7000","zwoelfer":"5000"} test.js:20 Uncaught TypeError: Cannot read properties of undefined (reading 'nr') at clickPHPtoJSResponse (test.js:20) at Object.success (test.js:7) at c (jquery.min.js:2) at Object.fireWith [as resolveWith] (jquery.min.js:2) at l (jquery.min.js:2) at XMLHttpRequest. (jquery.min.js:2) Do you have an idea what I should also change? – Marley2009 Oct 29 '21 at 07:35
  • @RatajS: Without prase I tried it like this: `function clickPHPtoJSResponse(data) { // Antwort des Server ggf. verarbeiten //var response = JSON.parse(data); var anzahl = data[0].nr; var einer = data[1].einer; var zwoelfer = data[2].zwoelfer; document.getElementById("lab1er").innerHTML = einer + " " + zwoelfer + " " + anzahl;` – Marley2009 Oct 29 '21 at 07:49
  • @RatajS: And did you get the same console message? Because my code was the same as the code you posted. – Marley2009 Oct 31 '21 at 09:16

1 Answers1

0

The problem was the following: "nr" => $erg->num_rows To get what I wanted I had to save $erg->num_rows as a variable and to put the variable into the array. So first step: $nr= $erg->num_rows; Second step: "nr" => $nr