I am new to php so here goes...
I am trying to output all items ordered from my SESSION variables into input values with a function so that I can process them with a form submit. Now the input fields are created successfully and all ok. here is the code
function list_cart_items() {
$num =0;
$total=0;
foreach($_SESSION as $name => $value) {
if ($value!=0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$iget = mysql_query('SELECT id,name,price FROM items WHERE id='.mysql_real_escape_string((int)$id));
while ($iget_row = mysql_fetch_array($iget)) {
$num ++;
$total += number_format(($iget_row['price']*$value),2);
echo "<tr>
<td><input type='text' name='order_item".$num."' value='".$num."' size='2' maxlength='2'>
<td><input type='text' name='order_qty_item".$num."' value='".number_format($value,1)."' size='3' maxlength='3'>
<td><input type='text' name='order_name_item".$num."' value='".$iget_row['name']."' size='30' maxlength='30'>
<td><input type='text' name='order_value_item".$num."' value='R ".number_format(($iget_row['price']*$value), 2)."' size='6' maxlength='6'>
</tr>";
}
}
}
}
echo "<tr><td colspan='2'><b>Order Total</b></td><td></td><td colspan='2'><b>R ".$total."</b></td></tr>";
Here is the code of the actual form creation with submit button
echo "<div id='shoppingcart'><form action='' method='POST'>";
echo "<table class='checkout'><caption><b>Shopping Cart</b></caption>";
**list_cart_items();**
echo "<div id='information'>";
echo "<tr><th colspan='2'>Name and Surname:</th><td colspan='3'><input type='text' size='45' maxlength='50' name='Contact' id='Contact'></td><tr>";
echo "<tr><th colspan='2'>Telephone number:</th><td colspan='3'><input type='text' size='15' maxlength='12' name='Telephone' id='Telephone'></td><tr>";
echo "<tr><th colspan='2'>Delivery Address:</th><td colspan='3'><textarea rows='8' cols='45' id='address' name='address'>Enter Address Here:</textarea></td><tr>";
echo "<tr><th colspan='2'>Instructions:</th><td colspan='3'><textarea rows='8' cols='45' id='instructions' name='instructions'></textarea></td><tr>";
echo "<tr><td colspan='5' align='center'><button type='submit' name='submit' value='submit' style='border: 0; background: transparent'>
<img src='images/buynow.jpg' class='button' alt='submit'/>
</button></td><tr>";
echo "</form></div></table></div>";
Now comes the million dollor question , since I do not know how many products have been selected and ordered , I do not know what the maximum value is for my SESSION['cart_X']; thus I do not know how many input fields have been created on my form.
What I am trying to do is to process each value in my POST array successfully and output that into a $emailbody (Order needs to be processed and emailed)
I have tried playing around with the foreach ($_POST as $entry){ print $entry."
";}
but no success
here is the code ..
if (isset($_POST['submit'])){
$orderno = session_id();
$contact = $_POST['Contact'];
$telephone = $_POST['Telephone'];
$address = $_POST['address'];
$instructions = $_POST['instructions'];
//Here I need to catch every POST variable (all items given as input fields in my form) values
$to = "orders@t.co.za";
$subject = "Order received via website";
$headers = 'MIME-Version: 1.0' . "
";
$headers .= 'Content-type: text/html; charset=iso-8859-1'."
";
$headers .= 'To: Johan <schoeman.jj@gmail.com>'."
";
$headers .= 'From: Order via Web <orders@triumphant.co.za>'."
";
$emailbody = "Order number :".$orderno."<br>
Contact Person: ".$contact."<br>
Telephone number: ".$telephone."<br>
Delivery Address: ".$address."<br>
Instructions: ".$instructions."<br>
//Here I need to include every POST order item