Javascript and PHP

libdanz
libdanz used Ask the Experts™
on
When I use Checkbox, I name them as Checkname[] for me to parse them as arrays in PHP, but this will cause my Javascripts to have errors.
 
Can anyone help

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Can u tell what is the javascript error ?

Author

Commented:
<INPUT name=test[] value=23>
<INPUT name=test[] value=24>
<INPUT name=test[] value=25>
This will work fine when submitted to PHP , but will not work fine when using Javascript to manipulate it as an array (test)

<INPUT name=test value=23>
<INPUT name=test value=24>
<INPUT name=test value=25>
This will work fine with Javascript, but will not be treated as an array with PHP...
MHQ

Commented:
You can't name a checkbox so that it ends with [] to make it an array... name all of them Checkname... then you should be able to parse them as an array in PHP the way you want it. If multiple objects have the same name from a HTML form submission, they will be accessible as an array when you retrieve them.

Regards,
MHQ
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
<INPUT name=test[] value=23>
<INPUT name=test[] value=24>
<INPUT name=test[] value=25>
This will work fine when submitted to PHP , but will not work fine when using Javascript to manipulate it as an array (test)

<INPUT name=test value=23>
<INPUT name=test value=24>
<INPUT name=test value=25>
This will work fine with Javascript, but will not be treated as an array with PHP...

Author

Commented:
<INPUT name=test[] value=23>
<INPUT name=test[] value=24>
<INPUT name=test[] value=25>
This will work fine when submitted to PHP , but will not work fine when using Javascript to manipulate it as an array (test)

<INPUT name=test value=23>
<INPUT name=test value=24>
<INPUT name=test value=25>
This will work fine with Javascript, but will not be treated as an array with PHP...

Commented:
First off a better way to do the above would be to do this if you have a LOT of those values like 1 - 25.

<?php

$a= 1;

while ($i <= 25) {
   print "<input name=test value=$i ";
   $i++;
}
?>

To parse this on the second page you now use the brackets

First Solution:

print "$test[0]";
print "$test[1]";

or for a lot of these I usually replace the number variables with $a like this

Second Solution:

$a = 0
while ( ! isset ($test) ) {
   print "$test[$a]";
   $a++;
}

This should output
test[1]
test[2]
test[3]
which these point to each item in the array

Please let me know if you cannot get this to work its been a while since I have done this and I am not sure if the second solution is completly correct you might have to use a double $ but I will have to look it up.

I am pretty sure the second one works but I cannot remember if there is a set way you have to do the $a inside the brackets.

Commented:
Oops first part should be

<?php

$i= 1;  Forgot to make this an i not an a

while ($i <= 25) {
  print "<input name=test value=$i ";
  $i++;
}
?>
What kind of javascript operation you plan to do ?

If you can put it up, I can modify that javascript code.

Regards,
JD

Commented:
You can use array style input names (test[]) and still use the variable in javascript.  You just can't call it directly.  Here is what you have to do:

<form name="form_name">
  <input type="checkbox" name="check[]">
</form>

<script>
function function_name(tForm) // <--- tForm = "form_name"
{
  tForm["check[]"].checked = true;
}
</script>

That is how you can use input arrays in javascript.

  -- Rob
Commented:
in order to get rid of the error try this:


<INPUT name="test[]" id="test1" value="23">
<INPUT name="test[]" id="test2" value="24">
<INPUT name="test[]" id="test3" value="25">

Now, JavaScript uses test1,test2,test3, etc. and PHP uses test[]

cheers =0)

Author

Commented:
Thank you Smada and ivanmata. I dont exactly understand how this forum works. If I Click on Accept as Answer what will happen, because your answers have been useful to me...

Commented:
well, you have two options:

1) Accept any comment, and post another question just to give points to the other one i.e. if you accept Smada's comment, post another question with subject: 'points for ivanmata'... or vice versa...

2) Accept just one of our comments (the one you're gonna use)

cheers =0)

Commented:
I'm not in it for the points.  Just give 'em to ivanmata.  His method is probably easier, especially when dealing with checkboxes.  I use my method more for selects.

  -- Rob

Commented:
Something else that I have done when trying to combine arrays with JavaScript, is to do something like the following:

<INPUT name=test[23] value=23>
<INPUT name=test[24] value=24>
<INPUT name=test[25] value=25>

This creats an array just like if it was "test[]", but now each one has a unique identifier, so that JavaScript can see them seperately.

The above does require a few other steps to complete the process though.  I usually have a hidden field with a list of all the values in the array:

<input type="hidden" name="allVals" value="23|24|25">

Then after submission, back on the PHP side of things, you can do a for loop on allVals:

<?
while($x=0; $x<count($allVals); $x++){
 $curVal = $test[$allVals[$x]];
 // do whatever you want with this $curVal here
}
?>

May be more complicated then it's worth, but this is how I beat arrays in both PHP and JavaScript.

Commented:
woops... that "while" loop should be a "for" loop

<?
for($x=0; $x<count($allVals); $x++){
$curVal = $test[$allVals[$x]];
// do whatever you want with this $curVal here
}
?>

Commented:
I am new on this site so: hi all.

Correct me if am am wrong but putting a checkbox into an array is not a good idea because with the post of the form only checked checkboxes will be in your array.

Example: if you have 4 checkboxes, check the 2nd and 4th, in php you will receive an array of two elements, both checked.

Problem can be solved by using the method of zelph77's earlier post.

New question: if I use the method of ivanmata's post, is there a way of having the form elements in a javascript array?

thx
Hi,

i have had the same problem with javascript and php.
Use in the htmlform the name AND id-tag
Example:

<INPUT name=test[] id=test value=23>
<INPUT name=test[] id=test value=24>
<INPUT name=test[] id=test value=25>


Now Javascript is able to manipulate it as an array and php can use it as an array :-)
Greetz Torsten

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial