View Full Version : Creating / Using Arrays with Forms

07-07-2007, 01:23 PM
Hey Guys,

I've been searching all over the internet for a simple answer to this and can't seem to get anything working... Here is what I am looking to do:

Use a form which will update a bunch of like elements:

<form method="POST" action="samepage.html">

<!-- My First Set From the Database -->
<input name="first_input" value="1">
<input name="second_input" value="1">
<input name="third_input" value="1">

<!-- My Second Set From the Database -->
<input name="first_input" value="1">
<input name="second_input" value="1">
<input name="third_input" value="1">

<input type="submit" value="Update" name="update_links" >

I know the names cannot be the same for all of the names of the inputs, but I want some sort of array of inputs for this and then maybe I can iterate through them on the postback.

I am detecting the submit, via:

if ($_POST['update_links'])
{ //Code Goes Here }

I would expect to be able to do this quite simply, but just can't figure it out :( I hope the example above was clear enough... There can be any number of groups of inputs, so I need to iterate thru all of them...

Thanks a ton!!!!

07-07-2007, 01:33 PM
Maybe this is what you want to do..

$inputs = array ( "first_input", "second_input", "third_input");

foreach ($inputs as $key){
echo "<input name=".$key." value=\"1\">";

That would create all the input fields from array.

07-07-2007, 02:09 PM
This will help you with creating form field names that are arrays - http://www.php.net/manual/en/faq.html.php#faq.html.arrays

07-07-2007, 02:52 PM
I've got the inputs down, I think... I have had this:

echo '<input type="hidden" name="site_id['.$indexid.']" value="'.$msubs['sid'].'">';

echo '<input type="hidden" name="cas_id['.$indexid.']" value="'.$gcc['cid'].'">';

echo '<input type="hidden" name="exists['.$indexid.']" value="'.$linkinfo[0]['count'].'">';

Now, I need to be able to get all the site_id[x], cas_id[x], exists[x] out of the post after the submit... That is really what I have been struggling with.

I did try looking at that array page from php.net and just couldn't come up with code from it that would iterate through the above :( I tried, though!

thanks again.

07-07-2007, 05:56 PM
Been trying different methods all morning :( Nothing is working for me.

I can't find any example on the web of what I am trying to accomplish even though I am sure it is being done over and over again.

07-07-2007, 06:09 PM
I would try to help you, but i still don't understand what you are talking about. And i think i'm not the only one, couse people here would answer you i'm sure if they understood you better.

Sometimes a good night sleep solves the problem :)

07-07-2007, 06:51 PM
You can use foreach(...) to iterate through each array or even the whole $_POST array if you want - http://php.net/foreach

07-07-2007, 07:08 PM
I figured out what was wrong... 1 stupid mistake and 1 way I didn't know about :)

Stupid Mistake...

<textarea name="" name="MyVar"></textarea>

I had 2 names identified and it took the 1st one :(

To Retreive:

for ($i = 1; $i <= $total; $i++) {
$myitem = $_POST['MyVar'][$i];

echo $i.' - '.$myitem.'<br />';