I have a table of items, and when the user selects an item i want to set that id to a php variable so when it hits the button “Ok” i associate that item with the user. He can only choose one item at a time. I have tried some php / ajax tutorials and it didn’t seem to work.
Here are the codes:
<table id="tableItens" style="border:1px solid #000000;" cellpadding="0" cellspacing="0">
<tbody>
<?php foreach($produtoList as $produto) {
if ($columnCounter == 0) { ?>
<tr style="background-color:#FFFFFF;">
<?php
}
?>
<td id="<?php echo $produto['id'] ?>" width=160px; height="110" onclick="<?php echo 'selectRow(' . $produto['id'] . ');' ?>" style="text-align: center; border:1px solid #000000;border-bottom:0;"><?php echo'<img align="center" width="150" height="100" src="/resources/images/' . $produto['nome_imagem'] . '"></img>' ?></td>
<?php $columnCounter++; ?>
<?php if ($columnCounter == 3) {
$columnCounter = 0; ?>
</tr>
<?php } ?>
<?php } ?>
</tbody>
<script type="text/javascript">
function selectRow(id) {
var table = document.getElementById("tableItens");
document.getElementById(id).style.backgroundColor = 'red';
$.ajax
({
type: "POST",
url: "vote.php",
data: id,
cache: false,
success: function(data)
{
alert("ok");
}
});
}
</script>
Do i have to include a ajax file or something? Maybe a jquery reference?
I’m using a var_dump($_POST) at the beginning of the page and doesn’t show anything. I’m sending the value to the same page im in.
You haven’t set the data of you ajax request correctly.
Refer to http://api.jquery.com/jQuery.ajax/
NOTE: it can also be a String, but you have to format it properly
try
and of course, you have to include a reference to jQuery, you are using jQuery
To test stuff like this out I suggest using the JS console that usually comes with most modern browsers. You can change POST to GET and see what kind of URLs are generated.