I'm brand new to php so struggling to figure this one out from existing answers.
I need to see which of up to 4 checkboxes have been selected on a form in the resulting notification email.
The form IS sending the email, but it only includes the sender's comments, NOT the checkbox selection(s) made.
Anyone willing to point out the error in my code? Please go ahead and assume the lowest comprehension level of n00bness.
Here's the relevant form html:
<input type="checkbox" name="timeslots[]" value="thu" />Thursday after 7pm <br/>
<input type="checkbox" name="timeslots[]" value="fri" />Friday after 5.30pm <br/>
<input type="checkbox" name="timeslots[]" value="sat" />Saturday afternoon<br/>
<input type="checkbox" name="timeslots[]" value="sun" />Sunday afternoon<br/>
..and here's the php script I've cobbled together so far:
<?php
$email_to = "me#mysite.com";
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];
$email_from = $_POST["email"];
$email_subject = "Form request";
$times = $_REQUEST["timeslots"];
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
$headers =
"From: $email_from .\n";
"Reply-To: $email_from .\n";
$body = "Name: $name\n Message: $comments\n
$times";
ini_set("sendmail_from",$email_from);
$sent=mail($email_to,$email_subject,$comments,$headers,"-f".$email_from);
if($sent)
{
header("Location:thanks.html");
}else{
header("Location:senderror.html");
}
?>
The problem is that $times is an array. you should do:
$times = $_POST["timeslots"];
$times = implode(', ', $times);
and then you can use it in your email
$times is an array because in PHP when you declare input elements with an array as name (like you did), an array is posted. In your case only the selected checkboxes will be posted.
One more thing: only the value of the checkbox will be posted, so if you check the first two checkboxes you will send "thu, fri" in the mail.
$times is array in your code:
<?php
$email_to = "me#mysite.com";
$name = $_POST["name"];
$email = $_POST["email"];
$comments = $_POST["comments"];
$email_from = $_POST["email"];
$email_subject = "Form request";
$times = $_POST["timeslots"];
if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
// Invalid email address
die("The email address entered is invalid.");
}
$strTimes = implode(", ", $times);
$headers[] = "From: $email_from .\n";
$headers[] = "Reply-To: $email_from .\n";
$body = "Name: $name\n Message: $comments\n $strTimes";
ini_set("sendmail_from",$email_from);
$sent=mail($email_to,$email_subject,$comments,$headers,"-f".$email_from);
if($sent)
{
header("Location:thanks.html");
}else{
header("Location:senderror.html");
}
?>
Edited: in your original code line 17 & 18 should be arrays, (line 18 in your original code is unused)
Related
This question already has answers here:
Relying on HTML 'required' for simple form validation
(4 answers)
Closed 1 year ago.
I am a beginner helping my aunt build a personal website. I have an HTML form where users can contact her (slightly simplified for clarity):
<form id = "contact-form" method = "post" action = "contact-form-handler.php">
<input name = "name" type = "text" required><br>
<input name = "email" type = "email" required><br>
<textarea name = "message" class = "form-control" required></textarea><br>
<input type = "submit" class = "form-control submit" value = "SEND MESSAGE">
</form>
The script contact-form-handler.php reads,
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$email_from = "SEND_EMAIL#ADDRESS.com";
$email_subject = "Message from fziastories";
$email_body = "Name: $name.\n".
"Email: $visitor_email.\n".
"Message: $message.\n";
$to = "RECIEVE_EMAIL#ADDRESS.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject, $email_body, $headers);
header("Location: index.html");
?>
This form works great. When I enter various tests, the message goes through perfectly. And when I don't enter a value for one of the three inputs, I am not able to hit the 'SEND MESSAGE' button.
Except, every once in a while, maybe once or twice a week, I receive an empty message with no values filled out. This is confusing to me because I figure the current setup precludes users from submitting the form without entering value but also through the tests I have ruled out the possibility that a valid response is being entered as blanks.
I would greatly appreciate any advice! If you have any follow-up questions, do not hesitate to ask. Thank you!
The required tag in HTML forms is not very secure!
You should always validate the data on your server.
This could look something like this:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['message'])) {
header("Location: index.html");
exit;
}
$email_from = "SEND_EMAIL#ADDRESS.com";
$email_subject = "Message from fziastories";
$email_body = "Name: $name.\n".
"Email: $visitor_email.\n".
"Message: $message.\n";
$to = "RECIEVE_EMAIL#ADDRESS.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject, $email_body, $headers);
header("Location: index.html");
?>
i am struggling to pull through a "Date" input type on my php form processor to email.
I've tried various coding but cannot get the value to show on email :-(
everything else works - assistance would be greatly appreciated!
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$reference = $_POST['reference'];
$date = $_POST['date'];
$amount = $_POST['amount'];
$email_from = 'my email here';
$email_subject = "Online Payment Notification";
$email_body = "You have received an online payment notification from $name.\n".
"please see details below:\n
Name: $name
Email: $visitor_email
Reference#: $reference
Date: $date
Amount: £$amount
";
$to = "my email here";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
[The] Date is showing as YYYY-MM-DD but i want it to show as
DD-MM-YYYY
To change the format of the date from YYYY-MM-DD to DD-MM-YYYY, simply replace this line of code:
$date = $_POST['date'];
with:
$date = date("d-m-Y", strtotime($_POST['date']));
While I am sending the email on the pop up of submit button I used if(isset($_POST['submit'])) but it does not takes the value.
On submit button so tell me what is the problem in this code it will not accept submit as isset post.
In the if function the of isset the submit button not inputting the values also explain if there is jquery issue
<?php
//print_r($_POST);die();
ini_set('display_errors', 'On');
if(isset($_POST['submit'])){
$to = "basavraj.p#wepearl.in"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = trim($_POST['fname']);
/*$last_name = $_POST['last_name'];*/
$email = trim($_POST['email']);
$phone_number = trim($_POST['phone-number']);
$subject = trim($_POST['subject']);
$description = trim($_POST['description']);
$quote_sub = trim($_POST['quote-sub']);
$message = "First Name: ".$first_name."\r\n Email: ".$email." \r\n Phone Number: ".$phone_number." \r\n Subject: ".$subject." \r\n Description: ".$description." " ;
$headers = "From: kashmira.s#wepearl.in" . "\r\n" .
"CC: pooja.s#wepearl.in";
/*$headers2 = "From:" . $to;*/
if(mail($to,$quote_sub,$message,$headers))
{
echo "success";
}
else
{
echo "failed";
}
}
?>
I think your query
if(isset($_POST['submit'])){
fails because you might not have added name="submit" in <input type="submit" /> statement or you may have someother name.
So, add
<input type="submit" name="submit" value="Go!" />
in your html page. In php, just check whether form has been submitted or not by,
if(isset($_POST['submit'])){
To check, just echo the submitted data by,
if(isset($_POST['submit']))
{
$from = $_POST['email'];
echo "From email : " . $from;
/ ..add rest here like name.../
Good afternoon, I having difficulty with the radio button on a form I have created. I found a similar problem on here, but with having limited php coding experience, I was unable to correctly code it or my form. Any help would be greatly appreciated.
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$procedure = $_POST['procedure'];
$date = $_POST['date'];
$message = $_POST['message'];
$policybox = null;
foreach($_POST['policy'] as $policy){
if(isset($policy)){
$policybox .= "Yes\r\n";
} else{
$policybox .= "No\r\n";
}
}
$formcontent="From: $firstname $lastname \nEmail: $email \nPhone: $phone \nType of Procedure: $procedure \nDate Requested: $date \nI have read and understood the policies: $policybox \nMessage: $message";
$recipient = "sales#domainname.com";
$subject = "Appointment Form from DomainName.com";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: appointments.php');
?>
The field Im trying to get to show up is:
<div class="form_info cmsms_radio">
<div class="check_parent">
<input type="radio" name="policy" value="Yes" />
<label for="policy">I have read and understand the Refund Policy and Cancellation Policy</label>
</div>
Im sure its something small Im missing, but like I said Im not very experienced with php yet.
Your $_POST['policy'] variable isn't going to be an array (based on your code), therefore you do not need to iterate over it. You can simply test the form value itself.
Replace your foreach with the following:
if(isset($_POST['policy']) && $_POST['policy'] == "Yes") {
$policybox .= "Yes\r\n";
} else{
$policybox .= "No\r\n";
}
I have a form in my webpage that sends data submitted by the user to an email address. i.e. a contact form. I know how to open a new page to inform the user that the data has been fired off to the email address. However I do not want to open a new page, I just want to inject a simple Email Sent! text below the actual form for the user; instead of redirecting to a new page.
I have absolutely no idea how to achieve this in PHP. I have all the basics of the process script set up here;
<?php
/*--/ subject & email variables /--*/
$emailSubject = 'Email Title';
$webMaster = 'php.test#outlook.com';
/*--/ gathering data variables /--*/
$firstField = $_POST['first_Name'];
$lastField = $_POST['last_Name'];
$emailField = $_POST['email'];
$telField = $_POST['phone'];
$selectField = $_POST['select'];
$enquiryField = $_POST['enquiry'];
$body = <<<EOD
<br><hr><br>
First Name: $first_Name <br>
Last Name: $last_Name <br>
Email: $email <br>
Tel: $phone <br>
I heard about you from; $select <br>
Enquiry: $enquiry <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster,$emailSubject, $body, $headers);
?>
If anyone can help I would be thankful.