1

I want to get data from a MySQL-database using JS and PHP (without jquery or ajax) But I keep getting the "undefined Return Value Error".

It's my first time programming in JS and PHP.

url: "../PHP/Kassensystem.php?productname="+productname

JS:

function getProduct(url) {

let product;

var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onload = function () {
    if (request.status == 200) {
        product = request.responseText;
    }
};
request.send(null);

return product;
}

PHP:

<?php
$productname = $_GET['productname'];
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'allgolddb';

$conn = mysqli_connect($servername, $username, $password, $dbname);
$prodctInfos = array();

$sql = "SELECT * FROM products WHERE name = '$productname'";

$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
  $prodctInfos[] = $row;
}

$json_array = json_encode($prodctInfos);
return $json_array;
?>
Strohhut
  • 490
  • 1
  • 6
  • 22
  • 3
    your code is wide open to **SQL injection attacks** - use parameterised statements to secure your data. – Franz Gleichmann May 15 '19 at 16:11
  • 3
    Side note: _"without jquery or ajax"_ - You're still using Ajax. That's what you're using `new XMLHttpRequest()` for. – M. Eriksson May 15 '19 at 16:11
  • 1
    Never combine [javascript] and [mysql] in your question. It's two completely different realms that **never** meet. So it's either [php] and [mysql] or [javascript] and [php] – Your Common Sense May 15 '19 at 16:12
  • The issue is most likely that you have `return $json_array;` while you should output the data using: `echo $json_array;`. Using `return` in this context won't actually do anything. – M. Eriksson May 15 '19 at 16:14
  • 1
    @MagnusEriksson oh that was an editing error, ok I will try with echo ... did not work :/ – Strohhut May 15 '19 at 16:15

1 Answers1

0

In your open method you've set 3rd param to true so your request.send will be executed asynchronously. And as with every asynchronous JavaScript, by the time your code execution reaches return product product is still undefined and hence the error.

You can set the 3rd param to false to make it synchronous and your code will work (atleast working in firefox so far):

request.open("GET", url, false);

However the sync option is deprecated. From MDN

Synchronous requests on the main thread can be easily disruptive to the user experience and should be avoided; in fact, many browsers have deprecated synchronous XHR support on the main thread entirely. Synchronous requests are permitted in Workers.

As for working asynchronously, you'll have to consume within the callback to onload method:

request.onload = function () {
  if (request.status == 200) {
    product = request.responseText;
    console.log(product) // consume here
  }
};

You could also use Promise, optionally with async/await. More here to work with asynchronous code.

1565986223
  • 6,420
  • 2
  • 20
  • 33