Using Arrays in a Form

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi, I am pretty new on php, and have been trying to solve a problem I
am having for the past 2 days.

I have a HTML page which is going to have a form and some of the
fields in the form will work like a table to allow the user to enter
up to 15 lines on information on 4 columns, this application is to
allow someone to enter a recipe into a database, so we will have 15
lines to allow people to enter: Quantity, Mesasure, Ingredient, and a
flag to indicate if is optional or not.

So I tought that would be better to create a bi-dimensional array and
pass it to the php code that is called when submit is hit. So here is
what I have done:

<form action="insert_recipe.php" method="POST">
  $ingr_array = array(array());
some code...

 <td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][0] ?>" ID="quantity0" size="5"
 <td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][1] ?>" ID="measure0" size="20"
 <td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][2] ?>" ID="ingredient0" class="defaultfont"
 <td align="center" class="cellbg"><input type="checkbox" name="<?php
$ingr_array[0][3] ?>"  ID="optional0" class="defaultfont"></td>
// 14 more of this...

and then on my insert_recipe.php I am trying to check If I got
something using these debug messages:

   echo "Size of array is ".sizeof($ingr_array)."<BR>";
   echo "Quantity is ".$ingr_array[0][0]."<BR>";
   echo "Measure is ".$ingr_array[0][2]."<BR>";
   echo "Ingredient is ".$ingr_array[0][2]."<BR>";

So when I type any data in the form  and hit the submit button, I got
the following displayed:

Size of array is 0
Quantity is
Measure is
Ingredient is

Any idea on what I can be doing wrong here ?

Thanks in advance for any help on this.


Re: Using Arrays in a Form

Your input is wrong....try <INPUT NAME='array_name[]'>. Ive just
completed something similar and loosley did the following:

Built input form using NAME="array_name[]"
Upon submission, IMPLODED the array into a single string:
Submitted the resulting variable into the db: SQL="INSERT into tblname
('col_name') VALUES ($arvar)
Reverse to extract from DB/view data...


Re: Using Arrays in a Form

*** M.D. escribió/wrote (23 Sep 2004 12:54:48 -0700):
Quoted text here. Click to load it

Where do you set the value of $ingr_array?

-+ Álvaro G. Vicario - Burgos, Spain
+- (la web de humor barnizada para la intemperie)
++ Las dudas informáticas recibidas por correo irán directas a la papelera
-+ I'm not a free help desk, please don't e-mail me your questions

Re: Using Arrays in a Form

Unless you have register_globals ON you can only reference screen variable
through the $_POST array. What you need to do is this:

$ingr_array = $_POST['ingr_array'];

Tony Marston

Quoted text here. Click to load it

Site Timeline