I have 12 forms on the same page each form needs to be able to be submitted on there on. When submitted the info should be emailed to me. I'm looping in PHP to create the forms to the products:
<?php foreach ($_productCollection as $_product): ?>
<form action="" method="post">
<p>
<textarea class="finalTextArea" name="message" id="textarea" placeholder="Motivering MAX 50 Tecken"></textarea>
</p>
<p>
<input type="text" class="finalText" name="name" id="textfield" placeholder="För och Efternamn">
</p>
<p>
<input type="text" class="finalText" name="email" id="textfield2" placeholder="E-mail">
</p>
<p>
<input type="submit" class="finalTextSubmit" name="submit" value="Rösta!">
</p>
</form>
and I'm trying to email the form to me using this code:
<?php
if(isset($_POST['submit'])){
$to = "martin@imonline.se"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . " wrote the following:" . "
". $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "
" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
}
?>
It works on the first form, and when that's submitted it won't work at all. Is there a way to do this with AJAX or another solution ?
If you switch the names of all your inputs to end with []
, like for example name="message[]"
then you will be able to have multiple messages submitted. If you choose to do this you will have to have all fields in one single form though. Something similar to the following (although be aware I have not tested this, so it might contain some flaws, it's just to give an alternative way to solve the problem):
<form action="" method="post">
<?php
foreach ($_productCollection as $_product){
?>
<p>
<textarea class="finalTextArea" name="message[]" id="textarea" placeholder="Motivering MAX 50 Tecken"></textarea>
</p>
<p>
<input type="text" class="finalText" name="name[]" id="textfield" placeholder="För och Efternamn">
</p>
<p>
<input type="text" class="finalText" name="email[]" id="textfield2" placeholder="E-mail">
</p>
<?php
}
?>
<p>
<input type="submit" class="finalTextSubmit" name="submit" value="Rösta!">
</p>
</form>
Now all of the post parameters will be stored in arrays, creating multiple of the same value. We then have to loop through each one and send a mail for every element in the array.
<?php
if( $_SERVER['REQUEST_METHOD'] == "POST" ){
$i = 0;
foreach( $_POST['email'] AS $from ){
$to = "martin@imonline.se";
$first_name = $_POST['name'][$i];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . " wrote the following:" . "
". $_POST['message'][$i];
$message2 = "Here is a copy of your message " . $first_name . "
" . $_POST['message'][$i];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
$i++;
}
}