A javascript routine receives a form, with variables as:
cmd = "update", lst[0] = "1", lst[1] = "3"
It calls a php routine(through an Ajax call):
function p_admin_users_update($form)
{
*** get the from data as "variable name", "variable value". ***
for all variables in form:
$txt .= variable, value
(say $txt = "variable name = variable value");
return ($txt);
}
?>
Question: How can I get the variable name and its value in the PhP routine?
Tried many ways... no avail. get_object_vars does not work: type says object, function does not.
2007-08-26
10:36:22
·
2 answers
·
asked by
just "JR"
7
in
Computers & Internet
➔ Programming & Design
Ajax is a requirement (updating a division in a page without reloading the full content, Web 2.0).
The form is created in a division.
A "button" calls a js routine and the form is sent to the JS routine.
In the JS routine, all the data is received, together with variables, and displayed.
Use the following routine to show the form contents:
function showform(theForm)
{
var txt = "Form name: " + theForm.name + "\r\n";
txt += "theForm.elements.length = " + theForm.elements.length + "\r\n";
for (var i=0; i < theForm.elements.length; i++)
txt += theForm.elements[i] . name + " = " + theForm.elements[i].value + "\r\n";
alert(txt);
}
This shows the whole of the form as a pop-up (alert).
2007-08-26
10:56:13 ·
update #1
JS? I have no choice: JS runs on the client, PhP runs on the server. The PhP can only be called by a JS routine (XmlHTTPrequest type)...
And I have to pass variables from one to the other all the time, in both directions: this is the new "fun" with Web 2.0 that every new client wants. This requires the code to be nearly trebbled in size: one in JS, one in PhP and one in handling the exchanges requests.
2007-08-26
20:35:21 ·
update #2
Sorry guys, and thanks, but I have found the solution.
A js function gets a form and passes the data to a PhP code through AJAX:
function js_cats_update(theForm)
{
var txt = "";
for (var i=0; i < theForm.elements.length; i++)
{
txt += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
}
sajax_target_id = 'admindata';
x_p_admin_cats_update(txt);
sajax_target_id = ' ';
}
And the PhP routine that receives it:
function p_admin_cats_update($txt)
{
parse_str($txt);
// from here, ALL variables, NAME and VALUES are available!
If, say, you have a variable named" lst[5]" in your form (includes "indexed arrays!"), you can call
if ($lst[5] == something)
do something!
Although no apparent variable names are there
3 days headache!
}
2007-08-27
05:31:56 ·
update #3