469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Receiving form variables as an array

DG
I have setup an HTML page were the user will be able to set the number
of text boxes they want to enter data in. The number of text boxes
will be saved as a hidden form variable. Below is a quick HTML code
that will show what im talking about.

<Form>
<input type="textbox" name="n1">
<input type="textbox" name="n2">
<input type="textbox" name="n3">
<input type="hidden" name="num_boxes" value="3">
When the user submits the data, it pass the info to a PHP page where
the info will be used to insert into a mysql database. What is the
best way to go about doing this? There are two methods i came up with,
and 1 will be a pain to do, and the other im not sure how to
accomplish.

1. Perform if /else statements such as
if($n1 != ""){
insert $n1 into table
}
if($n2 != ""){
insert $n2 into table
and continue upto a max value

This would work if there were 3 textboxes, however what if there are
30? Then this is extremely inefficient.
2. Save $n1 to $n3 into an array so that i could just perform a loop
for(int $i = 0; $i<$num_boxes;$i++){
insert $array[i] into table

is method two possible? If so, can someone point me in the right
direction?
Thanks for your help.
Jul 16 '05 #1
4 14024
DG wrote:
1. Perform if /else statements such as
if($n1 != ""){
insert $n1 into table
}
if($n2 != ""){
insert $n2 into table
and continue upto a max value

This would work if there were 3 textboxes, however what if there are
30? Then this is extremely inefficient.


something like this will iterate until nX is not set:

$i = 1;
while( true ) {
$varname = "n$i";
if( ! isset( $_POST[$varname] ) ) break;
$val = $$varname; # yes, that's two $$
....
}

of course, adjust $_POST to whatever you like.

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 16 '05 #2
stephan beal wrote:
$val = $$varname; # yes, that's two $$


That will only work if nX is in the global space. It is far better to pull
it from $_{POST,GET}.
--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 16 '05 #3
stephan beal wrote:
$i = 1;
while( true ) {
$varname = "n$i";


Sorry, i forgot this very important part: ++$i

--
----- stephan beal
Registered Linux User #71917 http://counter.li.org
I speak for myself, not my employer. Contents may
be hot. Slippery when wet. Reading disclaimers makes
you go blind. Writing them is worse. You have been Warned.

Jul 16 '05 #4
Hi DG!
On 25 Jul 2003 07:47:34 -0700, pr*********@hotmail.com (DG) wrote:
I have setup an HTML page were the user will be able to set the number
of text boxes they want to enter data in. The number of text boxes
will be saved as a hidden form variable. Below is a quick HTML code
that will show what im talking about.

<Form>
<input type="textbox" name="n1">
<input type="textbox" name="n2">
<input type="textbox" name="n3">
<input type="hidden" name="num_boxes" value="3">
When the user submits the data, it pass the info to a PHP page where
the info will be used to insert into a mysql database. What is the
best way to go about doing this? There are two methods i came up with,
and 1 will be a pain to do, and the other im not sure how to
accomplish.

3.

<input type="textbox" name="n[]">
<input type="textbox" name="n[]">
<input type="textbox" name="n[]">
Try that...

HTH, Jochen
--
Jochen Daum - CANS Ltd.
PHP DB Edit Toolkit -- PHP scripts for building
database editing interfaces.
http://sourceforge.net/projects/phpdbedittk/
Jul 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Konrad | last post: by
5 posts views Thread by Danny | last post: by
reply views Thread by Danny | last post: by
7 posts views Thread by h7qvnk7q001 | last post: by
4 posts views Thread by Cerebral Believer | last post: by
26 posts views Thread by Jerim79 | last post: by
15 posts views Thread by Kevin Davis | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.