Jump to content

processing/looping through multiple-similar POST data--CONFUSED?!?!


barbs75

Recommended Posts

Hey guys,

 

I'm having a little problem with my form...

 

What i have is some javascript which allows a user to click to 'add a room' onto my submit page which creates this HTML for each added room:

<div class="inputfield">
                        <p class="largeblue"><strong>Bedroom_1</strong></p>
                            <div class="dimensions">
                            	<label class="room" for="bedroom_width_1">Dimensions:</label>
                            	<input class="fade" name="bedroom_width_1" id="bedroom_width_1" type="text" size="5" value="" /> x <input class="fade" name="bedroom_length_1" id="bedroom_length_1" type="text" size="5" value="" />
                            </div>
                            <div class="descriptionHolder">
                            	<label class="room" for="bedroom_desc_1">Description:</label>
                            	<textarea name="bedroom_desc_1" id="bedroom_desc_1" cols="35" rows="5">Enter your description here.....
                            	</textarea>
                            </div>	
                       </div>

 

When they add more rooms, the number at the end for the 'id' values for each input/textarea, are incremented, so if the user added 2 rooms, the HTML for the input fields for 'bedroom_width' would be as follows:

  • <input class="fade" name="bedroom_width_1" id="bedroom_width_1" type="text" size="5" value="" /> x <input class="fade" name="bedroom_length_1" id="bedroom_length_1" type="text" size="5" value="" />
     
  • <input class="fade" name="bedroom_width_2" id="bedroom_width_2" type="text" size="5" value="" /> x <input class="fade" name="bedroom_length_2" id="bedroom_length_2" type="text" size="5" value="" />

 

Now, what i am struggling with is how to process this data in these post data to then validate, and enter into my database.

 

I dont really know how i would do this?? i have tried doing this using an array to store the widths, heights and descriptions for each room.

 

Here is the code i came up with:

$bedroom_width = array();
$bedroom_length = array();
$bedroom_desc = array();
//load in bedrooms information
for($i=1;$i<=$no_of_bed;$i++){
$bedroom_width[$i] = $_POST['bedroom_width_'.$i];
$bedroom_length[$i] = $_POST['bedroom_length_'.$i];
$bedroom_desc[$i] = $_POST['bedroom_desc_'.$i];
}

 

but when i use an echo to see the value of one of the array elements, there is nothing there!

The technique that i have used for referencing to the post data, i have seen used for a SESSION variable, but it is not working here!!

 

any idea how i would go about doing this? or is there something wrong with my code?? i really can't find any examples of this kind of thing on the net, but surely there must be a simple way of doing this??

 

cheers guys

 

Craig

Hey littledragon,

 

the variable $no_of_bed is stored earlier on in my form processing script, which is taken from POST data from my form,

 

here's the script for the processing page:

<?php
//turn on sessions
session_start();
header("Cache-control: private"); 
//checking if user is logged in, if not send them to login screen
if (!$_SESSION['username']) 
{ 
include("login_form.php"); 
exit(); 
}
//include database connections and email functions
include('database.php');
include('email_fns.php');

//input username session info into variable
$username = $_SESSION['username'];

$detailserrorcount = 0;
$detailserrorarray = array();
$detailsdataarray = array();

//load data from SESSION data
$username = $_SESSION['username'];
$user_id  = $_SESSION['user_id'];

//load posted data from form into local variables
//load in house general info
$property_type = $_POST['propertyType'];
$status        = $_POST['status'];
$price         = $_POST['price'];
$no_of_bed     = $_POST['noofbed'];
$no_of_bath    = $_POST['noofbath'];

//laod in default room information
//living room
$livingroom_width  = $_POST['livingroom_width'];
$livingroom_length = $_POST['livingroom_length'];
$livingroom_desc   = $_POST['livingroom_desc'];
//dining room
$diningroom_width  = $_POST['diningroom_width'];
$diningroom_length = $_POST['diningroom_length'];
$diningroom_desc   = $_POST['diningroom_desc'];

$bedroom_width = array();
$bedroom_length = array();
$bedroom_desc = array();
//load in bedrooms information
for($i=1;$i<=$no_of_bed;$i++){
$bedroom_width[$i] = $_POST['bedroom_width_'.$i];
$bedroom_length[$i] = $_POST['bedroom_length_'.$i];
$bedroom_desc[$i] = $_POST['bedroom_desc_'.$i];
}

echo 'the value of bedroom_width_1 variable is'. $bedroom_width_1;

?>

 

cheers

 

Craig

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.