Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Problem with the 'for' loop for processing multiple forms
Old 10-01-2008, 12:52 PM Problem with the 'for' loop for processing multiple forms
Junior Talker

Posts: 3
Trades: 0
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.
dedurus is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-01-2008, 01:16 PM Re: Problem with the 'for' loop for processing multiple forms
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
You should probably post the code you do have, so that we may examine it. However, it looks as if you may need to study arrays and looping for a short time, so that you have a better comprehension of the tools needed to accomplish what you need. http://us.php.net/foreach http://us.php.net/array
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-01-2008, 06:34 PM Re: Problem with the 'for' loop for processing multiple forms
Sleeping Troll's Avatar
Ultra Talker

Posts: 351
Name: Butch Begy
Trades: 0
You need AJAX, it is not that tough! give it a go!
Sleeping Troll is offline
Reply With Quote
View Public Profile
 
Old 10-01-2008, 08:48 PM Re: Problem with the 'for' loop for processing multiple forms
Junior Talker

Posts: 3
Trades: 0
Thanks guys for the tips.

Anyway, here's the code.

This is the number_of_rooms.php:

Code:
<form action="room_details.php" method="post">
<label for="room_number">Total room number in the hotel</label>
<input name="room_number" id="room_number" />
<input type="submit" name="insert_room_number" id="insert_room_number" value="Insert the number of rooms" />
</form>
In this form i put the total number of rooms for the hotel, and that value is passed to the room_details.php file:

Code:
<?php
$room_number=$_POST['room_number'];
echo '<form id="form1" name="form1" method="post" action="">';
for($i=1; $i<=$room_number; $i++){
 
 $room_type="room_type".$i;
 
  $capacity="capacity".$i;
 
  
 
echo '<fieldset><legend>'."Room Number ".$i.'</legend>';
echo '<label for="room_type">'."Room Type".'</label>';
echo '<input type="text" name="'.$room_type.'" />';
echo '<br />';
echo '<label for="capacity">'."Capacity".'</label>';
echo '<input type="text" name="'.$capacity.'" />';
echo '<br />';
 
echo '<input name="room_number" type="hidden" value="'.$room_number.'" />';
 
echo '</fieldset>';

}
 
echo '<input class="button" type="submit" name="insert_room_details" value="insert_room_details">';
echo '</form>';
} 
if(isset($_POST['insert_room_details'])){                  

     $room_number=$_POST[$room_number];             

     $capacity=$_POST[$capacity];                          

     
     $qv="INSERT INTO `room`(`room_number`,`capacity`) VALUES ('$room_number','$capacity')";
     $res=mysql_query($kv) or die(mysql_error());
     }
  

}
?>
When the $room_number from the previous form is passe to room_details.php, i got a number of forms that equals to $room_number.
All fields in each form diplayed in room_details.php has the same name as in other forms, but incremented by the $i value from the "for" loop, so every field actually has unique name.
What i'm trying to accomplish is to process all forms to database, each as separate record, and use only one submit button.
I've tried several variations of the "for" loop, bot none of them gave me the expected results.

Some of them has executed only once, and only the last form values were inserted into the database.

I've been trying to solve this for 4 days (), and yet have no idea how to fix this.
I would like to use AJAX, but still i'm PHP begginer, so i guess i should learn some PHP first.

Does anyone has any idea how t osolve this?

Thanks in advance.





Last edited by dedurus; 10-01-2008 at 09:01 PM..
dedurus is offline
Reply With Quote
View Public Profile
 
Old 10-02-2008, 06:38 PM Re: Problem with the 'for' loop for processing multiple forms
wayfarer07's Avatar
Poo on You

Latest Blog Post:
Introducing WowWindow
Posts: 3,987
Name: Abel Mohler
Location: Asheville, North Carolina USA
Trades: 0
Although I don't have time to write the code for you, I suggest you draw a map, that explains the exact logical steps that you must take in order to accomplish your goal. Once you have a solid algorithm, the logic and syntax will fall into place. Whenever you have questions, look things up. Work out the logic first, then worry about the PHP. I wouldn't worry about anything involving AJAX, for now. You don't need it for this.
__________________
I build web things. I work for the startup
Please login or register to view this content. Registration is FREE
.
wayfarer07 is offline
Reply With Quote
View Public Profile Visit wayfarer07's homepage!
 
Old 10-02-2008, 11:06 PM Re: Problem with the 'for' loop for processing multiple forms
Average Talker

Posts: 29
Name: Dave
Trades: 0
I think your values you're getting from the forms need to be stored in an array and your query code will loop through every element in those arrays. I think there may be a problem when you're retrieving your data from the submit, you're still referencing $capacity after the loop so $capacity=$_POST[$capacity]; I believe, is always going to give you the last element in the form.

Last edited by davie_gravy; 10-02-2008 at 11:17 PM..
davie_gravy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Problem with the 'for' loop for processing multiple forms
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.63133 seconds with 12 queries