English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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

2 answers

you have to create a hidden field in the form called "variable name" and with value "value"
I can't see the actual form in your code, but in html it would be
Then when the form calls the php file, that value will be sent along with all the other data in the form

As a matter of interest, is there some particular reason for using Ajax/javascript for updating the form, it must be pretty slow compared to php?

2007-08-26 10:47:03 · answer #1 · answered by Anonymous · 0 0

Javascript is pretty useless at handling data, if at all possible change to using php to read the form post information.

2007-08-26 11:25:09 · answer #2 · answered by Anonymous · 0 0

fedest.com, questions and answers