By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,713 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,713 IT Pros & Developers. It's quick & easy.

Using Arrays in a Form

P: n/a
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:

IN THE HTML FILE:
....
<form action="insert_recipe.php" method="POST">
<?php
$ingr_array = array(array());
?>
....
some code...
....

<tr>
<td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][0] ?>" ID="quantity0" size="5"
class="defaultfont"></td>
<td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][1] ?>" ID="measure0" size="20"
class="defaultfont"></td>
<td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][2] ?>" ID="ingredient0" class="defaultfont"
size="27"></td>
<td align="center" class="cellbg"><input type="checkbox" name="<?php
$ingr_array[0][3] ?>" ID="optional0" class="defaultfont"></td>
</tr>
// 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.

M.D.
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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:
$arvar=implode(",",$array_name);
Submitted the resulting variable into the db: SQL="INSERT into tblname
('col_name') VALUES ($arvar)
Reverse to extract from DB/view data...

HTH

Jul 17 '05 #2

P: n/a
*** M.D. escribió/wrote (23 Sep 2004 12:54:48 -0700):
$ingr_array = array(array()); <td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][0] ?>" ID="quantity0" size="5"


Where do you set the value of $ingr_array?

--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (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
--
Jul 17 '05 #3

P: n/a
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

http://www.tonymarston.net

"M.D." <m6**@hotmail.com> wrote in message
news:e0**************************@posting.google.c om...
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:

IN THE HTML FILE:
...
<form action="insert_recipe.php" method="POST">
<?php
$ingr_array = array(array());
?>
...
some code...
...

<tr>
<td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][0] ?>" ID="quantity0" size="5"
class="defaultfont"></td>
<td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][1] ?>" ID="measure0" size="20"
class="defaultfont"></td>
<td align="center" class="cellbg"><input type="text" name="<?php
$ingr_array[0][2] ?>" ID="ingredient0" class="defaultfont"
size="27"></td>
<td align="center" class="cellbg"><input type="checkbox" name="<?php
$ingr_array[0][3] ?>" ID="optional0" class="defaultfont"></td>
</tr>
// 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.

M.D.

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.