0

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.

lorrainemutheu
  • 115
  • 1
  • 17
  • Why are you handling two forms? For most of the cases, you can use javascript to create forms step by step (tabs). – Gabriel Heming Aug 09 '17 at 20:40
  • Am handling two forms because data from both is required to be sent as one mail. Please elaborate further about how I can apply tabs. And just a reminder the sending of the first form prompts the opening of the second form – lorrainemutheu Aug 09 '17 at 20:47
  • The concept of tabs is to show and hide parts of the same form when needed. Then, send all data together. Take a look at (there's a fiddle example also): https://stackoverflow.com/questions/25352468/tabbed-forms-using-html-css-js – Gabriel Heming Aug 09 '17 at 20:49
  • Use AJAX and send them in succession. After the first is done, then send the second. – Difster Aug 09 '17 at 20:49
  • @Difster please could you give an example code on how to do this – lorrainemutheu Aug 10 '17 at 10:17
  • There are tons of tutorials on how to do ajax. – Difster Aug 10 '17 at 15:09

0 Answers0