Connecting Tech Pros Worldwide Forums | Help | Site Map

sending an array from php to javascript

nescio
Guest
 
Posts: n/a
#1: Mar 29 '06
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






Csaba Gabor
Guest
 
Posts: n/a
#2: Mar 29 '06

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

Rithish
Guest
 
Posts: n/a
#3: Mar 29 '06

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.

nescio
Guest
 
Posts: n/a
#4: Mar 29 '06

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


nescio
Guest
 
Posts: n/a
#5: Mar 29 '06

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


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Mar 29 '06

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
Closed Thread


Similar JavaScript / Ajax / DHTML bytes