Send cloned files in javascript using php

0

I'm creating an order form. In this form there is a field with the product and the quantity. I'm using a javascript function to clone the field when the client wants one more product, so every time the client clicks on new product creates a new checkbox for it to select the desired product and then fills in the quantity. What I need is to send this data that is generated through this function by php. I tried several things but in no way succeeded. Below are the codes.

JavaScript

 <fieldset id="listproducts">  <legend> PRODUCTS </legend>
        <div id = 'forclone'>
            <select name="product" id="product">
            <optgroup label="GALLON BAG">
                <option>GALLON BAG BANANA SPLIT</option>
                <option>GALLON BAG BIRTHDAY CAKE</option>
                <option>GALLON BAG COTTON CANDY</option>
              </optgroup>
               <optgroup label="PRE-CUP">
                <option>PRE-CUP BANANA SPLIT</option>
                <option>PRE-CUP CHOCOLATE</option>
                <option>PRE-CUP COTTON CANDY</option>
               </optgroup>
                </select>

            <select name="quantity" id="quantity">
                <optgroup label="Qty">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                  </optgroup>
                  </select>

PHP

<?php

if(isset($_POST['email'])) {

$email_to = "[email protected]";

$email_subject = "[ Online Order ]";


function died($error) {

    echo "We are very sorry, but there were error(s) found with the form you submitted. ";

    echo "These errors appear below.<br /><br />";

    echo $error."<br /><br />";

    echo "Please go back and fix these errors.<br /><br />";

    die();

}



if(!isset($_POST['name']) ||

    !isset($_POST['business']) ||

    !isset($_POST['email']) ||

    !isset($_POST['phone']) ||

    !isset($_POST['quantity']) ||

    !isset($_POST['product'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');

}


$name = $_POST['name']; // required

$business = $_POST['business']; // required

$email_from = $_POST['email']; // required

$phone = $_POST['phone']; // not required

$quantity = $_POST['quantity']; // required

$product = $_POST['product']; // not required


$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}


if(strlen($error_message) > 0) {

    died($error_message);

}

$email_message = "<strong>You received an online order. <br>Follow below the informations.</strong><br><br>\n\n";



function clean_string($string) {

    $bad = array("content-type","bcc:","to:","cc:","href");

    return str_replace($bad,"",$string);

}



$email_message .= "<br><strong>Name: </strong>".clean_string($name)."\n";

$email_message .= "<br><strong>Business: </strong>".clean_string($business)."\n";

$email_message .= "<br><strong>Email: </strong>".clean_string($email_from)."\n";

$email_message .= "<br><strong>Phone: </strong>".clean_string($phone)."\n";

$email_message .= "<br><br><strong>ORDER: </strong>\n";

$email_message .= "<br><br><strong>Qty: </strong>".clean_string($quantity)."\n";

$email_message .= "&nbsp;&nbsp;<strong>Product: </strong>".clean_string($product)."\n";


$headers = "Content-Type: text/html; charset= UTF-8 \r\n";
$headers .= 'From: '.$email_from."\r\n".

    'Reply-To: '.$email_from."\r\n" .

    'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

?>


<p>Thank you for order. We will contact you as soon as possible.</p>
<p>You can close this page now.</p>


    <?php

}

?>

HTML

<fieldset id="listproducts">  <legend> PRODUCTS </legend>
        <div id = 'forclone'>
            <select name="product" id="product">
            <optgroup label="GALLON BAG">
                <option>GALLON BAG BANANA SPLIT</option>
                <option>GALLON BAG BIRTHDAY CAKE</option>
                <option>GALLON BAG COTTON CANDY</option>
              </optgroup>
               <optgroup label="PRE-CUP">
                <option>PRE-CUP BANANA SPLIT</option>
                <option>PRE-CUP CHOCOLATE</option>
                <option>PRE-CUP COTTON CANDY</option>
               </optgroup>
                </select>

            <select name="quantity" id="quantity">
                <optgroup label="Qty">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                  </optgroup>
                  </select>
    
asked by anonymous 08.02.2017 / 00:24

2 answers

0

As I understand it, you need to save a list of products, this? If it is, you need to change the name of the elements on the screen, to pass an array by POST.

Where do you have:

<select name="product" id="product">
    ...
    <option>GALLON BAG BANANA SPLIT</option>

You need to swap for:

<select name="product[]">
    ...
    <option value="GALLON BAG BANANA SPLIT">GALLON BAG BANANA SPLIT</option>

With this, in PHP you will receive an array so just use a for ():

<?php
$produto = $_POST["product"];
$qtd = $_POST["quantity"];

for ($i = 0; $i < count($produto); $i++) {
    // para inserir cada produto você usa $produto[$i] e $qtd[$i]
}
    
08.02.2017 / 13:24
0

@ Éderson Fernandes It did not work, did not arrive at the email information. Take a look if I did right ...

HTML

<select name="product[]" id="product">

<select name="quantity[]" id="quantity">

PHP

$name = $_POST['name']; // required

$business = $_POST['business']; // required

$email_from = $_POST['email']; // required

$phone = $_POST['phone']; // not required

$quantity = $_POST['quantity']; // required

$product = $_POST['product']; // not required

for ($i = 0; $i < count($produto); $i++) {
    // para inserir cada produto você usa $produto[$i] e $qtd[$i]
}


$email_message .= "<br><strong>Name: </strong>".clean_string($name)."\n";

$email_message .= "<br><strong>Business: </strong>".clean_string($business)."\n";

$email_message .= "<br><strong>Email: </strong>".clean_string($email_from)."\n";

$email_message .= "<br><strong>Phone: </strong>".clean_string($phone)."\n";

$email_message .= "<br><br><strong>ORDER: </strong>\n";

$email_message .= "<br><br><strong>Qty: </strong>".clean_string($quantity[$i])."\n";

$email_message .= "&nbsp;&nbsp;<strong>Product: </strong>".clean_string($produto[$i])."\n";
    
09.02.2017 / 02:23