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;
?>