On my webpage I have a number of buttons that relate to table columns in a MySQL database (the id of the buttons and the table column names are exactly the same).
What I would like to do is capture the ids in an array and via AJAX, send back an array of values associated with those table columns with the same names. The AJAX call is no problem, however I'm struggling to understand how to convert the data for use with PHP in my server code.
My code is as follows:
jQuery:
var username = USERNAME;
var array = [1, 2, 3, 4, 5];
$.ajax ({
cache: false,
url: PHP_FILE,
dataType: "json",
type: "post",
data: {username: username, array: array},
success: function(data) {
console.log(data);
}
});
PHP:
<?php
$dbc = DATABASE CONNECTION;
$username = mysqli_real_escape_string($dbc, trim($_POST['username']));
$auth = mysqli_real_escape_string($dbc, trim($_POST['array']));
$array = json_decode($auth, true);
$query = "SELECT auths.".$array." FROM details INNER JOIN auths USING (code) WHERE un = '".$username."'";
$data = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($data);
echo(json_encode($row));
?>
Presumably json_decode is not the right route, however I'm struggling to understand what the alternative would be.
var_dumping$_POSTto see what's in it