I have been trying all of the examples given on SO for passing an array generated by jQuery to PHP to store in a MySql DB but all I get on the PHP side is "Array".
I am writing code for a Bingo game. My jquery generates and array called "drawArray" which should be stored in my database. Example:
["N37", "G72", "O47", "I43", "N26", "G65", "I62", "N14", "B69", "G67", "I63", "G09", "G01", "G52", "N57", "B42", "N21", "I54", "N21", "N13", "N10", "N01", "N71", "I21", "O01", "G72", "O27", "G32", "O31", "B19", "O34", "I69", "O49", "O29", "G52", "O26", "I34", "I66", "I68", "I60"]
I have tried ajax, jquery.post and either way I either get noting on the PHP side or I only get "Array"
Here is my jQuery:
$("#draw").click(function() {
drawNumbers();
console.log(drawArray);
$.ajax({
url: "includes/dealerpicks.php",
type: 'POST',
data: {'drawArray[]' : drawArray},
dataType: "json",
async: false
});
});
And on my PHP side:
//POST Data
$DealerNums = $_POST['drawArray'];
echo $DealerNums . "<br><br>";
$sql="INSERT INTO drawings (dealerPicks)
VALUES
('$DealerNums')";
if (!mysqli_query($conn, $sql))
{
die('Error: ' . mysqli_error($conn));
}
echo "1 record added";
mysqli_close($conn)
?>
So of course I need to store all of those dealer numbers into the DB for later use when drawing out the Bingo Card. I figured I would store it and all of the drawings into an array in the DB to be referenced later in my PHP.
Why am I only getting "Array" for a value?
$DealerNums
is an array. You are using it as a string. Something like this should solve your problem, I am also adding a couple of safety checks:
$DealerNums = $_POST['DealerNums'];
if (!is_array($DealerNums))
{
// handle error
}
//escape_string to avoid SQL injection
$DealerStr = $conn->escape_string(join(',', $DealerNums));
$sql="INSERT INTO drawings (dealerPicks)
VALUES
('$DealerStr')";
Thank you for recommending JSO Stringify.
My updated code is as follows:
$("#draw").click(function() {
drawNumbers();
console.log(drawArray);
var jsonDrawArray = JSON.stringify(drawArray);
$.ajax({
url: "includes/dealerpicks.php",
type: 'POST',
data: {data:jsonDrawArray},
dataType: "json",
async: false,
cache: false,
});
and my PHP:
//POST Data
$DealerNums = $_POST['data'];
echo $DealerNums . "<br><br>";
$sql="INSERT INTO drawings (dealerPicks)
VALUES
('$DealerNums')";
if (!mysqli_query($conn, $sql))
{
die('Error: ' . mysqli_error($conn));
}
echo "1 record added";
mysqli_close($conn)
?>
Then for those of you following, I am using the Array later on in my PHP page like this:
<?php
$query = "SELECT dealerPicks FROM drawings WHERE drawID = '14'";
$result = mysqli_query($conn, $query);
if($result === FALSE) {
die(mysqli_error());
}
while ($row = mysqli_fetch_array($result)) {
$dealer = json_decode($row['dealerPicks']);
foreach ($dealer as $key => $value) {
$letter = substr($value, -3, 1);
$number = substr($value, -2, 2);
echo '<div class="dealernum"><span class="alphabet">'.$letter.'</span>'.$number.'</div>';
}
}
?>
The data you're sending is not in correct format. Try it like this:
function drawNumbers() {
return ["N37", "G72", "O47", "I43", "N26", "G65", "I62", "N14", "B69", "G67", "I63", "G09", "G01", "G52", "N57", "B42", "N21", "I54", "N21", "N13", "N10", "N01", "N71", "I21", "O01", "G72", "O27", "G32", "O31", "B19", "O34", "I69", "O49", "O29", "G52", "O26", "I34", "I66", "I68", "I60"];
}
$("#draw").click(function () {
var drawArray = drawNumbers();
$.ajax({
url: "submit.php",
type: 'POST',
data: {drawArray: JSON.stringify(drawArray)},
dataType: "json"
});
});