How to send multiple checkbox responses from HTML form via php? - php

I have set up a contact form, and set it to email the response to an email account. Part of the form is a series of checkboxes, and I need to get these to display in the email as a list. This is the code I have below, which at the moment returns 'Array' instead of the values of the checkboxes. Any suggestions?
HTML:
<h3>Service required:</h3>
<input type="text" id="name" name="name" placeholder="Name" required />
<input type="email" id="email" name="email" placeholder="Email" required />
<input class="check-box styled" type="checkbox" name="service[]" value="Service / repairs" /><label> Service / repairs</label>
<input class="check-box styled" type="checkbox" name="service[]" value="MOT" /><label> MOT</label>
<input class="check-box styled" type="checkbox" name="service[]" value="Cars for sale" /><label> Cars for sale</label>
Here's the php:
<?php
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
}
$formcontent= "From: $name \n Service(s) required: $service \n";
$recipient = "name#email.com";
$subject = "You have a new message from $name";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You! We will get back to you as soon as we can.";
?>
Thanks,
Jason

You should join (for example implode with ', ') your array elements to a string.
<?php
$formcontent= "From: $name \n Service(s) required: ".implode(", " ,$service)." \n";
?>

Why don't you loop through the array to get the desired results into a String?
if (isset($_POST['service'])) {
$service = $_POST['service'];
// $service is an array of selected values
$service_string = "";
for($i=0;$i<count($service);$i++)
{
if($i!=0)
{
$service_string = $service_string . ", ";
}
$service_string = $service_string . $service[$i];
}
}
You will then get an output of a comma separated list of each ticked item as $service_string.

Since several checkboxes are stored in $_POST['service'], it is an array itself and has become two-dimensional. Its different indexes are accessible like this: $_POST['service'][0].
To do something with $_POST['service'], you can use foreach to loop through all indexes:
foreach($_POST['service'] as $post){
//Do stuff here
}
Alternatively, use implode() to simply concatenate all indexes.

Your input type checkbix must have unique names. Otherwise last checkbox will be found in $_POST. Or you can loop through as discuss above. Make your email html format and write a string of html to $formcontent. e.g.
$formcontent = "<html><head></head><body>";
$formcontent .= "<ul><li>".$_POST["checkbox1"]."</li>";
$formcontent .= "<li>".$_POST["checkbox2"]."</li>";
$formcontent .= "</ul></body></html>";
To write email in html format see mail function on php website.

Related

PHP question. I get email, but it doesn't have user input from website

The PHP code sends me an email from the server, but the user input is empty or "blank". I only receive: "From: \ Email: \ Subject: \ Message: " and that's it.
How do I fix my PHP and/or HTML code to receive user input from the form? Here is my existing HTML and PHP code that doesn't send me any "user input" from the form.
PHP
HTML
Thanks to anyone who can help!!
Consider this is your form
<form action="yourpage.php" method="post">
<input type="text" name="name" placeholder="enter name" required>
<input type="submit" name="submit" placeholder="enter name">
Make sure that <form action="" is linking to correct page and method="post" should be there.
If form is there on current page i.e. in my case its yourpage.php then leave form action blank. Also i recommend adding required in end of input field e.g.
<input type="text" name="name" placeholder="enter your name" required>
as it forces user to write some value there so you dont get a empty value
Next will be your php file, just add a code in first line
if(isset($_POST['submit'])) {
above code verifies that user clicks on submit button so overall code will look like
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phoneT'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST[itypel];
$message = $_POSTUmessagefl;
$formcontent = " From:$name \n Phone: $phone \n CallBack: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "YOUREMAIL#HERE.COM";
$subject - "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href=iform.htmll
style='text-decoration:none;color:#ff0099;'> Return
Home</a>";
}
?>

Get value of multiple checkbox & echo in email

Getting trouble on adding multiple checkbox value within mail body due to my low knowledge in PHP.
I know that it possible to show/echo checkbox by array with foreach loop but i don't know to echo it within mail body. I want to echo it within $message.
HTML Code sample-
<input type="checkbox" name="color[]" value="Value1"> Title1
<input type="checkbox" name="color[]" value="Value2"> Title2
<input type="checkbox" name="color[]" value="Value3"> Title3
<input type="checkbox" name="color[]" value="Value4"> Title4
<input type="checkbox" name="color[]" value="Value5"> Title5
PHP Code-
<?php
$to = "arifkpi#gmail.com";
$fromEmail = "arif#arif-khan.net";
$fromName = "Arif Khan";
$subject = "Contact Email";
$message = "Hey, Someone Sent you a Contact Message through your Website.
Details Below-
Name: $_POST[fname] $_POST[lname]
Email Address: $_POST[email]
Contact Number: $_POST[contact1] $_POST[contact2] $_POST[contact3]
Zip Code: $_POST[zip]
Best Time To Contact: $_POST[besttime]
Payment Plan Options: $_POST[payment_plan]
MUA: $_POST[mua]
Shoot Concept:
Shoot Concept(Other): $_POST[shootother]";
$headers = "From:" . $fromName . " " . $fromEmail;
$flgchk = mail ("$to", "$subject", "$message", "$headers");
if($flgchk){
echo "A email has been sent to: $to";
}
else{
echo "Error in Email sending";
}
?>
You can simply do,
$colors = isset($_POST['color']) ? implode(",",$_POST['color']) : '';
And now you can use this $colors (you will get all selected colors as comma separeted) in your email message body part.
$message = "Hey, Someone Sent you a Contact Message through your Website.
Details Below-
Name: $_POST[fname] $_POST[lname]
Colors: $colors
Email Address: $_POST[email]
Contact Number: $_POST[contact1] $_POST[contact2] $_POST[contact3]
Zip Code: $_POST[zip]
Best Time To Contact: $_POST[besttime]
Payment Plan Options: $_POST[payment_plan]
MUA: $_POST[mua]
Shoot Concept:
Shoot Concept(Other): $_POST[shootother]";
Try this
if(isset($_POST['color'])) {
$message.= implode(',', $_POST['color']);
}

php radio button email

I am trying to make a form to ask people if they are attending an event. I have searched multiple threads and forums. I also resorted to google and read through a few tutorials and I am unable to correlate the correct answers to what I need. I am trying to figure out how to send an e-mail to with a message based on the radio button selected. Please help me if you can. It is greatly appreciated.
<FORM method="post" name="RSVPform" action="respond.php" target="_blank">
Name(s): <input name="name" size="42" type="text" /><br/><br/>
Will you be attending the event?<br/><br/>
<input checked="checked" name="answer" type="radio" value="true" /> Yes, I(we) will attend.<br/><br/>
If yes, how many will be attending? <input name="number" size="25" type="text" /><br/><br/>
<input name="answer" type="radio" value="false"/>Sadly, we are unable to attend. <br/><br/> <input name="Submit" type="submit" value="Submit" />
</FORM>
This is the php I've been trying to use.
<?php
$to = "myemail#email.com";
$name = $_REQUEST['name'] ;
$answer = $_REQUEST['answer'] ;
$subject = "RSVP from: $name";
$number = $_REQUEST['number'] ;
$headers = "RSVP";
$body = "From: $name, \n\n I(we) will be attending the event. There will be $number of us. \n\n Thanks for the invite.";
$sent = mail($to, $subject, $body, $headers) ;
if($sent)
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
else
{echo "<script language=javascript>window.location = 'LINK BACK TO CONTACT PAGE';</script>";}
?>
I'm not sure how to change the $body message depending on the radio button selected. Is this possible?
You need a condition in the PHP, but note that in your HTML "true" and "false" will be sent as strings not booleans, so are both truthy, but you can check the actual string.
Anywhere after $answer = $_REQUEST['answer'] ; append/modify/write your email body, e.g.
if ($answer=='true') {
$body='Yay you\'re coming';
}else{
$body='Ah screw you then';
}
Here's the absolute basic of what you'd need. Just swap your variables based on the posted answer.
if($_POST['answer'] == "true")
{
// user is coming
}
else
{
// user is not coming
}

PHP Array in email

Im trying to write a contact form and so far everything works only theres a multiple checkbox in the form and im unsure how to call all data and so my email returns 'array' for the variable 'service'
My code is...
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$service = $_POST['service'];
$description = $_POST['description'];
$location = $_POST['location'];
$to = "liam#.co.uk";
//begin of HTML message
$message = "
From : $name,
Email: $email,
Number: $number,
Service: $service,
Description: $description
Location: $location
";
//end of message
$headers = "MIME-Version: 1.0rn";
$headers .= "Content-type: text/html; charset=iso-8859-1rn";
$headers .= "From: $email\r\n";
mail($to, $subject, $message, $headers);
?>
My form for the service input is...
<dt><input type="checkbox" value="Static guarding" name="service[]" class="cbox">Static guarding</dt>
<dt><input type="checkbox" value="Mobile Patrols" name="service[]"class="cbox">Mobile Patrols</dt>
<dt><input type="checkbox" value="Alarm response escorting" name="service[]"class="cbox">Alarm response escorting</dt>
<dt><input type="checkbox" value="Alarm response/ Keyholding" name="service[]"class="cbox">Alarm response/ Keyholding</dt>
<dt><input type="checkbox" value="Other" name="service[]"class="cbox">Other</dt>
<dt><input type="hidden" value="Other" name="service[]"class="cbox"></dt>
You can process the checkbox values into a string with implode():
$checkboxes = implode(',', $_POST['service']);
$message = <<<EOL
...
Service: $checkboxes
EOL;
Note the use of a heredoc - it's a MUCH nicer way of defining a multi-line string than using a regular quoted string.
Because you appended your form input names with brackets, PHP will form an indexed array out of them.
$sCount = count($service);
for ($s=0; $s<$sCount; $s++) {
echo $service[$s];
}
You might be better off creating an associative array:
<dt><input type="checkbox" value="Mobile Patrols" name="service[mobile]"class="cbox">Mobile Patrols</dt>
<dt><input type="checkbox" value="Alarm response escorting" name="service[escorting]"class="cbox">Alarm response escorting</dt>
That way, in your script, you could access them as:
echo $service['mobile'];
echo $service['escorting'];
Note that empty checkboxes aren't submitted so you'll want to first check if the array element is set.

How to send form data to multiple email addresses using PHP?

I am a web designer, and dont really know much about PHP. I have a form, and I want the values to be sent to three email addresses.
Here is my HTML:
<form id="player" method="post" action="process.php">
<label for="name">Your Name</label>
<input type="text" name="name" title="Enter your name" class="required">
<label for="phone">Daytime Phone</label>
<input type="tel" name="phone" class="required">
<label for="email">Email</label>
<input type="email" name="email" title="Enter your e-mail address" class="required email">
<input type="submit" name="submit" class="button" id="submit" value="I'd like to join Now" />
</form>
I have somehow found a PHP code that should send the data to ONE email address only, but I dont even know if it works or not.
Here is the code for that:
<?php
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);
// Send Message
mail( "you#youremail.com", "Contact Form Submission",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
"From: Forms <forms#example.net>" );
?>
Thanks
Add headers
<?php
// Get Data
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$url = strip_tags($_POST['url']);
$message = strip_tags($_POST['message']);
$headers .="From: Forms <forms#example.net>";
$headers .="CC: Mail1 <Mail1#example.net>";
$headers .=", Mail2 <Mail2#example.net>";
// Send Message
mail( "you#youremail.com", "Contact Form Submission",
"Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $url\nMessage: $message\n",
$headers );
?>
You should be able to separate email addresses with commas in the first parameter of the mail() function, i.e.
mail('email1#example.com, email2#example.com', $subject, $message, $headers);
Or sepcific CC and optionally BCC addresses as per Ahmad's answer.
The mail function (which is used in the code that you posted) allows you to specify multiple recipients. See the PHP documentation of that function for details: http://php.net/manual/en/function.mail.php
Edit: You basically need to replace the "you#youremail.com" part with a list of addresses, separated by commas, e.g.:
mail("you#youremail.com,somebody#domain.com,anotherone#domain.com", ...
use
$to = "email#email.com"
$to .= ", anotheremail#email.com";
this will help you to create multiple recipient.

Categories