I am handling two forms in the same php script. When the first form is submitted it prompts the opening of the second form which when submitted is sent to the same php script as the first form. I have tried using if(isset($_POST['details_submit']) && isset($_POST['customer_submit'])
to handle both forms at the same time when both are submitted but this doesn't work. I would like to handle data from both forms at the same time since data from the first form is required together with data from the second form to send to mail. Here is my code:
<?php
if(isset($_POST['details_submit'])){
$body = json_decode(file_get_contents('php://input'),true);
$GLOBALS['details'] = "";
foreach ($body as $key => $value) {
$details.= "Item : ".$value['item_name'];
$details.= ", Quantity : ".$value['quantity'];
$details.= ", Amount : ".$value['amount'];
$details.= ", Total : ".$value['total']."\n";
}
echo $details;
$GLOBALS['subtotal'] = 0;
foreach ($body as $key => $value) {
$subtotal = $value['total'] + $subtotal;
}
echo $subtotal;
}// end if statement
if(isset($_POST['customer_submit'])){
$customer = "";
$customer.= "Customer : ".$_POST['Name']."\n";
$customer.= "Email : ".$_POST['Email']."\n";
$customer.= "Phone Number : ".$_POST['Phone']."\n";
$customer.= "Residence : ".$_POST['Area'];
echo $customer;
$email = $_POST['Email'];
$to = 'example@gmail.com';
$subject = 'Natures Touch Order';
$message = "<b>Customer Order</b> \n"
.$details."\n
<b>Customer Details</b> \n"
.$customer."\n
The subtotal is KSH ".$subtotal.".";
$headers = 'From: '.$email."\r\n";
'X-Mailer: PHP/' . phpversion();
$send1 = mail($to, $subject, $message, $headers);
}
?>
Everything works fine but when I it comes to sending the mail I get the error Undefined variables details and subtotal
which is probably because the variables details
and subtotal
are handled in the first form. Any solution on how to handle this will be highly appreciated.