I've got a simple contact form set up, but am having a little trouble with the finishing touches. I'm getting an error message whenever the form is submitted. Was hoping someone can take a look at my code and point out my mistake. Thanks!
(edited) Forgot to mention the error is that once form is submitted, I am being redirected to the error page (error.html) instead of the success (contactthanks.php) page.
HTML:
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="City">City:</label>
<input type="text" name="City" id="City" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Message:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
PHP:
<?php
$EmailFrom = "";
$EmailTo = "MYEMAIL#gmail.com";
$Subject = "";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
exit;
}
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
?>
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.html\">";
}
It is redirecting to your error page because the call to mail is returning false (indicating the email was not accepted for delivery) and that is exactly what you are telling it to do in that situation.
You need to check the SMTP set up on your server.
Typically the settings to check for are sendmail_from and sendmail_path in your PHP.ini file. Seeing as you are already setting a From header, sendmail_path is likely to not be set up correctly.
See what's causing the problem with
ini_set("display_errors", "1");
error_reporting(E_ALL);`.
My guess is you have wrong smtp settings and mail function fails.
Related
whats wrong in my code?
In HTML, I have taken form and i have created heading3 and i taken two inputs for name and email and i written textarea for message at last i taken another input is for submit and i closed the form tag.
<form method="post" action="contactengine.php">
<h3>Message Me</h3>
<input type="text" name="Name" id="Name" placeholder="Enter Name" required>
<input type="email" name="Email" id="Email" placeholder="Enter Email" required>
<textarea name="Message" id="Message" placeholder="Your Message"> </textarea>
<input type="submit" name="submit" value="Send Now" class="submit-button">
</form>
<?php
$EmailFrom = "xyz#gmail.com"; //your first email from where you want to send email
$EmailTo = "abc#gmail.com"; //your second email where you want to receive contact form content
$Subject = $_POST['Name']." Contacted you from your portfolio site"; //mail subject
$Name = Trim(stripslashes($_POST['Name'])); //getting name from html page
$Email = Trim(stripslashes($_POST['Email'])); //getting email from html page
$Message = Trim(stripslashes($_POST['Message'])); //getting message from html page
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
I would try this:
// send email
$success = mail($EmailTo, $Subject, $Body, "From:" . $EmailFrom);
This question already has answers here:
How to read if a checkbox is checked in PHP?
(21 answers)
Closed 5 years ago.
I have a working form but I would like to add a check box to it. Sorry, completely new to PHP so I'm unsure what I would need to add to the PHP to make it work.
The HTML:
<label for="Name"></label>
<input type="text" placeholder="Name" name="Name" id="Name" />
<label for="Tel"></label>
<input type="text" placeholder="Tel" name="Tel" id="Tel" />
<label for="Email"></label>
<input type="text" name="Email" placeholder="Email address" id="Email" />
<label for="Message"></label><br />
<textarea name="Message" placeholder="Your Message" rows="10" cols="20" id="Message"></textarea>
<label for="Subscribe"></label>
<input type="checkBox" name="CheckBox" id="checkbox" /> <p>Please tick this box to be kept up to date with offers</p>
The PHP:
$EmailFrom = "me#test.com";
$EmailTo = "me#test.com";
$Subject = "Enquiry from website";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
$Message = Trim(stripslashes($_POST['Checkbox']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=https://myurl.com\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
Any help would be really appreciated. Thanks
You're overwriting the $Message var with the value of the checkbox:
$Message = Trim(stripslashes($_POST['Message']));
$Message = Trim(stripslashes($_POST['Checkbox']));
You could also consider that checkboxes send on/off values unless you specify a different value with the parameter value="xxxx"
I'm trying to create a form where the user is required to use a password that I give them.
So when the user submits a form they will have to type "Password123" into the form before they can submit the form to my email. However, if they get the password wrong, the site will redirect to an error page.
So far, I keep getting an internal server error.
Here is the html:
<form method="post" action="contactengine.php">
<div class="forms">
<h4>PASWORD: <input type="password" placeholder="******" name="Password" /></h4>
<h4>YOUR NAME: <input type="text" placeholder="Smith Family" name="Name" /></h4>
<h4>YOUR CITY: <input type="text" placeholder="Anytown, USA"name="City" /></h4>
<h4>YOUR TELLEPHONE: <input type="text" placeholder="123.456.7891"name="Tel" /></h4>
<h4>YOUR EMAIL: <input type="text" placeholder="you#emaildomain.com" name="Email" /></h4>
<h4>MESSAGE TO US:</h4> <textarea width="10000px" name="Message" placeholder="Go ahead and say something nice to us!" rows="20" cols="20" id="Message"></textarea>
<br>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</form>
</div>
This is the PHP:
<?php
$Password = Trim(stripslashes($_POST['Password']));
$EmailFrom = "me#gmail.com";
$EmailTo = "me#gmail.com";
$Subject = "THIS IS THE SUBJECT";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// THIS IS WHERE I AM STUCK
if ($Password != "Password123") {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
May be you can try this ...
if ($Password != "Password123") {
echo "<script language='javascript'> window.location='error.htm'; </script>";
exit;
}
if ($Password != "Password123") {
echo "<script>document.location.href='new.php'</script>";
}
You may try this code,May be helpful to you.
Hi I have simple contact form for email on my site, and the form works including the success of a message sent,however, I am not receiving email to the designated webmail server. I am running the latest PHP. Do some web servers cache mail or is there some error in this code I am not seeing.
<?php
$EmailFrom = "email#mydomain.com";
$EmailTo = "email#mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=index\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
<div id="contact-area">
<form method="post" action="contactengine.php">
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" />
<label for="Email">Email:</label>
<input type="text" name="Email" id="Email" />
<label for="Message">Thought:</label><br />
<textarea name="Message" rows="20" cols="20" id="Message"></textarea>
<input type="submit" name="submit" value="Transmit" class="submit-button" />
</form>
<div style="clear: both;"></div></div>
Sending mail is more complicated than just running the mail() command, you need to take a look at the php.ini settings for the email configuration and continue troubleshooting from there.
If you are running on a host that does not configure the mail() function to run, you can try using the following opensource mailer:
PHPMailer
What you can try is to use the examples provided and run from your local development stack (XAMPP, MAMP, WAMP) connected from your home, you should be able to receive mail if configured correctly.
Once it is OK, you can try it on the server, it should send as well, if not, check that the outgoing port for sending mail is not blocked.
Alternative to using PHPMailer, you can look for a simple PHP mailer and try those out as well.
Don't use print "<meta http-equiv=\"refresh\"... because during testing, it caused an endless loop while sending multiple copies of the same Email.
Use header('Location: ...'); instead, along with exit;.
Plus, all your <meta http-equiv=\"refresh\" have generated a parse error.
Working example (tested with Email successfully received):
<?php
$EmailFrom = "email#mydomain.com";
$EmailTo = "email#mydomain.com";
$Subject = "Contacting Me";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
// echo "error 1";
header('Location: error.htm');
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
header('Location: http://www.example.com/');
exit;
}
else{
header('Location: error.htm');
exit;
}
?>
i think its the mail function headers issue.
http://php.net/manual/en/function.mail.php
http://php.net/manual/en/function.mail.php
Hi Can anyone suggest a quick form script for a php form?
I generated the one below from telepro and modified the html and emails a bit, but I get this error for the checkbox
Parse error: syntax error, unexpected '=' in /home/inn.co.uk/public/mailer.php on line 14
thanks for your help
Regards
Judi
<form method="POST" action="contact.php">
<div class="form-field">
<input type="text" name="Name" onFocus="if(this.value=='Name')this.value='';" value="Name">
</div>
<div class="form-field">
<input type="text" name="Telephone" onFocus="if(this.value=='Telephone')this.value='';" value="Telephone">
</div>
<div class="form-field">
<input type="text" name="Timetocall" onFocus="if(this.value=='Time to call')this.value='';" value="Time to call">
</div>
<div class="form-field">
<ul class="tickboxes">
<li>Do you agree to our<br/>Terms of Business <input type="checkbox" name="DoyouagreetoourTermsofBusiness?" value="Yes" /></li></ul></div>
<div class="button">
<input type="image" src="images/submit.jpg" value="" name="submit">
</div>
</form>
<?php
// Website Contact Form Generator
// http://www.tele-pro.co.uk/scripts/contact_form/
// This script is free to use as long as you
// retain the credit link
// get posted data into local variables
$EmailFrom = "enquiry#inn.co.uk";
$EmailTo = "judith#yahoo.co.uk";
$Subject = "New enquiry";
$Name = Trim(stripslashes($_POST['Name']));
$Telephone = Trim(stripslashes($_POST['Telephone']));
$Timetocall = Trim(stripslashes($_POST['Timetocall']));
$DoyouagreetoourTermsofBusiness? = Trim(stripslashes($_POST['DoyouagreetoourTermsofBusiness?']));
// validation
$validationOK=true;
if (Trim($Name)=="") $validationOK=false;
if (Trim($Telephone)=="") $validationOK=false;
if (Trim($Timetocall)=="") $validationOK=false;
if (Trim($DoyouagreetoourTermsofBusiness?)=="") $validationOK=false;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "Timetocall: ";
$Body .= $Timetocall;
$Body .= "\n";
$Body .= "DoyouagreetoourTermsofBusiness?: ";
$Body .= $DoyouagreetoourTermsofBusiness?;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=ok.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
You are using a question mark in your variable name. That doesn't work.
See the PHP Manual on variables for naming conventions.
replace
$DoyouagreetoourTermsofBusiness?
by
$DoyouagreetoourTermsofBusiness
and it will work fine.