I am quite new to php so excuse my poor knowledge but I am using an email form for users to register on my web page but I am getting errors running it.
the full script is as follow:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "contact#slapmybeat.com";
$email_subject = "New e-mail subscriber";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form your submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if
(!isset($_POST['email'])) {
died('We are sorry, but there appears to be a problem with the email your submitted.');
}
$email_from = $_POST['email']; // required
$error_message = "";
$email_exp = "/^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/";
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Email: ".clean_string($email_from)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?
}
?>
First I had a "eregi depreciated" error because of the initial script which was as follow:
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
After googling it I replaced it with:
$error_message = "";
$email_exp = "/^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/";
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
but now it is giving me the following, although the email adress is correct:
We are very sorry, but there were error(s) found with the form your
submitted. These errors appear below.
The Email Address you entered does not appear to be valid.
Please go back and fix these errors.
any help will be much apreciated
eregi is case-insensitive, preg_match is not.
You have to add i to the end of the pattern (/..../i).
Not a direct answer, but if you want to check for a valid e-mail address, it is a lot easier to use php's built-in filters:
$email = filter_var($email_from, FILTER_VALIDATE_EMAIL);
Now $email will contain the filtered e-mail address or false if the filter failed (not a valid address in this case).
You should escape the "." inside the [] because it will match every character if you don't do it, you'll have:
"/^[A-Z0-9\._%-]+#[A-Z0-9\.-]+\.[A-Z]{2,4}$/i"
Related
I have read all your posts about inserting headers into a php form file in order to redirect the user to another URL AFTER the form is submitted - but I can't figure out how to do it. Below is my code. Can you show me where to put the header/redirect so that the information gets e-mailed and then the user goes to another html page?
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "pecraig#moneymovers.com";
$email_subject = "Mailing List Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you
submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['company']) ||
!isset($_POST['street']) ||
!isset($_POST['city']) ||
!isset($_POST['state']) ||
!isset($_POST['zip'])) {
died('We are sorry, but there appears to be a problem with the form you
submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$company = $_POST['company']; // required
$street = $_POST['street']; // required
$city = $_POST['city']; // required
$state = $_POST['state']; // required
$zip = $_POST['zip']; // required
$error_message = "";
$string_exp = "/^[A-Za-z0-9 .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$company)) {
$error_message .= 'The Company you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$street)) {
$error_message .= 'The Street you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$city)) {
$error_message .= 'The City you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$state)) {
$error_message .= 'The State you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$zip)) {
$error_message .= 'The Zip Code you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Response from Mailing List Page. Please enter in database.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Street: ".clean_string($street)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip: ".clean_string($zip)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thanks for subscribing to our mailing list
<?php
}
?>
Right after #mail($email_to, $email_subject, $email_message, $headers);
header('Location: nextpage.php');
Note that you will never see 'Thanks for subscribing to our mailing list'
That should be on the next page, if you echo any text you will get an error because the headers would have been already created, if you want to redirect never return any text, not even a space!
First give your input type submit a name, like this name='submitform'.
and then put this in your php file
if (isset($_POST['submitform']))
{
?>
<script type="text/javascript">
window.location = "http://www.google.com/";
</script>
<?php
}
Don't forget to change the url to yours.
If your redirect is in PHP, nothing should be echoed to the user before the redirect instruction.
See header for more info.
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.google.com/"
You can include your header function wherever you like, as long as NO html and/or text has been printed to standard out.
For more information and usage: http://php.net/manual/en/function.header.php
I see in your code that you echo() out some text in case of error or success. Don't do that: you can't. You can only redirect OR show the text. If you show the text you'll then fail to redirect.
Whenever you want to redirect, send the headers:
header("Location: http://www.example.com/");
Remember you cant send data to the client before that, though.
Once had this issue, thought it reasonable to share how I resolved it;
I think the way to do that in php is to use the header function as:
header ("Location: exampleFile.php");
You could just enclose that header file in an if statement so that it redirects only when a certain condition is met, as in:
if (isset($_POST['submit'])){ header("Location: exampleFile.php") }
Hope that helps.
I have read all your posts about inserting headers into a php form file in order to redirect the user to another URL AFTER the form is submitted - but I can't figure out how to do it. Below is my code. Can you show me where to put the header/redirect so that the information gets e-mailed and then the user goes to another html page?
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "pecraig#moneymovers.com";
$email_subject = "Mailing List Form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you
submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['company']) ||
!isset($_POST['street']) ||
!isset($_POST['city']) ||
!isset($_POST['state']) ||
!isset($_POST['zip'])) {
died('We are sorry, but there appears to be a problem with the form you
submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$company = $_POST['company']; // required
$street = $_POST['street']; // required
$city = $_POST['city']; // required
$state = $_POST['state']; // required
$zip = $_POST['zip']; // required
$error_message = "";
$string_exp = "/^[A-Za-z0-9 .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$company)) {
$error_message .= 'The Company you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$street)) {
$error_message .= 'The Street you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$city)) {
$error_message .= 'The City you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$state)) {
$error_message .= 'The State you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$zip)) {
$error_message .= 'The Zip Code you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Response from Mailing List Page. Please enter in database.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Company: ".clean_string($company)."\n";
$email_message .= "Street: ".clean_string($street)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip: ".clean_string($zip)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thanks for subscribing to our mailing list
<?php
}
?>
Right after #mail($email_to, $email_subject, $email_message, $headers);
header('Location: nextpage.php');
Note that you will never see 'Thanks for subscribing to our mailing list'
That should be on the next page, if you echo any text you will get an error because the headers would have been already created, if you want to redirect never return any text, not even a space!
First give your input type submit a name, like this name='submitform'.
and then put this in your php file
if (isset($_POST['submitform']))
{
?>
<script type="text/javascript">
window.location = "http://www.google.com/";
</script>
<?php
}
Don't forget to change the url to yours.
If your redirect is in PHP, nothing should be echoed to the user before the redirect instruction.
See header for more info.
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP
Otherwise, you can use Javascript to redirect the user.
Just use
window.location = "http://www.google.com/"
You can include your header function wherever you like, as long as NO html and/or text has been printed to standard out.
For more information and usage: http://php.net/manual/en/function.header.php
I see in your code that you echo() out some text in case of error or success. Don't do that: you can't. You can only redirect OR show the text. If you show the text you'll then fail to redirect.
Whenever you want to redirect, send the headers:
header("Location: http://www.example.com/");
Remember you cant send data to the client before that, though.
Once had this issue, thought it reasonable to share how I resolved it;
I think the way to do that in php is to use the header function as:
header ("Location: exampleFile.php");
You could just enclose that header file in an if statement so that it redirects only when a certain condition is met, as in:
if (isset($_POST['submit'])){ header("Location: exampleFile.php") }
Hope that helps.
I am using the below PHP script in my contact form to send enquiries to my email address. I tested it using XAMP but no email came through. Is that due to it being tested on a local host or is there something up with my code?
Please have a look below and let me know, as you can tell I am very new to PHP.
thanks,
Joe.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "angliojoe#gmail.com";
$email_subject = "eMail enquiry";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you shortly.
<?php
}
?>
Yes, it's probably because you don't have a mail server installed on your local machine.
You might want to try uploading it to a Linux web server (I assume you're using Windows) that also has an email server installed?
You can try using PHPMailer, but then you need an smtp server. PHPMailer at sourceforge
remove the # symbol before mail() and check with apache error log for actual mailer problem.
Note: The problem should be in mailer configuration. you must be defined smtp details in php.ini
check phpinfo(); if there is any entry for smtp? sendmail? if you have smtp server installed then call mail('you#yourmail.com','subject','bodyMessage') and see if you get any mail?
A alert box should appear if there occurs any error below while submitting the form. And there should also appear a message box if the form is submitted correctly.
The existing code takes me to the other pages when there are any errors occured. And also takes me to the other page when the form is submitted. I need the messsage box for the errors occured accoring to the given below code.
Below is my Php code
<?php
if(isset($_POST['Email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "jay44556677#gmail.com";
$email_subject = "website html form submissions";
function died($error) {
// your error code can go here
echo "We're sorry, but there's errors found with the form you submitted.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['Name']) ||
//!isset($_POST['last_name']) ||
!isset($_POST['Email']) ||
//!isset($_POST['telephone']) ||
!isset($_POST['Message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['Name']; // required
//$last_name = $_POST['last_name']; // required
$email_from = $_POST['Email']; // required
//$telephone = $_POST['telephone']; // not required
$comments = $_POST['Message']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
//if(!preg_match($string_exp,$last_name)) {
//$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
//}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($first_name)."\n";
//$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
//$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- place your own success html below -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
die();
?>
There are several ways - ajax (as Barmar suggested) or just regular javascript function called on the button click (onClick()).
If you choose the first method you can find multitude of script examples here or elsewhere on the Internet. If you choose the latter method, then don't use the <input type="submit" /> but <input type="button" onclick="yourfunction();return false" />. The last bit (return false) is actually quite important. Among others you can check this topic; it might give you some further ideas.
Ok, I have a form which sits inside of a popup box. I want the confirmation message to display inside of the pop up box when the form has been successfully submitted.
How do I do this?
I have the site live that I am working on, take a look here www.firestarmediallc.com
When you get to the site, click the GET QUOTE link on the top of the page and the box will appear.
Actually, if you guys could take a look at the mailer functions and see if those are correct too, that would be awesome. I have been trying to get this form functional for the last two days and haven't had any luck.
Sorry, I just realized that you can't see the php file. Thank you in advance. Here it is:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "tabethamoe#yahoo.com";
$email_subject = "Quote Request";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = "^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$";
if(!eregi($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z .'-]+$";
if(!eregi($string_exp,$first_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
$('#contactform').submit(function () {
sendContactForm();
return false;
});
<?php
}
?>
The site's not loading, but if you want a form to submit to itself, you simply have:
[... deleted irrelevant code, now that I can see how the site works...]