Okay so I've a contact form I want people to select multiple items, once they submit the contact form I want it to send an email using a html template. I've set it up to string replace the data in the html template, but every time I try to do the array it ether says array or only shows one of the multiple items that were selected.
This is the HTML Select Code I made sure to add [] to make the name into an array.
<select class="js-example-multiple js-states form-control" multiple="multiple" name="product[]">
<option value="Baby Shark Castle 15ft x 18ft">Baby Shark Castle 15ft x 18ft</option>
<option value="Pirate's assault course 12ft x 25ft">Pirate's assault course 12ft x 25ft</option>
<option value="Yellow Mega Slide 18ftx18ft">Yellow Mega Slide 18ftx18ft</option>
<option value="18ft x 18ft Disco Dome Lights & Speaker">18ft x 18ft Disco Dome Lights & Speaker</option>
<option value="Assault Course 35ft Long 12 ft Wide">Assault Course 35ft Long 12 ft Wide</option>
<option value="Inflatable Nightclub 12ft x 15ft">Inflatable Nightclub 12ft x 15ft</option>
<option value="40ft Assault course 15ft x 40ft">40ft Assault course 15ft x 40ft</option>
<option value="Inflatable Pub 17x17 - Holds 20 People">Inflatable Pub 17x17 - Holds 20 People</option>
</select>
This is the php code, I've been successful in replacing the other individual values but when I try to replace one with multiple values it only shows one value. I tried a foreach loop but this only works when I echo the value $product. I want to string all the items selected not just one, I have an example of what I want it to look like below.
// Bring in the email template here
$html = file_get_contents('template.html');
// You only need to modify the following three lines of code to customise your form to mail script.
$email_to = "[email protected]"; // Specify the email address you want to send the mail to.
$email_from = "[email protected]"; //Specify the email address that you want to send the email from. This needs to be Fasthosts hosted,
$email_subject = "Website Contact Form"; // Set the subject of your email.
// Specify a page on your website to display a thankyou message when the mail is sent
$thankyou_url = "../thankyou.html";
// Get the details the user entered into the form
$name = $_POST["name"];
$reply_to = $_POST["email"];
$number = $_POST["number"];
$date = $_POST["date"];
$message = $_POST["message"];
$products = $_POST["product"];
// Validate the email address entered by the user
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
// Replacing the details in the template the above variables
$html = str_replace("{{username}}",$name,$html);
$html = str_replace("{{email}}",$reply_to,$html);
$html = str_replace("{{number}}",$number,$html);
$html = str_replace("{{date}}",$date,$html);
$html = str_replace("{{message}}",$message,$html);
foreach($products as $product){
$list = $product . "<br> test <br>";
$html = str_replace("{{list}}",$list,$html);
};
This is the html template code
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px;">
{{list}}
</p>
</div>
This is the result I'm getting
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px;">
Pirate's assault course 12ft x 25ft
</p>
</div>
The end result I want is when people select any number of items for it to all appear in the one place like this instead of above
<div style="margin: 0px; padding: 0px;">
<p style="margin: 0px; padding: 0px;">
Baby Shark Castle 15ft x 18ft
Assault Course 35ft Long 12 ft Wide
Pirate's assault course 12ft x 25ft
and so on
</p>
</div>
CodePudding user response:
You need implode
Replace
foreach($products as $product){
$list = $product . "<br> test <br>";
$html = str_replace("{{list}}",$list,$html);
};
with
$list = implode("<br>",$products);
$html = str_replace("{{list}}",$list,$html);
CodePudding user response:
When you do this:
foreach($products as $product){
$list = $product . "<br> test <br>";
$html = str_replace("{{list}}",$list,$html);
};
You are actually doing the replace n times, but only the first one will work, because afterwards there is no more "{{list}}" to be replaced. That is not an error, just not what you expect.
Try it like this:
$html = str_replace("{{message}}",$message,$html);
$list = '';
foreach($products as $product){
$list .= $product . "<br>";
};
$html = str_replace("{{list}}",$list,$html);
Now you are building a temporary String variable with all selected products and then you only replace it once.