Page 1 of 1

Submitting multiple inputs w. same name not returning array

Posted: Mon Mar 23, 2009 12:10 pm
by vincentwansink
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

Code: Select all

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:

Code: Select all

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:

Code: Select all

echo count($_POST[timeid])
It returns a count of 1.

When I do this:

Code: Select all

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?

Re: Submitting multiple inputs w. same name not returning array

Posted: Mon Mar 23, 2009 12:18 pm
by mattpointblank
Show us your form code.

Re: Submitting multiple inputs w. same name not returning array

Posted: Mon Mar 23, 2009 1:44 pm
by califdon
You probably need to define your variable as an array before assigning indexes. Something like:

Code: Select all

$timeid=Array();

Re: Submitting multiple inputs w. same name not returning array

Posted: Mon Mar 23, 2009 3:48 pm
by vincentwansink
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.

Code: Select all

 
<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>&nbsp;to&nbsp;</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>&nbsp;to&nbsp;</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>&nbsp;to&nbsp;</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>

Re: Submitting multiple inputs w. same name not returning array

Posted: Mon Mar 23, 2009 5:45 pm
by vincentwansink
:D I found it. 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.

When I implemented this code, I tested it and it seemed to work great. Obviously I did not test all cases. It works for single input posts but obviously not for array input posts.

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);
}

[Heavy sigh of relief after wasting almost an entire day on this]