I am using ajax to send data to a php page that queries mysql and returns the results. I have in my page, a variable that holds the dept value. However, if I var_dump($_POST) I see that the var is an array.
If I manually enter a value in the query, then data is returned. However, just not using my $dept var.
How do I decode this array to make the variable available for my query. Thanks
ajax code
$.ajax({
url: 'deptdata.php',
type: "POST",
contentType: "application/json; charset=utf-8",
data: {dept: depts},
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (data) {
alert('error');
}
});
post in firebug tab
dept=DEMOBILL
deptdata.php
<?php
$dept = $_POST['dept']; <--- array?
//open connection to mysql db
$connection = mysqli_connect("localhost","root","","sample") or die("Error " . mysqli_error($connection));
//fetch table rows from mysql db
$sql = "select custref from boxes where department = '".$dept."' and status = 1";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));
//create an array
$emparray = array();
while($row = mysqli_fetch_assoc($result))
{
$emparray[] = $row;
}
echo json_encode($emparray);
//close the db connection
mysqli_close($connection);
?>