Page 1 of 1
Using for loop to process several forms
Posted: Wed Oct 01, 2008 11:14 am
by dedurus
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.
Re: Using for loop to process several forms
Posted: Wed Oct 01, 2008 12:17 pm
by Ziq
To realize this problem you can use this method.
Firstly, you should create some form, for example:
Code: Select all
<form action="" method="POST"><br>
<b>First room</b><br>
<input name="descriptions[0][]" value="This is a description for room 1"><br>
<input name="quality[0][]" value="This is a quality mark for room 1"><br><br>
<b>Second room</b><br>
<input name="descriptions[1][]" value="This is a description for room 2"><br>
<input name="quality[1][]" value="This is a quality mark for room 2"><br><br>
<!-- Many others rooms //-->
<b>N-room</b><br>
<input name="descriptions[n-1][]" value="This is a description for room n"><br>
<input name="quality[n-1][]" value="This is a quality mark for room n"><br>
<input type="submit">
</form>
Then, you can use php-handler:
Code: Select all
<?php
// form's handler
$count_of_forms = 2;
for ($i = 0; $i < $count_of_forms; $i++)
{
print_r($_POST['descriptions'][$i]);
print_r($_POST['quality'][$i]);
}
?>
Re: Using for loop to process several forms
Posted: Wed Oct 01, 2008 1:10 pm
by dedurus
Thanks for this Ziq.
Anyway, i have managed to make the dynamic forms with the following code:
number_of_rooms.php code:
Code: Select all
<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>
And the
room_details.php code:
Code: Select all
<?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=\"broj_na_sobi\" type=\"hidden\" value=\"$room_number\" />";
echo '</fieldset>';
if(isset($_POST['insert_room_details'])){ //Possible
$room_number=$_POST[$room_number]; //that the problem
$capacity=$_POST[$capacity]; //is in this part
$qv="INSERT INTO `room`(`room_number`,`capacity`) VALUES ('$room_number','$capacity')";
$res=mysql_query($kv) or die(mysql_error());
}
}
echo "<input class=\"button\" type=\"submit\" name=\"'insert_room_details\" value=\"'insert_room_details\">";
echo '</form>';
}
}
?>
As you can see from the code from
room_details.php, i assign the $i parameter to each field in each new form, so it can be distinguished from other fields in other forms.
I think the problem lies in the "for" loop, but cannot realize where exactly and how to solve this.
Thanks again, and hope someone will help me this with my-brain-killing issue.
Re: Using for loop to process several forms
Posted: Wed Oct 01, 2008 1:51 pm
by Ziq
Sorry, but I cannot understand what are you doing?
Firstly, you have the syntaxes errors.
Code: Select all
//...
echo '<label for=\"capacity\">'."Capacity".'</label>';
echo '<input type=\"text\" name=\"$capacity\" />';
//...
Attention to the quotes. You are using a (') and you cannot insert a variable. You should correct this:
Code: Select all
//...
echo '<label for="capacity">'."Capacity".'</label>';
echo '<input type="text" name="'.$capacity.'" />';
//...
And I cannot understand why are you use INSERT query inside "for" loop? I think, better split your task. Firstly, create a HTML form, secondly, INSERT the values into database.
You should try to search the errors yourself. Try to look on generated HTML-code. Dump your $_POST variable
Code: Select all
<?
// Insert into top of your code
print_r($_POST);
?>
Re: Using for loop to process several forms
Posted: Wed Oct 01, 2008 6:03 pm
by dedurus
Thanks again Ziq.
I have corrected the syntax errors you pointed, but yet i still cannot insert anything in the database, although the print_r($_POST) is giving me all the values i have inserted into the forms.
I have put the INSERT query inside the "for" loop because i though that it would be better to put it there, since i have one form displayed several times (depending on the value of the $room_number in the number_of_rooms.php file), so i wanted to put every form as a separate record.
That's why i have only one Submit button, although there are several occasions of the same form.
Guess this is where my problem lies, since no record is processed at all, but don't know how to fix it.
I think that the fix is somewhere in the loop, but cannot find an appropriate solution.
Any idea for this?
Re: Using for loop to process several forms
Posted: Thu Oct 02, 2008 2:32 am
by Ziq
Maybe I am not understand something... OK, are you want to create a form including many forms and you want to process it together?
If I am right:
Code: Select all
<?php
$room_number=3; // Change this
/**
* PHP handler
*/
// If user send a data
if ($_POST['action'] == 1)
{
for($i=1; $i<=$room_number; $i++)
{
$room_number_i =$_POST['room_type'.$i];
$capacity_i =$_POST['capacity'.$i];
$qv='INSERT INTO `room`(`room_number`,`capacity`) VALUES (\''.$room_number_i.'\',\''.$capacity_i.'\')';
echo $qv.'<br>'; // delete this :)
// Uncomment this
//$res=mysql_query($kv) or die(mysql_error());
}
}
/**
* Create HTML form.
*/
echo '<form id="form1" name="form1" method="post" action="">'."\n\r";
for($i=1; $i<=$room_number; $i++)
{
$room_type="room_type".$i;
$capacity="capacity".$i;
echo '<fieldset><legend>'."Room Number ".$i.'</legend>'."\n\r";
echo '<label for="room_type">Room Type</label>'."\n\r";
echo '<input type="text" name="'.$room_type.'" />'."\n\r";
echo "<br />"."\n\r";
echo '<label for="capacity">Capacity</label>'."\n\r";
echo '<input type="text" name="'.$capacity.'" />'."\n\r";
echo "<br />";
echo '</fieldset>'."\n\r";
}
echo '<input name="broj_na_sobi" type="hidden" value="'.$room_number.'" />'."\n\r";
echo '<input type="hidden" name="action" value=1>'."\n\r";
echo '<input class="button" type="submit" name="insert_room_details" value="insert_room_details">'."\n\r";
echo '</form>';
?>
Try to execute this code.
P.S. You should use one type of quotes (' or "). It is really simplify understanding of you code.
Re: Using for loop to process several forms
Posted: Thu Oct 02, 2008 10:39 am
by dedurus
Ziq, you're my hero.
I haven't realized that i could use two "for" loops, one for the form, and one for the query.
Great idea.
Thanks again and "ДА ЖИВЕЕ РУСИЈА!"
