Jump to content

[SOLVED] Submitting multiple inputs w. same name is not returning an array


vincentwansink

Recommended Posts

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
Share on other sites

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
Share on other sites

  • 1 month later...

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.