hope that this time I won't mess up :) for people not to missunderstand me that I want to Set Session variable using javascript in PHP, please read this line:
echo "<td><input type='button' id=$type value='Buy!' onclick=location.href='buyone.php'></td>";
I want to have the button ID increase from 1 to 5, but seems like I only get all button have id=5 after the 5-times loops.
while($row=mysqli_fetch_array($result)){
$type = $row['petID'];
echo "<tr>";
echo "<td>" .$type. "</td>";
echo "<td>" .$row['price']. "</td>";
echo "<td><input type='button' id=$type value='Buy!' onclick=location.href='buyone.php'></td>";
echo "<tr>";
}
The $type value is from 1 to 5, because I have 5 rows with ID from 1 to 5 in database. I want to create a table where each row have a button, that's if I click the button, I'll insert the a newline in database. That's a different story, but this's what I want now: when I click a button, I can save the id of that row in $_SESSION['type'] to use in others PHP file. So I use this:
<script>
$("#1").click(function(){
<?php $_SESSION['type'] = 1;?>
});
$("#2").click(function(){
<?php $_SESSION['type'] = 2;?>
});
$("#3").click(function(){
<?php $_SESSION['type'] = 3;?>
});
$("#4").click(function(){
<?php $_SESSION['type'] = 4;?>
});
$("#5").click(function(){
<?php $_SESSION['type'] = 5;?>
});
</script>
But whatever I clicked, the $_SESSION['type']'s value is still 5. I think it's because the button's id is always $type last value after the loop, so all the id is 5, that's why I can only get 5 when I run it.
What should I do to fix it? Also, I want to have a loop in the script too, but I'm not sure how to use it.
So thanks in advance, and I'm here waiting for all your comments :)