I am trying to write a script that will update my database(s) without the user having to click any buttons. I also want the update to occur whenever a field is modified in the form.
So here is an example form
<form>
<input type="text" name="fieldone" value="currentvalfromdb" onChange="updateDB()" />
<select name="selectone" onChange="updateDB()">
<option value="currentvalfromdb">currentvalfromdb</option>
<option value="1">1</option>
...
</select>
<textarea name="textarea1" onChange="updateDB()">currentvalfromdb</textarea>
</form>
Now whenever a user changes a value on any one of the fields, the field is updated in the DB and a confirmation is printed below the field saying "Data Updated" or something to that effect.
Now using ajax I've been able to update a single field. But the script I'm using assigns the updated value to the same variable every time (in this case it's 'q').
the javascript looks like this (I found this on the web all over the place)
var xmlHttp;
function updateDB(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="scripts/ajaxProcess.php";
url=url+"?q="+str;
//url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
I see why the variable 'q' ends up being what is passed to the db update script. What I need is for this variable to be dynamic, that is to say, that the variable name changes per field. I 'could' just assign a var for each variable in the script, but problem a: there are 6 different tables involved in this update and problem b: there are almost 50 fields to work with and they may change from time to time; I don't what to have to do extra work if I don't have to.
Is there a way to write the script so that it will dynamically figure out the field name (I will use the database field name as the form field name, to keep it simple) and assign THAT field name to the $_GET's name? A bit more simple, but I would also include the table name in the field name (so something like table|fieldname as the field name) so when the variable is sent to be processed, it'll know which table to query, which field, and what the new value is. I suppose I'll also have to send the old value (or the id for the row of that table being updated) so it may need to be something like "table|row|fieldname"
If I can get assistance with the first part (dynamic variable name), I can figure out the second half pretty easily.
Thanks!!