Latest news from Xavier Media
It is currently Sun Dec 08, 2013 12:25 pm

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Using the 'for' loop to process multiple forms
PostPosted: Wed Oct 01, 2008 4:37 pm 

Hi to all

I'm php novice,and tried to solve this problem for 3 days but no success. I've tried other php-help forums, Google, but cannot find an appropriate solution for the problem i'm stuck in.
I'm trying to make a simple script in which the user first enters the hotel info and then the room number that the hotel has.

After entering the the room number, the next step is to enter a info about each room separately (beds, floor,etc).
In this step i have manged to display enough forms that equals to the room number entered in the previous step (e.g if the hotel has 10 rooms, then the same form is displayed 10 times).

Also i have managed to increment the form fields respectively (e.g. the first form has room_name1 as a name for the field, the second has room_name2, and so on).

I want to process all values of each form as a separate record in the database, and for that to use only one submit button.
For example, if the hotel has 10 rooms, then the same form is displayed 10 times, and only one submit button for all the forms, which will process all 10 forms,each one as a separate record.
I tried several variations of the "for" loop, but i only "succeed" to insert the last form data, and not all the previous.

Does anyone have an idea how to make a "for" loop for quering (inserting) values from several forms that have dynamic form names?

Thanks in advance.

Report this post
Reply with quote  
 Post subject: Re: Using the 'for' loop to process multiple forms
PostPosted: Thu Oct 02, 2008 6:11 pm 
Offline Admin Admin
Super VIP Member
Super VIP Member
User avatar

Joined: Tue Mar 15, 2005 4:37 pm
Posts: 1082
Location: Sweden
Points: 1500
If you call the variables like room_name as an array it's quite easy instead of adding a number. Use fields like this:
PHP Code:
<INPUT TYPE=text NAME=room_name[] SIZE=30>
Notice the [] instead of a number!

Then when you process the variables you simply just do something like this:
PHP Code:
$room_names = $_REQUEST[room_name];
$num_rooms = count ($room_names); // Will return the number of room names entered

for ($i = 0; $i < $num_rooms; $i++;
echo '<B>Room '. ($i+1) .': </B>'. $room_names[$i] .'<BR />';

If you got 4 rooms this will print something like this:
Room 1: Jaddah
Room 2: Jaddah 2
Room 3: Jaddah 3
Room 4: Whatever name you filled in

Hope this helps :D


Xavier Media®
Join me at LinkedIn | Get your latest blog post in your signature you too

Report this post
 Profile Send private message  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Registered users: No registered users

You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
SEO MOD © 2007 StarTrekGuide ]