Radio button "yes" if checked, "no" if unchecked - php

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";
}

Related

Randomly receiving empty emails via PHP script [duplicate]

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");
?>

This script show me this message every time "Please fill out all the mandatory fields." [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 3 years ago.
I'm trying everything but this PHP script show this error "Please fill out all the mandatory fields."
Please help me with how to solve this problem.
<?php
session_start();
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['phone'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($mailto, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
echo "Please fill out all the mandatory fields.";
}
} else {
echo "Your enquiry could not be sent for some reason; please try sending us again.";
}
?>
Your if else statements are positioned incorrectly.
They point to wrong conditions.
The rearranged code:
<?php
session_start();
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['phone'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($email, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
// #1: Swipe with #2
echo "Your enquiry could not be sent for some reason; please try sending us again."; // Flip this with #2
}
} else {
echo "Please fill out all the mandatory fields."; // #2
}
?>
Too many nested IF statements is a bad practice because it is difficult to debug. Instead, you should break them to small statements
For example:
<?php
session_start();
if !(isset($_POST['fullname']) || isset($_POST['email']) || isset($_POST['phone'])) {
echo "Your enquiry could not be sent for some reason; please try sending us again.";
exit();
}
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$headers = "MIME-Version: 1.0"."\r\n";
$headers.= "Content-type:text/html;charset=UTF-8"."\r\n";
$headers.= 'From: <'.$email.'>'."\r\n";
$mailto = "google#gmail.com";
$subject = "Web Design & Development Service";
$msg2send = "Hi $fullname,
Hi, we have received one fresh query for you.
Name: $fullname
Email: $email
Phone: $phone ";
$msg2send = nl2br($msg2send);
if (mail($mailto, $subject, $msg2send, $headers)) {
echo "Thanks for writing to us. We will get back to you as soon as possible.";
} else {
echo "Please fill out all the mandatory fields.";
}
?>
Now the code becomes clearer and you can trace back. Seem your code has error at mail function so it return False, then the message shows up.

After submitting form values, data is not appearing in post

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.../

Adding security to PHP mail file

I've created a working contact form with a PHP file. I have read so many post about security, to the point I'm really confused about what code is needed to filter out potential spammers. Code below is what I have so far. Would you be so kind as to provide any code that would secure this php file so I can learn the correct way.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "";
$subject = "Contact Form Enquiry";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
$homepage = file_get_contents('http://www.fashionablefondants.co.uk/response.html');
echo $homepage;
?>

Using php to post checkbox selections in email

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)

Categories