Hi
I can't seem to fix this problem whereby my foreach and for loops too much.
Here is the php
<?php
session_start();
$pgn="order";
include("inc/header.php");
?>
<div id="content">
<div style="float: right"> <img src="images/steps/step1.gif" alt="step 1" />
<br />
<img src="images/steps/step2.gif" alt="step 2" /> <br />
<img src="images/steps/step3.gif" alt="step 3" /> </div>
<p class="header">Order</p>
<form method="post" action="inc/order.php" name="orderform" id="orderform">
<div class="form" id="orderformformat">
<?php if($_SESSION['msg'] == "error")
{
echo "<div class=\"error\">Please fill in:<ul>" . $_SESSION['message'] . "</ul></div><br />";
}
?>
<p class="explanation">Please fill in your personal details</p>
<label for="first_name">First name:</label>
<input type="text" id="first_name" name="first_name" value="<?php echo $_SESSION['first_name'];
?>" />
<br />
<label for="last_name">Last name:</label>
<input type="text" id="last_name" name="last_name" value="<?php echo $_SESSION['last_name'];
?>" />
<br />
<label for="cellphone">Cellphone:</label>
<input type="text" id="cellphone" name="cellphone" value="<?php echo $_SESSION['cellphone'];
?>" />
<br />
<label for="street_number">Street number:</label>
<input type="text" id="street_number" name="street_number" value="<?php echo $_SESSION['street_number'];
?>" />
<br />
<label for="street_name">Street name:</label>
<input type="text" id="street_name" name="street_name" value="<?php echo $_SESSION['street_name'];
?>" />
<br />
<label for="street_name">Suburb:</label>
<input type="text" id="suburb" name="suburb" value="<?php echo $_SESSION['suburb'];
?>" />
<br />
<label for="city">City:</label>
<select name="city">
<option <?php if($_SESSION["city"]=="Wellington")
{
echo "selected=\"selected\"";
}
?>>Wellington</option>
<option <?php if($_SESSION["city"]=="Palmerston North")
{
echo "selected=\"selected\"";
}
?>>Palmerston North</option>
</select>
<br />
<label for="city">Email address:</label>
<input type="text" id="email_address" name="email_address" value="<?php echo $_SESSION['email_address'];
?>" />
<br />
<p class="explanation">And now your items...</p>
<ol>
<?php
/* If the form hasn't been submitted, make a default value */
if(!$_SESSION['items_have_been_submitted']) {
$count = 5;
}
/* If it has been submitted, the get the value that was sent and set the count at that */
else {
$count = $_SESSION['count'];
}
/* Let's set the count form variable and list to one higher than the set count */
$count1 = $count + 1;
/* Put our items back into an array */
$itemspre = $_SESSION['items'];
$items = explode("|", $itemspre);
/* Now we are going to loop through the form fields as many times as the count is at */
for($i = 1; $i <= $count; $i++) {
$itemspre = $_SESSION['items'];
$items = explode("|", $itemspre);
/* It's looping the whole thing - should just fill the values */
foreach ($items as $item)
{
$itemsdisplay = "<li><input type=\"text\" id=\"items[]\" name=\"items[]\" value=\"" . $item . "\" /></li>";
echo "$itemsdisplay";
}
}
?>
</ol>
<input type="submit" value="Submit order" id="submit" name="submit" />
</div>
</form>
</div>
<?php include("inc/footer.php");
?>
<?php
/* Start a new session for confirmation and error purposes */
session_start();
/* Set the message back to blank so it can be filled up with fresh values */
$_SESSION['message'] = '';
$_SESSION['msg'] = '';
$location = "http://127.0.0.1/websites";
error_reporting(E_ALL);
/* Get form data */
$first_name = stripslashes(trim($_POST["first_name"]));
$last_name = stripslashes(trim($_POST["last_name"]));
$cellphone = stripslashes(trim($_POST["cellphone"]));
$street_number = stripslashes(trim($_POST["street_number"]));
$street_name = stripslashes(trim($_POST["street_name"]));
$suburb = stripslashes(trim($_POST["suburb"]));
$city = stripslashes(trim($_POST["city"]));
$email_address = stripslashes(trim($_POST["email_address"]));
$itemspre = $_POST['items'];
$items = implode("|", $itemspre);
$count = $_POST['count'];
/* Put that data into session variablese */
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['cellphone'] = $cellphone;
$_SESSION['street_number'] = $street_number;
$_SESSION['street_name'] = $street_name;
$_SESSION['suburb'] = $suburb;
$_SESSION['city'] = $city;
$_SESSION['email_address'] = $email_address;
$_SESSION['count'] = $count;
$_SESSION['items'] = $items;
/* error check */
$error="no";
$message = '';
$okay = preg_match(
'/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]
{
2,4
}
$/',
$email_address
);
if ($okay)
{
$valid = TRUE;
}
else
{
$valid = FALSE;
}
if($first_name=='')
{
$message = "<li>First name</li>";
$error = "yes";
}
if($last_name=='')
{
$message .= "<li>Last name</li>";
$error = "yes";
}
if($cellphone=='')
{
$message .= "<li>Cellphone</li>";
$error = "yes";
}
if($street_number=='')
{
$message .= "<li>"."Street number</li>";
$error = "yes";
}
if($street_name=='')
{
$message .= "<li>Street name</li>";
$error = "yes";
}
if($suburb=='')
{
$message .= "<li>Suburb</li>";
$error = "yes";
}
if($items=='')
{
$message .= "<li>"."Items</li>";
$error = "yes";
}
if($email_address=='')
{
$message .= "<li>Email address</li>";
$error = "yes";
}
elseif(!$valid)
{
$message .= "<li>Valid email address</li>";
$error = "yes";
}
if($itemspre) {
$_SESSION['items_have_been_submitted'] = TRUE;
}
else {
$_SESSION['items_have_been_submitted'] = FALSE;
}
if($error=="yes")
{
$_SESSION['msg'] = "error";
$_SESSION['message'] = $message;
header("Location: $location/order.php");
}
elseif($error=="no")
{
header("Location: $location/confirm.php");
}
?>