I'm reading the values of my MySql-Databasetable with php like this:
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$servername = "localhost";
$username = "wsl_home";
$password = "wsl";
$dbname = "wsl_home";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['functionname']) == 'getDrinks') {
$sql = "SELECT ID, Drinkname FROM drinks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["ID"]. " - Name: " . $row["Drinkname"]. "<br>";
return $row;
}
} else {
echo "0 results";
}
}
?>
This works and i get the values as expected. Now I want to return them to my javascript using ajax to call the php function:
jQuery.ajax({
type: "POST",
url: 'drinks.php',
dataType: 'json',
data: {functionname: 'getDrinks', arguments: [1, 2]},
success: function (obj, textstatus) {
if( !('error' in obj) ) {
console.log("hi " + obj);
yourVariable = obj.result;
}
else {
console.log(obj.error);
}
}
});
But I get no output in the success function. Does somebody know how to accomplish this task?