I’m getting the following error in display.php **Warning: Invalid argument supplied for foreach(). I am trying to loop through each of these numbers in my display function and generate an image for each from Main. When I echo display.php it outputs nums[0]=$displayid
nums[1]=$displayid and i want to pass the $displayid into my query statement.
generate_numbers()
var rndNums = new Array(16,17,18,19,20,21,22,23,24,25,26,27,28);
var generate_numbers = function()
{
for(var i=0; i < 13; i++)
{
var idx = Math.floor(Math.random()*rndNums.length);
var rndNum = rndNums[idx];
rndNums[idx] = rndNums[rndNums.length - 1];
rndNums.pop();
document.getElementsByName("nums[]").item(i).value=rndNum;
}
}
Display.php
$nums = isset($_POST['nums']); // array
foreach($nums as $key => $displayid)
{
echo 'nums['.$key.']='.$displayid."<br>\n";
}
$stmt->bind_param("i", $displayid);
$stmt->execute();
$stmt->bind_result($image);
$stmt->fetch();
header("Content-Type: image/jpeg");
echo $image;
?>
Main.html
<form id="numberForm" action="display.php" method="POST">
<input type="hidden" name="nums[]">
<input type="hidden" name="nums[]">
</form>
<button onClick="generate_numbers();document.getElementById('numberForm').submit()">submit</button>
returns a boolean (whether the variable is set or not) and not an array.
Maybe you are trying to do this:
Also, note, you should not echo text before the image data, you are going to corrupt your response. And the
$display_idin the query would be the last one from the loop