October 1, 200817 yr Hi to all I have a problem with putting data in db, with dynamically created form, actually dynamically created "n" number of times of the same form. In details, i have a form where the user inserts the number of rooms, and when the submit button is pressed, the form for inserting info for each room separately is displayed n times, where n is the number of the rooms which was entered in the previous form. The code (number_of_rooms.php): $hotel_id=mysql_insert_id(); //the id of the hotel from previous query </p> <form id="form1" name="form1" method="post" action="rooms_details.php"> <label for="number_of_rooms">Total room number</label> <input type="text" name="number_of_rooms" id="number_of_rooms" /> <input name="hotel_id" type="hidden" value="<?php echo $hotel_id; ?>" /> <p> <input type="submit" name="insert_room_number" id="insert_room_number" value="Insert Total Room Number" /> </p> </form> Code from room_details.php : <form id="form1" name="form1" method="post" action="room_details.php?number_of_rooms=$number_of_rooms"> <?php for($i=1;$i<=$number_of_rooms;$i++){ ?> <fieldset><legend>Room Number <?php echo $i; ?></legend> <label for="room_number">Real Room Number</label> <input type="text" name="room_number<?php echo $i; ?>" id="_room_number" /> <br /> <label for="room_type">Room Type</label> <input type="text" name="room_type<?php echo $i; ?>" id="room_type" /> <br /> <label for="room_name">Room Name</label> <input type="text" name="room_name<?php echo $i; ?>" id="room_name" /> <input name="hotel_id" type="hidden" value="<?php echo $hotel_id; ?>" /> <input name="number_of_rooms" type="hidden" value="<?php echo $number_of_rooms; ?>" /> </fieldset> <?php } ?> <input type="submit" name="vnesi_site_sobi" id="insert_all_room_data" value="Insert data for all rooms" /> </form> So far works fine, since if i enter the number 3 in the number_of_rooms.php form, I get the same form three times for inserting room details in room_details.php , and each form has same name of the fields incremented by the $i parameter in the for loop. For example, with 3 rooms, i have room_number1 for the Real Room Number field in the first form, room_number2 in the second form. room_number3 in the third form, etc. What i'm trying to achieve is to to put all the forms in the room_details.php file in the same form, so the submission button is clicked only once, and submits all the data of each form in the database separately as a new record, bypassing the the need to push the submit button for each room separately. This is because if i have for example 20 or 50 rooms, entering details one by one and pushing the submit button for each room is tedious. For this, i tried with for loop, but with various combination of it i only succeed to insert only the last form data, and NOT all of them as separate record. I know it looks like a big mess, but i'm trying to solve this for 2-3 days and haven't managed to find the appropriate for loop that will insert each form as a separate record with a single click. Does anyone have any idea? Thanks in advance
October 2, 200817 yr just add i to the field name eg: <form> <?php loop{ echo '<span>Room '.$i.'</span>'; echo '<label for="fielda'.$i.'"><input name="fielda'.$i.'" />'; echo '<label for="fieldb'.$i.'"><input name="fieldb'.$i.'" />'; echo '<label for="fieldc'.$i.'"><input name="fieldc'.$i.'" />'; } ?> <input type="submit"/> </form> which will result in: <form> <span>Room 1</span> <label for="fielda1"><input name="fielda1" /> <label for="fieldb1"><input name="fieldb1" /> <label for="fieldc1"><input name="fieldc1" /> <span>Room 2</span> <label for="fielda2"><input name="fielda2" /> <label for="fieldb2"><input name="fieldb2" /> <label for="fieldc2"><input name="fieldc2" /> <span>Room 3</span> <label for="fielda3"><input name="fielda3" /> <label for="fieldb3"><input name="fieldb3" /> <label for="fieldc3"><input name="fieldc3" /> <input type="submit"/> </form>
October 5, 200817 yr Author well i have done that, but the problem is in processing all those forms each one as a separate record with a single submit button. i've tried with two 'for' loops, one for the form, and on e for the query, and first it was working ok, but now seeems that it stops inserting values in the database. i'm getting crazy with this. thanks
Create an account or sign in to comment