views:

521

answers:

2

Hi guys,

I'm new to jquery and ajax and I'm having trouble doing this...

I have a php webpage that builds a table with values from a mysql table. The rows are "messages" and the last field of the row is a boolean field "read" that states if the message has been read or not.

Building the table, I transformed the boolean value to a checkbox to be "checked" or "unchecked", either the value is 1 or 0.

What I want to do is to connect this checkbox to an ajax event, using jquery, that if the checkbox status changes (if it's clicked), then it sends two values (status of the checkbox when the table was first loaded, and the message id) to another php file.

On this other php file I would update the status "read" of that particular message on my mysql table.

Can you give me any lights on how to do this? The php part I can handle but I don't know what jquery/ajax functions to build and how to call them on the html part of the checkbox...

Thanks

+1  A: 

The following code will add an event handler to the checkbox with id #checkboxId. The handler will either call test.php?checked=0 or checked=1, depending on the current checkbox status (unchecked/checked)

edit: here you have a full blown example for the client side. from php, you have to echo the message ids as the checkboxes value. this is then returned to server as param msgId. Now supporting multiple checkboxes with class filtering. You can change the class and parameter names, of course.

<html>
<head>
<title>Checkbox test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

</head>
<body>

<input type="checkbox" class="messageActivated" value="msg0">
<input type="checkbox" class="messageActivated" value="msg1">
<input type="checkbox" class="messageActivated" value="msg2">

</body>

<script type="text/javascript">
$(".messageActivated").bind('click', function() { 
    if($(this).attr('checked'))
    {
        $.get("store.php", { checked: 1, msgId: $(this).attr('value') } );
    }
    else
    {
        $.get("store.php", { checked: 0, msgId: $(this).attr('value') } );
    }
});
</script>
</html>
henchman
Thanks henchman.Just a few doubts... With that code, how can I pass the message_id to the test.php file?And other doubt... how do I call that function on this html part<input type="checkbox" name="checkboxId" value="checkboxId">?
Metraton
the function is called automatically, because by usind "$(".messageActivated").bind('click', ", you add an event handler to each checkbox.Problem with passing the message id has been solved in the current version... somebody doesn't like me ;((
henchman
A: 

Hi again Guys, thanks for your support.

Henchman, I think you're approach it's not going to work in this particular example because I don't have different names for the checkboxes... because de table is builted from a "while" cicle of a query made to the database...

Piemesons, I tried you're code, with a few changes, here's what I did (it's still not working...)...

So, on the checkbox I have:

Then, on the Jquery side I have:

<script type="text/javascript">
    function my_function()
    {
        var messagem_id = "<?php echo $messagem_id; ?>";
        var lida = "<?php echo $lida; ?>";
        $.post("trata_mensagem_lida", {
            mensagem: mensagem_id,
            estado: lida,
        },
                function(response){
                    alert(response);
            });
    }

</script>

The "messagem_id" and "lida" are set before the table is formed, after the query is made to the database. And i assume that in the "arguments" part of the function i can put more than one argument. Then I really don't understand what the "function(response)" does... Can you enlight me on this?

On the PHP side, I only have this for now:

$mensagem = $_POST['mensagem']; $estado = $_POST['estado']; echo $mensagem; echo $estado;

Because I just want to check if the values get there... cause if they do, thatn it's easy to update...

How can I debug and get to show on the main page some alert message with the "echo's" text?

Metraton
anyone can figure out what i'm doing wrong here?
Metraton
edit ur question .. dont post in the answer section...
piemesons