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.../
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 have created my page and have incorporated a form. I believe I have set everything up correctly, however when I receive the email, none of the input data is shown?
jsFiddle attached: https://jsfiddle.net/ebw2d9s5/1/
phpcode:
<?php
if(isset($_POST['submit']))
{
echo "Error: You need to Submit the Form!";
}
$to = "admin#hosting-mate.com"; // this is your Email address
$headers = "From:" . $from ;
$name = $_POST['name'];
$from = $_POST['email']; // this is the sender's Email address
$phone = $_POST['phone'];
$address = $_POST['address'];
$message = $name . " " . " wrote the following:" . "\n\n" . $_POST['message'];
$subject = "DKS Online Quote Form";
$subject2 = "DKS Electrical and Data";
$response = "Mail Sent. Thank you " . $name . " for contacting us, we will be in contact with you shortly.";
mail($to,$subject,$message,$headers);
mail($from,$subject2,$response);
header('Location: ../index.php');
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
?>
Here is the test site: http://www.hosting-mate.com/dkselectricalanddata/
EDIT - This is all that shows up in my emails: http://www.hosting-mate.com/dkselectricalanddata/_assets/img.png
Thanks in advance!
- Jesse
After trying many different things to figure out why my data never came up in my emails, I realized that it was actually the HTML file that was not sending the data properly.
For anyone else that may run into this issue with a data form, dont use:
enctype:"text/plain"
Instead, use:
enctype:"multipart/form-data"
This will stop your emails from appearing with just the headings and no input data.
Thank you for everyone that tried helping with this problem.
Try This...
<?php
if(isset($_POST['submit']))
{
$to = "admin#hosting-mate.com"; // this is your Email address
$headers = "From:" . $from ;
$name = $_POST['name'];
$from = $_POST['email']; // this is the sender's Email address
$phone = $_POST['phone'];
$address = $_POST['address'];
$message = $name . " " . " wrote the following:" . "\n\n" . $_POST['message'];
$subject = "DKS Online Quote Form";
$subject2 = "DKS Electrical and Data";
$response = "Mail Sent. Thank you " . $name . " for contacting us, we will be in contact with you shortly.";
mail($to,$subject,$message,$headers);
mail($from,$subject2,$response);
header('Location: ../index.php');
// You can also use header('Location: thank_you.php'); to redirect to another page.
// You cannot use header and echo together. It's one or the other.
}else {
echo "Error: You need to Submit the Form!";
}
?>
Change this in your HTML Code :
<input type="submit" class="button" id="Button2" value="SEND" style="position:absolute;left:0%;top:640px;width:100%;height:80px;font-family:font_header;font-size:27px;z-index:20">
to
<input type="submit" class="button" name="submit" id="Button2" value="SEND" style="position:absolute;left:0%;top:640px;width:100%;height:80px;font-family:font_header;font-size:27px;z-index:20">
I just started learning php not long ago, and I am currently building a "contact us" form which will send the user input to my email. I have been on this for days thinking I will figure it out, but I am not getting it. I wanna also be able to receive the user's input in my email and also be able to detect the user's IP address. When I submitted the form, I received every other input but the IP address though I used "localhost".
I tried with the <input type="hidden" name="message" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"> but I read online that it's better to do it without the <input type="hidden"> and just process everything in the form processor script. Please kindly help me with this.
<?php
$emailError = "";
$messageError = "";
function getUserIp(){
$client = $_SERVER['HTTP_CLIENT_IP'];
$forward = $_SERVER['HTTP_X_FORWARD_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP)) {
$ip = $client;
}elseif(filter_var($forward, FILTER_VALIDATE_IP)) {
$ip = $client;
}else{
$ip = $remote;
}
return $ip;
}
if(isset($_POST['submit'])){
//declares variable
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
if(empty($_POST['email'])){
$emailError = "Please enter your email";
}
if(empty($_POST['subject'])){
$subjectError = "Please enter a subject?";
}
}
if(!empty($_POST['email']) && !empty($_POST['subject'])){
// Send the email
$to = "you#yourdomain.com";
$email = "From: $email";
$subject = "Subject: $subject";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";;
$headers = "From: $email,";
$send_contact=mail($to,$email,$subject,$message,$headers);
header("Location: domain");
}
?>
change below section --
if(!empty($_POST['email']) && !empty($_POST['subject'])){
// Send the email
$to = "you#yourdomain.com";
$ip =getUserIp();
$email = "From: $email";
$subject = "Subject: $subject";
$message = "$message" . "\n\n\n==- Sent from the website with IP Address: " . $ip . " -==";;
$headers = "From: $email,";
$send_contact=mail($to,$email,$subject,$message,$headers);
header("Location: domain");
}
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'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)