简体   繁体   中英

PHP send email using FOREACH div on the page?

I am doing a kind of contact form where the user can add as many time as he want a div that contains 3 things: 1 select box, and 2 text boxes.

I would like to know how its possible to select all boxes and add it into a nice PHP email function to send it clean like this:

Select1
Textbox1
Textbox1-1

select2
textbox2
textbox2-2

For more information on how I proceed, look below:

I use Jquery to clone my current box that contains the 3 elements, using this line:

$('#element_1').clone().attr('id', 'element_' + ++counter).appendTo('.container');

And then I change every element inside this box with jQuery again with the .attr() function. Every element has its own ID, following an easy pattern of +1 to everyone of them.

What's the real problem? Just be sure that all the div blocks are cloned into <form></form> tag and they will proceed to server side on form submit. If you are struggling with a question how to process those input fields, then use [] brackets for inputs name, so they will come to your script as an array and you can loop through them. ( example: input name="name[]" )

Don't add the counter. That makes it difficult to access in php. Use an array for your input's name instead.

Example:

<select name="selectbox[]">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<input name="textbox[]" />
<input name="textbox2[]" />

<select name="selectbox[]">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<input name="textbox[]" />
<input name="textbox2[]" />

php:

foreach( $_POST['textbox'] as $i => $textbox ) {
    // use $i as the key to get other inputs

    $textbox2 = $_POST['textbox2'][$i];
    $select = $_POST['selectbox'][$i];

    // 1 entire set of inputs
    echo $select;
    echo "<br />";
    echo $textbox;
    echo "<br />";
    echo $textbox2;
    echo "<br /><br />";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM