Topic: starting again: handling checkbox clicks

After a long time, Im starting again with Xajax. I forgot all I had learned and also I lost the work I did, so I have to start from scratch.
I want to implement a checkbox in a list of users that will be checked/unchecked according to a value from a database, if clicked, the code should switch the value to its opposite in the corresponding user and of course, update the checkbox. How can I do this?

Re: starting again: handling checkbox clicks

I messed with the concept and came up with this:

<?php
include 'xajax/xajax_core/xajax.inc.php';
$xajax = new xajax();
$xajax->configure('javascript URI','xajax/');
/*
  This function receives the id of the checkbox
  (The state name) and a string "true" or "false."
  This example just sends the values back to the page.
  Param: id string name of the state
  Param: checked string "true" or "false depending on the checkbox state
*/
function processCheckbox($id,$checked)
{
    $resp=new xajaxResponse();
    //Here's where you would handle the database update.
    $resp->assign("values","innerHTML" ,"Values: Id: $id  Checked: $checked");
    
    return $resp;
}
/**
  This creates a table of checkboxes and state names from 
  an array of state names. Just for practice.
  
  Each row contains a checkbox with the state name as id and the state name .
  the click calls a js function that extracts and processes what we want
  to send to the server function and sends them.
*/  
function createChecklist()
{
    $resp=new xajaxResponse();
    $states = array(
            "ALABAMA",
            "ALASKA",
            "ARIZONA",
            "ARKANSAS",
            "CALIFORNIA",
            "COLORADO",
            "CONNECTICUT",
            "DELAWARE",
            "DC");
    
    $s = '<table>';
    foreach($states as $state)
    {
      $statename = trim(substr($state,0,14)); //get the state name
$s .=<<<EOD
<tr><td><input type="checkbox" id="$statename" onclick="handleCheckbox(this)"/><td>$statename</tr>
EOD;
    }
    $s .= '</table>';
    $resp->assign("states","innerHTML",$s);
    return $resp;
}
$xajax->register(XAJAX_FUNCTION, 'processCheckbox');
$xajax->register(XAJAX_FUNCTION, 'createChecklist');
$xajax->processRequest();
?>
<!DOCTYPE html>
<html>
<title>Xajax Checkbox Testing</title>
<head>
<script>
/*
  This function receives the checkbox object,
  extracts it id and checked state and sends them
  to the registered function.
*/
function handleCheckbox(c)
{
  id = c.id; //The id is the state name
  ck = (c.checked) ? 'true' : 'false'; //This determines if the checkbox is checked
  //alert(id+'  '+ck);
  xajax_processCheckbox(id,ck); //Call the server function
}
</script>
  
<?php $xajax->printJavascript() ?>
</head>
<body onload="xajax_createChecklist()">
<div id="states"></div>
<br/><br/>
<div id="values"></div>

</body>
</html>
</head>
<body onload="xajax_createChecklist()">
<div id="states"></div>
<br/><br/>
<div id="values"></div>

</body>
</html>

This should do what you have in mind.

If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.