Topic: updating db from select value

i need to update a field in a table according to the selected value in a SELECT tag. I declared the select like this:

function updatePriv($id, $index) 
{
    $resp=new xajaxResponse();
   global $db;
   $db->query("UPDATE users SET priv=$index WHERE id=$id");
   return $resp;
}


$id = $r["id"]; //get user id
echo '<select name="priv" onchange="<?php $updatePriv($id, document.priv.selectedIndex); ?>">';
if ($r["priv"]==0) echo '<option value="0" selected>Admin</option>';
    else echo '<option value="0">Admin</option>';
if ($r["priv"]==1) echo '<option value="1" selected>Users</option>';
    else  echo '<option value="1">Users</option>';
if ($r["priv"]==2)  echo '<option value="2" selected>Premium users</option>';
    else echo '<option value="2">Premium users</option>';
echo "</select>\n";

but the value is not updated in the table. I guess that Im doing something wrong here, first of all I have no idea if it is valid to return an unmodified response and neither know how to set the select to its new value. Also, I think that perhaps Im sending $id in the wrong way.

Re: updating db from select value

The select tag should be like this:

'<select name="priv" onchange="xajax_updatePriv('.$id.', document.priv.selectedIndex)">';

If your select construction is inside of an xajax function you cannot use echo...

You can return an unmodified response object but it might be useful to have it alert the outcome of the query...

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

Re: updating db from select value

Doesnt works. Seems that I have some mistake somewhere:

function updatePriv($id, $index) 
{
    $resp=new xajaxResponse();
   global $db;
   echo "Values $id, $index";
   $db->query("UPDATE users SET priv=$index WHERE id=$id");
   return $resp;
}


$xajax->register(XAJAX_FUNCTION, 'updatePriv');
 $xajax->processRequest();

echo '<select name="priv" onchange="xajax_updatePriv('.$id.', document.priv.selectedIndex)">';
if ($r["priv"]==0) echo '<option value="0" selected>Admin</option>';
    else echo '<option value="0">Admin</option>';
if ($r["priv"]==1) echo '<option value="1" selected>Users</option>';
    else  echo '<option value="1">Users</option>';
if ($r["priv"]==2)  echo '<option value="2" selected>Premium users</option>';
    else echo '<option value="2">Premium users</option>';
echo "</select>\n";

Re: updating db from select value

Have you tried turning on the xajax debug tool?

$xajax->configure('debug', true);

Have you looked at ther server logs?

Have you tried just putting the html code after the closing php tag? All that quoted stuff and echos can lead to errors.

When is the select being generated?

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

Re: updating db from select value

Debug is enabled and xajax is working in that page correctly, I implemented there a couple of features using checkboxes and a button. Also, there are no errors in the log.

Re: updating db from select value

Hmm,

Would you please post the entire page (php & HTML).

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

Re: updating db from select value

i made a small test page, and doesnt works:

<?php

require ('xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->configure('debug','true');
$xajax->configure('javascript URI', 'xajax/');
 echo "<!DOCTYPE HTML>\n";

 echo '<meta charset="UTF-8">';
  
 echo "<title>Tes</title>\n";
 $xajax->printJavascript();
 
 function updatePriv($id, $index) 
{
    $resp=new xajaxResponse();

   $resp->assign('rank', 'innerHTML', "Id $id, Index $index");

   return $resp;
}

 $xajax->register(XAJAX_FUNCTION, 'updatePriv');
 $xajax->processRequest();
?>
<p id="rank">Test</p>
<select name="priv" onchange="xajax_updatePriv('1', document.priv.selectedIndex)">
<option value="0" selected>Admin</option>
<option value="1">Users</option>    
<option value="2">Premium users</option>
</select>

Re: updating db from select value

Here is the code reorganized to work:

<?php
require ('xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->configure('debug',true);
$xajax->configure('javascript URI', 'xajax/');
$xajax->register(XAJAX_FUNCTION, 'updatePriv');
$xajax->processRequest();
 echo "<!DOCTYPE HTML>\n";
 echo '<meta charset="UTF-8">';
 echo "<title>Tes</title>\n";
 $xajax->printJavascript();
 
 function updatePriv($id, $index) 
{
    $resp=new xajaxResponse();

   $resp->assign('rank', 'innerHTML', "Id $id, Index $index");

   return $resp;
}
?>
<p id="rank">Test</p>
<select name="priv" id="priv" onchange="xajax_updatePriv('1', document.getElementById('priv').selectedIndex)">
<option value="0" selected>Admin</option>
<option value="1">Users</option>    
<option value="2">Premium users</option>
</select>

I missed the problems earlier. sorry.
1. document.priv should be document.getElementById('priv'). I added an id to the select tag.

2. The 3 echo statements and the printJavascript() call were being executed each time the update function was called because everything before the processRequest() call that is outside of a function  is executed on each call.

3. In the debug configuration call you quoted true so debug would not work.


Your code would never validate.

Here it is as a proper php/html page:

<?php
require ('xajax/xajax_core/xajax.inc.php');
$xajax = new xajax();
$xajax->configure('debug',true);
$xajax->configure('javascript URI', 'xajax/');

function updatePriv($id, $index) 
{
    $resp=new xajaxResponse();

   $resp->assign('rank', 'innerHTML', "Id $id, Index $index");

   return $resp;
}
$xajax->register(XAJAX_FUNCTION, 'updatePriv');
$xajax->processRequest();
?>
<!doctype html>
<html>
<meta charset="UTF-8">
<title>Tes</title>
<?php $xajax->printJavascript() ?>
</head>
<body>
<p id="rank">Test</p>
<select name="priv" id="priv" onchange="xajax_updatePriv('1', document.getElementById('priv').selectedIndex)">
<option value="0" selected>Admin</option>
<option value="1">Users</option>    
<option value="2">Premium users</option>
</select>
</body>
</html>
If you ever stop learning you may as well dig a hole, crawl in and pull the top over yourself.

Re: updating db from select value

Yes! It works perfectly now.