sending an array from php to javascript | | |
hello,
i have made an application in php so that people can make, on the fly, a
form.
when they submit the form there is a javascript formvalidation.
because we do not know how many fields there are in the form (it is made on
the fly) i collect all the form fieldnames in an php array and sent it to
the javascript function:
$velden is the name of the array with the form fieldnames.
------------------- php code ------------------------------------
$info .= "<form onsubmit='return FormZoek(this,\"" . $velden . "\")'
name='zoek' ";
$info .= "action='" . $action . "' method='" . $method . "' >";
------------------------------------------------------------------
in javascript i want to do something like this:
------------------------ javascript code -----------------------
var aantal;
function FormZoek(hetForm, velden){
aantal = velden.length
for(var i = 0; i < aantal; i++ ) {
if (hetForm.velden[i].value == ""){
alert("Wilt u a.u.b. veld " + velden[i] + " invullen.");
hetForm.veld[i].focus();
return (false);
}
return (true);
}
}
----------------------------------------------------------------
but it is not working.
with an alert i can't even get the names from the array!
my question is: is it possible to sent an array from php to javascript and,
in javascript loop through the array and use this data?
thanks | | | | re: sending an array from php to javascript
nescio wrote:[color=blue]
> hello,
>
> i have made an application in php so that people can make, on the fly, a
> form.
>
> when they submit the form there is a javascript formvalidation.
>
> because we do not know how many fields there are in the form (it is made on
> the fly) i collect all the form fieldnames in an php array and sent it to
> the javascript function:
>
> $velden is the name of the array with the form fieldnames.
>
> ------------------- php code ------------------------------------
> $info .= "<form onsubmit='return FormZoek(this,\"" . $velden . "\")'
> name='zoek' ";
> $info .= "action='" . $action . "' method='" . $method . "' >";
> ------------------------------------------------------------------[/color]
....[color=blue]
> my question is: is it possible to sent an array from php to javascript and,
> in javascript loop through the array and use this data?[/color]
Without comment on the appropriateness of your approach, if should work
as you expect if you replace your $velden with:
'["' . implode('","', $velden) . '"]'
Csaba Gabor from Vienna | | | | re: sending an array from php to javascript
nescio wrote:[color=blue]
> hello,
>
> i have made an application in php so that people can make, on the fly, a
> form.
>
> when they submit the form there is a javascript formvalidation.
>
> because we do not know how many fields there are in the form (it is made on
> the fly) i collect all the form fieldnames in an php array and sent it to
> the javascript function:
>
> $velden is the name of the array with the form fieldnames.
>
> ------------------- php code ------------------------------------
> $info .= "<form onsubmit='return FormZoek(this,\"" . $velden . "\")'
> name='zoek' ";
> $info .= "action='" . $action . "' method='" . $method . "' >";
> ------------------------------------------------------------------
>
>
> in javascript i want to do something like this:
>
> ------------------------ javascript code -----------------------
> var aantal;
> function FormZoek(hetForm, velden){
>
> aantal = velden.length
>
> for(var i = 0; i < aantal; i++ ) {
> if (hetForm.velden[i].value == ""){
> alert("Wilt u a.u.b. veld " + velden[i] + " invullen.");
> hetForm.veld[i].focus();
> return (false);
> }
>
>
> return (true);
> }
>
>
> }[/color]
Let $velden be an comma separated string of the form field names, and
pass it into FormZoek() as a parameter.
For eg : $velden = "fld1,fld2,fld3";
Then in FormZoek(), convert the string input into an array, and loop
through it as below. I have not tested the following code, so you might
have to hack it up for errors/browser independence etc..
[snippet]
var form_elems = velden.split(",");
for ( var i=0; i<form_elems.length; i++ )
{
if ( hetForm.elements(form_elems[i]).value == "" )
{
//// validation ////
}
}
[/snippet]
[color=blue]
>
> my question is: is it possible to sent an array from php to javascript and,
> in javascript loop through the array and use this data?
>[/color]
No. You cannot send a php array to javascript. What you in turn can do
is create a javascript array through php.
$js_array = "js_array = new Array(" . count($form_elems) . ");";
for ($i=0; $i<count($form_elems); $i++)
{
$js_array .= "js_array[" . $i . "] = " . $form_elems[$i];
}
Again, I have not tested the above snippet. With a few changes, if any,
it should work fine.
Regards,
Rithish. | | | | re: sending an array from php to javascript
[color=blue]
> Without comment on the appropriateness of your approach, if should work
> as you expect if you replace your $velden with:
> '["' . implode('","', $velden) . '"]'[/color]
feel free to advice a better approach.
i am a novice to javascript and i am alway willing to learn something new | | | | re: sending an array from php to javascript
[color=blue]
> Let $velden be an comma separated string of the form field names, and
> pass it into FormZoek() as a parameter.
> For eg : $velden = "fld1,fld2,fld3";
>[/color]
i have sent the information now as a string, converted the string into an
array in javascript and then looped through the form elements and it works!
thanks for your help | | | | re: sending an array from php to javascript
Rithish wrote:
[color=blue]
> nescio wrote:[color=green]
>> my question is: is it possible to sent an array from php to javascript
>> and, in javascript loop through the array and use this data?[/color]
>
> No. You cannot send a php array to javascript. What you in turn can do
> is create a javascript array through php.
>
> $js_array = "js_array = new Array(" . count($form_elems) . ");";[/color]
^^^^^^^^^^^^^^^^^^
Unnecessary (JS arrays are dynamic by default), and error-prone. Depending
on the implementation, you might end up with an array having a number as
its sole element.
[color=blue]
> for ($i=0; $i<count($form_elems); $i++)
> {
> $js_array .= "js_array[" . $i . "] = " . $form_elems[$i];[/color]
Unnecessarily inefficient, and it creates something that is likely to be
syntactically wrong:
js_array[n] = some string value
[color=blue]
> }
>
> Again, I have not tested the above snippet. With a few changes, if any,
> it should work fine.[/color]
<?php
$form_elems = array('x', 'y"', 'z\'');
?>
var js_array = new Array(
<?php
$len = count($form_elems);
for ($i = 0; $i < $len; $i++)
{
echo ' "' . addslashes($form_elems[$i]) . '"'
. (($i < $len - 1) ? ',' : '') . "\n";
}
?>
);
PointedEars |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|