473,573 Members | 4,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14209
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*********@hot mail.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1712
by: Konrad | last post by:
After I register variable in session : session_start(); session_register($var); new page opens include("register2.php"); At the page I fill some forms and send variables from forms to another
0
1311
by: Zahid | last post by:
Hi, Im a little confused with sending and receiving data using UDP comms. Im writing an app that sends and recieves data (a byte array) using the UDP protocol. The data I send is 36 bytes long (confirmed when I display "data.length -1"). But when I receive the data on the other end and do "Data.length - 1" It displays 35 (1 size smaller...
5
1817
by: Danny | last post by:
I am working on a project in which a number of client applications will be posting xml documents as a byte array to an ASP.NET page on our web server. I am trying to simulate the process and run into problems. Sending code: ASCIIEncoding encoding = new ASCIIEncoding(); string lcUrl = "http://localhost/test/receive.aspx"; HttpWebRequest...
0
1336
by: Danny | last post by:
I am working on a project in which a number of client applications will be posting xml documents as a byte array to an ASP.NET page on our web server. I am trying to simulate the process and run into problems. Sending code: ASCIIEncoding encoding = new ASCIIEncoding(); string lcUrl = "http://localhost/test/receive.aspx"; HttpWebRequest...
7
6974
by: h7qvnk7q001 | last post by:
I'm trying to implement a simple server-side form validation (No Javascript). If the user submits a form with errors, I want to redisplay the same form with the errors highlighted. Once the form is correct I need to submit to another page that uses the form data. I first tried making the form submit action= field point to the same file....
4
2253
by: Cerebral Believer | last post by:
Hi I need help! Forgive me I am a PHP newbie. I have a small script that enables me to send a form from an HTML page. I want to use the HTML formatted form because the design of my website is complex, and I don't want to have to mess around with formatting a page using HTML within php. So basically the "action" of the HTML page sends the...
4
2990
by: dac | last post by:
I am quietly going insane on this project. I've never worked on a project like this one before. All my previous sticky forms were for data entry, not editing. I don't know how to display the form with data from the database, allow the user to update it, and then display the form again with POST data. I can get the data out of the database and get...
26
2788
by: Jerim79 | last post by:
I need to create a form that takes a number that the user enters, and duplicates a question the number of times the user entered. For instance, if the customer enters 5 on the first page, when they press next the form generates "How old are you?" 5 times on the page. The customer will answer all 5 questions then press next. Finally, all the...
15
2156
by: Kevin Davis | last post by:
Hello, I'm a new person when it comes to PHP and I have a quick question. I would like to create a form that will allow the user to add more information using the same form in case they have (similar to various employment sites). What would be the best way of using form arrays for that function?
0
7741
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8167
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8028
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5252
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3692
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2164
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1263
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
987
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.