processing javascript array with PHP |
processing javascript array with PHP |
joyful |
Aug 28 2011, 01:39 PM
Post
#1
|
Advanced Member Group: Members Posts: 239 Joined: 15-November 10 Member No.: 13,147 |
Hey!
I am dynamically creating form inputs with this script: CODE var counter = 1; var limit = 40; function addInput(divName){ if (counter == limit) { alert("You have reached the limit of steps: " + counter); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>"; document.getElementById(divName).appendChild(newdiv); counter++; } } (I found the above script here) This script works great! The issue arises when I attempt to process the data with PHP: CODE $myInputs = $_POST["myInputs"]; foreach ($myInputs as $eachInput) { echo $eachInput . "<br>"; } I seem to only be able to get the contents of the first input using the above script. What am I doing wrong? Should this work? I followed the tutorial at www.randomsnippets.com exactly; yet, it seems not to work! Thanks in advance! -- |
Brian Chandler |
Aug 29 2011, 10:36 PM
Post
#2
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
It helps to show the html that is being generated, not the php program generating it. For example, in the middle of the echo we have
...ut(".'"dynamicInput"'.");'>... which produces ...ut("dynamicInput");'>... (but which would be easier to read if written ...ut(\"dynamicInput\");'>... Anyway, I think the problem is that you have three [variable] text inputs with the same name, "myinputs[]". In the case of checkboxes, you can use this PHP (fake) array name to get more than one value for the same _name_ input, but it's not obvious that you can for text input. First thing to do is to check by printing out the complete $_POST array in the PHP form handler. Also print out the raw POST data (can't remember how to do that)... But I think it's cleaner to avoid the whole business of duplicate _name_ values. Can't you make the javascript generate additional text inputs with name=input2, name=input3, etc? |
Christian J |
Aug 30 2011, 06:03 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
I think the problem is that you have three [variable] text inputs with the same name, "myinputs[]". In the case of checkboxes, you can use this PHP (fake) array name to get more than one value for the same _name_ input, but it's not obvious that you can for text input. It's permitted by PHP, see code example here: http://www.php.net/manual/en/faq.html.php#faq.html.arrays (note that the default TYPE of INPUT is "text"). The following works for me. I changed the ACTION to the same page, so now you'll get a notice and warning when the page is first loaded (because the form is not yet submitted): CODE <?php $myInputs = $_POST["myInputs"]; foreach ($myInputs as $eachInput) { echo $eachInput . "<br>"; } echo "<form method='POST' action=\"\">"; echo "<div id='dynamicInput'> <div>Step 1<br><input type='text' name='myInputs[]' /></div> </div> <input type='button' value='Add another step' onClick='addInput(".'"dynamicInput"'.");'> "; echo "<div id='submitButtonWrap'><input type='submit' value='Create' name='addSubmit' /></div>"; echo "</form>"; ?> <script type="text/javascript"> var counter = 1; var limit = 40; function addInput(divName) { if (counter == limit) { alert("You have reached the limit of steps: " + counter); } else { var newdiv = document.createElement('div'); newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>"; document.getElementById(divName).appendChild(newdiv); counter++; } } </script> QUOTE But I think it's cleaner to avoid the whole business of duplicate _name_ values. Can't you make the javascript generate additional text inputs with name=input2, name=input3, etc? But then you must tease them apart in the PHP, which seems much messier. |
Brian Chandler |
Aug 30 2011, 02:23 PM
Post
#4
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
QUOTE But I think it's cleaner to avoid the whole business of duplicate _name_ values. Can't you make the javascript generate additional text inputs with name=input2, name=input3, etc? But then you must tease them apart in the PHP, which seems much messier. Not really. And there is something profoundly repugnant in programming terms in having [sort-of] variable names including [] characters. (Handling names like 'input1' 'input2' really is trivial -- something like: CODE $i = 1; while ($text = $_POST['input'. $i++]) // assuming text box must be nonempty { ...process $text... } |
Lo-Fi Version | Time is now: 25th April 2024 - 07:02 AM |