vincentwansink Posted March 23, 2009 Share Posted March 23, 2009 I'm banging my head against the wall because code that worked fine last week is no longer working. I have a list of time records. Each time record has a unique id which I submit along with all the other data on update. The input for the time id is called timeid[] on each row. The names are not unique and this is the way I need it to be. When I submit the rows I (should) get an array of values for timeid which I then loop through. This used to work fine. I've been using this technique for many years on many different websites including this one. If I take away the square brackets then it only grabs the last input with that name on submit. This morning I come into work and it's no longer working. The code is not finding an array upon submit and therefore is not looping through my rows and updating data. :banghead: When I run the following loop to see what values I'm getting in my $_POST foreach($_POST as $var => $value){ echo $var . ' : ' . $value . "<br>"; } I get the word "Array" for each value. I'm not sure if that's right but it seems promising. However, when I do the following: echo is_array($_POST[timeid]) ? '<br>Is an Array' : '<br> Is not an Array'; I get "Is not an Array" every time. When I do this: echo count($_POST[timeid]) It returns a count of 1. When I do this: echo $_POST[timeid][0] It returns the letter "A", which is obviously the first letter of the word "Array". If anybody can give me a clue it would be much appreciated. Now, I did make some changes to the site but I don't see how this could cause PHP to no longer recognize arrays. The list in question (the list of time enries) is being generated by a function. I added some code to this function that prints some images and divs if certain conditions are true. I did not change the time id input or any other inputs. All I did was add more code to this function. The new code works like a charm by the way. Is there a point at which a function gets too large that causes PHP to go stupid? This is essentially what my form looks like. I've removed all the css and javascript for readability. This code shows a list of three kids. Alexander Shillingford, Austin Lorenzo and Michayla Lawrence. For each child I have start time (hour,minute,ampm) and end time (hour,minute,ampm) as well as a timeid, childlistid and subtotal. <form id=mainform name=mainform method=post action="children.php"> <table> <tr> <td>Alexander Shillingford</td> <td> <table> <tr><td> <input type=text id=starthour name=starthour[] value='8'>: <input type=text id=startminute name=startminute[] value='45'> <input type=text id=startampm name=startampm[] value='AM'> <input type=hidden name=timeid[] value='1814'> <input type=hidden name=childlistid[] value=''></td> <td> to </td> <td> <input type=text id=endhour name=endhour[] value=''>: <input type=text id=endminute name=endminute[] value=''> <input type=text id=endampm name=endampm[] value=''></td> <td><img alt='delete' src='images/deletefaded.gif'></td> <td> <input id=subtotal name=subtotal[] value=''></td></tr> </table> </td> </tr> <tr> <td>Austin Lorenzo</td> <td> <table> <tr><td> <input type=text id=starthour name=starthour[] value='8'>: <input type=text id=startminute name=startminute[] value='45'> <input type=text id=startampm name=startampm[] value='AM'> <input type=hidden name=timeid[] value='1814'> <input type=hidden name=childlistid[] value=''></td> <td> to </td> <td> <input type=text id=endhour name=endhour[] value=''>: <input type=text id=endminute name=endminute[] value=''> <input type=text id=endampm name=endampm[] value=''></td> <td><img alt='delete' src='images/deletefaded.gif'></td> <td> <input id=subtotal name=subtotal[] value=''></td></tr> </table> </td> </tr> <tr> <td>Michayla Lawrence</td> <td> <table> <tr><td> <input type=text id=starthour name=starthour[] value='8'>: <input type=text id=startminute name=startminute[] value='45'> <input type=text id=startampm name=startampm[] value='AM'> <input type=hidden name=timeid[] value='1814'> <input type=hidden name=childlistid[] value=''></td> <td> to </td> <td> <input type=text id=endhour name=endhour[] value=''>: <input type=text id=endminute name=endminute[] value=''> <input type=text id=endampm name=endampm[] value=''></td> <td><img alt='delete' src='images/deletefaded.gif'></td> <td> <input id=subtotal name=subtotal[] value=''></td></tr> </table> </td> </tr> </table> </form> Link to comment https://forums.phpfreaks.com/topic/150769-solved-submitting-multiple-inputs-w-same-name-is-not-returning-an-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 23, 2009 Share Posted March 23, 2009 Find out what you are actually getting - echo "<pre>"; print_r($_POST); echo "</pre>"; Link to comment https://forums.phpfreaks.com/topic/150769-solved-submitting-multiple-inputs-w-same-name-is-not-returning-an-array/#findComment-792125 Share on other sites More sharing options...
vincentwansink Posted March 24, 2009 Author Share Posted March 24, 2009 I did print out my post but every value was the word "Array". Anyway, I discovered what was causing the problem. There was nothing wrong with my code above (as I suspected) but I'd totally forgotten that I had also added another piece of code in my common file that was causing the problem. This is the culprit here. I've removed it. //grabs the $_POST variables and adds slashes to help prevent sql injection attacks foreach ($_POST as $key => $input_arr) { $_POST[$key] = addslashes($input_arr); } It doesn't cause any problem for single input posts, but when I submit an array of inputs (number of inputs all with the same name) it obviously messes things up. Link to comment https://forums.phpfreaks.com/topic/150769-solved-submitting-multiple-inputs-w-same-name-is-not-returning-an-array/#findComment-793115 Share on other sites More sharing options...
sarahaziz2002 Posted May 19, 2009 Share Posted May 19, 2009 Hello Vincent i have the same problem but i don't know where is the error. And if i want to create new input elements and insert into mysql database how could i change the code to do so. This is the code <form method="post"> <input type="hidden" id="max_id" value="1"/> <table> <tr> <td align="left"><h2>Animation Group</h2></td> <td><input type="button" value="Rename"/></td> </tr> </table> <table> <tr> <thead> <tr> <th><h5>user name</h5></th> <th><h5>user real name</h5></th> </tr> </thead> <tr> <td><input type="text" name="rolename[]" id="rolename[]" value=""/></td> <td><input type="text" name="rolerealname1" id="rolerealname1"/></td> <td><input type="button" value="Edit"/></td> </tr> <tr> <td><input type="text" name="rolename[]" id="rolename[]"/></td> <td><input type="text" name="rolerealname2" id="rolerealname2"/></td> <td><input type="button" value="Edit"/></td> </tr> <tr> <td><input type="text" name="rolename[]"/></td> <td><input type="text" name="rolerealname3" id="rolerealname3"/></td> <td><input type="button" value="Edit"/></td> </tr> </table> </form> <div><input type="submit" value="Save" onClick="saveData()"/></div> javascript function saveData() { $.post("groups_users.php", $("form").serialize(), function(returned_data) {alert(returned_data);}); } php <?php foreach($_POST['rolename'] as $var => $value) { $_POST[$var] = addslashes($value); echo $value; } ?> Link to comment https://forums.phpfreaks.com/topic/150769-solved-submitting-multiple-inputs-w-same-name-is-not-returning-an-array/#findComment-837109 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.