PHP updating field value through AJAX

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.

Answers


You haven't set the data of you ajax request correctly. Refer to http://api.jquery.com/jQuery.ajax/

data: Data to be sent to the server...Object must be Key/Value pairs

NOTE: it can also be a String, but you have to format it properly

try

 data: {uid : id},

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.


The answer about the data field being incorrect is right, but something this simple shouldn't be a "POST" it should just be a GET, which doesn't need the 'data' field at all. Just change the URL field to: "vote.php?id=" + id

Like this:

$.ajax 
({ 
    type: "GET", 
    url: "vote.php?id=" + id, 
    cache: false, 
    success: function(data) 
    {  
       alert("ok"); 
    } 
});  

Need Your Help

XML Serialization in C#

c# xml serialization xml-serialization

Where Can I find good tutorial about XMl serialization to the object?