I have a problem with my contact form. When I fill it on my website, I get an e-mail, but fields: Name, eMail and Message remain blank in my received e-mail.
My contact complex consists of: contacts.html (html page on my website); contactengine.php (an actual form); thanks.html (it opens when a question is successfully posted); unsuccessful.html (opens when unsuccessful).
Can anyone tell me what's wrong with this code?
CONTACTS.HTML
<form id="form" form action="contactengine.php" method="post" >
<fieldset>
<label><input type="text" name="cf_name" value="Name" id="cf_name" onBlur="if(this.value=='') this.value='Name'" onFocus="if(this.value =='Name' ) this.value=''"></label>
<label><input type="text" name="cf_email" value="e-Mail" id="cf_email" onBlur="if(this.value=='') this.value='e-Mail'" onFocus="if(this.value =='e-Mail' ) this.value=''"></label
<label><textarea name="cf_message" id="cf_message" onBlur="if(this.value==''){this.value='Message'}" onFocus="if(this.value=='Message'){this.value=''}">Message</textarea></label>
<div class="btns">
Cancel
Submit</div>
</fieldset>
</form>
CONTACTENGINE.PHP
<?php
$EmailFrom = "form#mysite.rs";
$EmailTo = "office#mysite.rs";
$Subject = "Message from website";
$cf_name = Trim(stripslashes($_POST['Name']));
$cf_email = Trim(stripslashes($_POST['Email']));
$cf_message = Trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=unsuccessful.html\">";
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $cf_name;
$Body .= "\n";
$Body .= "e-Mail: ";
$Body .= $cf_email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $cf_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=thanks.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=unsuccessful.html\">";
}
?>
The lines:
$cf_name = Trim(stripslashes($_POST['Name']));
$cf_email = Trim(stripslashes($_POST['Email']));
$cf_message = Trim(stripslashes($_POST['Message']));
should be changed to
$cf_name = Trim(stripslashes($_POST['cf_name']));
$cf_email = Trim(stripslashes($_POST['cf_email']));
$cf_message = Trim(stripslashes($_POST['cf_message']));
The variable name doesn't matter, but the text inside $_POST[''] should be the same as the name value for the form element!
You prefixed the names in your HTML with cf_, but didn't do the same in your PHP!
EDIT: Oops, I missed something!
You are linking to contactengine.php with an <a> link!
Your browser treats this as a regular GET request, and doesn't send the data with it!
Even though you put JavaScript code to submit the form, the browser's default action (to go to the link) overpowers your code.
To fix this, either
Change the last <a> tag in contacts.html to <input type="submit" value="Submit" />, turning the link into a submit button, or
Add ; return false; to the end of your onclick handler for the submitting link. This stops the browser from doing its default behavior, and submits the form.
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);
Some help with the following would be greatly appreciated! I've been searching the web and stackoverflow for hours. There is this problem with my php mail function.
There's a form on my website (the site uses wordpress) with two text fields, name and phone number. There's also a hidden field in the form that displays the current url, so we can see on what page the form was filled in. This information is also stored in a php session.
<?php session_start();
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
?>
<form name="callback" action="callme.php" method="post" onsubmit="return(validate());">
<label>Name:</label><input type="text" name="name" size=15 />
<label>Tel:</label><input class="sky" type="text" name="tel" size=15 />
<div id="afterfive"><p>Call after five<input type="checkbox" name="afterfive" value="Call back after five"></p></div>
<input type="hidden" name="url" value="<?php echo $_SESSION['url']; ?>">
<input type="submit" class="classname" value="Call me back!" title="Call me back!" />
</form>
<script>
function validate()
{
if( document.callme.name.value == "" )
{
alert( "Please fill in your name" );
document.callme.name.focus() ;
return false;
}
if( document.callme.tel.value == "" )
{
alert( "Please fill in your phone number" );
document.callme.tel.focus() ;
return false;
}
}
</script>
The following php code is callme.php:
<?php session_start();
$name = $_POST['name'];
$tel = $_POST['tel'];
$afterfive = $_POST['afterfive'];
$url = $_POST['url'];
$to = "me#myemail.com";
$subject = "Please call back $name";
$message .= "Hi, the following person would like to be called back: \n";
$message .= "Name: $name \n";
$message .= "Phonenumber: $tel \n";
$message .= "$afterfive \n";
$message .= "This message was send from this page: $url \n";
$headers = "From: meagain#myemail.com" . "\r\n";
$headers .= "BCC: meagain#myemail.com" . "\r\n";
if(mail($to, $subject, $message, $headers)){
$_SESSION['name'] = $_POST['name'];
$_SESSION['tel'] = $_POST['tel'];
header("Location: http://www.mywebsite.thankyou");
}
?>
After submitting the form, the visitor is redirected to our thankyou page and given the opportunity the fill in additional information using a second form. The information previously stored in the php session (form fields name, tel and url) are added in hidden form fields.
This all works fine most of the time, but sometimes we receive e-mails with all or some fields empty. Of course this could be users with javascript disabled or google bots that submit blank forms, but the weird thing is that sometimes even the url field is empty (the form is not visible on our homepage). Shouldn't $_SERVER['REQUEST_URI'] always still work?
I was thinking about adding php form validation, but I'm not sure this will solve the problem. Could this have something to do with the hyper cache plug-in for wordpress? Or could it be related to the php session?
"but sometimes we receive e-mails with all or some fields empty"
You should be using a server-side method instead of JS such as
if(empty($_POST['name'])){ die("You need to enter your name.");
(JS can always be disabled by the user, one probable cause for empty emails/fields)
and that will ensure that the fields you wish to be NOT empty, be filled. In conjunction with what Andrewsi mentioned, use if(isset($_POST['submit'])){ at the top of your handler, and name your submit button to name="submit" that way the callme.php if accessed directly, won't process the information without the submit button being clicked.
For example:
Note: There are many other ways to achieve this, but this is a basic yet effective method.
Naming your submit button such as:
<input type="submit" name="submit" value="Submit">
in your case, it would be:
<input type="submit" name="submit" class="classname" value="Call me back!" title="Call me back!" />
PHP handler
<?php
session_start();
if(isset($_POST['submit'])){
if(empty($_POST['name'])){ die("You need to enter your name."); }
if(empty($_POST['tel'])){ die("You need to enter your telephone number."); }
if(empty($_POST['afterfive'])){ die("You need to fill this field."); }
if(empty($_POST['url'])){ die("You need to fill this field."); }
$name = $_POST['name'];
$tel = $_POST['tel'];
$afterfive = $_POST['afterfive'];
$url = $_POST['url'];
$to = "me#myemail.com";
$subject = "Please call back $name";
$message .= "Hi, the following person would like to be called back: \n";
$message .= "Name: $name \n";
$message .= "Phonenumber: $tel \n";
$message .= "$afterfive \n";
$message .= "This message was send from this page: $url \n";
$headers = "From: meagain#myemail.com" . "\r\n";
$headers .= "BCC: meagain#myemail.com" . "\r\n";
if(mail($to, $subject, $message, $headers)){
$_SESSION['name'] = $_POST['name'];
$_SESSION['tel'] = $_POST['tel'];
header("Location: http://www.mywebsite.thankyou");
}
}
// You could use this at end also to show a message
// if callme.php is accessed directly.
// else {echo "You cannot do that from here.";exit;}
?>
Your javascript refers to document.callme, but there is nothing in your code with that name.
I made captcha by this [tutorial][1],
[1]: http://codechirps.com/how-to-add-a-completely-custom-captcha-to-any-web-form/ but it seems to me it doesn't complete.
I made code but i can send email even if i put wrong answer. I feel that i have to write extra code in php file but i don't know where. Any help greatly appriciated
<div class="modal-body">
<form class="contact" name="contact">
<label class="label" for="name">Имя</label><br>
<input type="text" name="name" class="input-xlarge"><br>
<label class="label" for="email">E-mail</label><br>
<input type="email" name="email" class="input-xlarge"><br>
<label class="label" for="message">Сообщение</label><br>
<textarea name="message" class="input-xlarge"></textarea>
</form>
</div>
<div class="modal-footer">
<p>2 + 3 =</p>
<input type="text" name="captcha" />
<input class="btn btn-warning" type="submit" value="Отправить" id="submit">
Закрыть
<?php
$myemail = '';
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
$captcha = check_input($_POST['captcha']);
echo "<span class=\"alert alert-success\" >Сообщение отправлено</span><br><br>";
if (!preg_match("/5/", $captcha))
{
show_error("Check your math, Dude");
}
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
}?>
Okay, so you need to check the values of your inputs to see if they are valid. If not, you display an error and the mail doesn't get sent. If all the checks pass, the maildoes get sent. So you need to check the $_POST['email'] and the $_POST['captcha'] field (and if you want to, check if the rest isn't empty or whatever).
In php, you can do this like this:
$myemail = "";
if(isset($_POST['name'])){ // check if a form is submitted
if(empty(trim($_POST['name'])) || empty(trim($_POST['message'])) || empty(trim($_POST['email'])) || empty(trim($_POST['captcha'])) ){ // check if values are not empty
echo "Please fill in all the required fields.";
}else if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){ // check email
echo "Please give a real e-mail address.";
}else if(!preg_match("/5/", $_POST['captcha'])){ // the code provided by your script
echo "Get your math right, dude!";
}else{ // all fields seem to be ok
// sanitize input using htmlspecialchars(), see http://stackoverflow.com/a/5788361/1319187
$name = htmlspecialchars($_POST['email']);
$email = $_POST['email']; // email doesn't need to be sanitized since it's been filtered
$message = htmlspecialchars($_POST['message']);
//Send the mail
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n ".
"Email: $email\n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
if(mail($to,$email_subject,$email_body,$headers)){
echo "Succes! Your mail has been sent!";
}else{
echo "Something went wrong with the sending of a mail.";
}
}
}
Should be rather straightforward, you can google some functions if you don't know what they do.
I also have no idea where check_input() comes from. It's not a native PHP function, and the link you provided doesn't show what it does. Also, the regex to check whether the value of the captcha is 5 is a bit stupid, you could just check $_POST['captcha'] == '5'. Also keep in mind you have to randomize these values a bit.
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.
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.