I have HTML and PHP code for contact form. When someone type characters like this: š, đ, ž, č, and ć (UTF-8) characters, script reports few errors.
1. How can I add UTF-8 input in $first_name and $last_name input forms?
Next problem is, message of error or success is poping up by Java Script pop up. If I type in name box "šđžčć" and in last name box "šđžčć" script is reprting three mesages:
Wrong name
Wrong last name
Please try again (not exactly like this but some message)
And that means - three pop up windows! You get one - you close it, then second - close it too, etc etc, and it's frustrating. My secong question:
2. How can I rewrite this PHP and put all messages and errors in one function/value/message and make just one pop up says: Wrong name, last name, i love you etc.
PHP code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you#yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "<script type='text/javascript'>alert('error')</script>";
echo "<script type='text/javascript'>alert('these errors apears below')</script>";
echo $error."<br /><br />";
echo "<script type='text/javascript'>alert('please fix errors')</script>";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died(<script type='text/javascript'>alert('Submitted successfully!')</script>');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_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 .= '<script type='text/javascript'>alert('wrong email !')</script>';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= '<script type='text/javascript'>alert('invalid firs name')</script>';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= '<script type='text/javascript'>alert('invalid last name')</script>';
}
if(strlen($comments) < 2) {
$error_message .= '<script type='text/javascript'>alert('invalid message')</script>';
}
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 .= "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);
?>
<!-- include your own success html here -->
echo "<script type='text/javascript'>alert('Submitted successfully!')</script>"
<?php
}
?>
Related
I found and copied this code for sending an email via a form fill and it works, but I don't know anything about PHP coding.
I thought that I could add multiple addresses like ... $email_to: = "email1#email.com, email2#email.com, email3#email.com"; ... but this only sends to the last one in the list.
I know it seem simple to you guys, but I am not able to decipher the other posts about this subject, since the other code samples are not exactly like mine, and I know nothing about how the code works.
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email1#email.com, email2#email.com, email3#email.com";
$email_subject = "Someone registered on website.com";
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['cell_phone']) ||
!isset($_POST['text_me'])){
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
$cell_phone = $_POST['cell_phone']; // required
$text_me = $_POST['text_me']; // 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($cell_phone) < 2) {
$error_message .= 'Please enter your cell phone number.<br />';
}
if(strlen($text_me) < 2) {
$error_message .= 'Please choose whether we can text you or not.<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 .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Cell Phone: ".clean_string($cell_phone)."\n";
$email_message .= "Text Me?: ".clean_string($text_me)."\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);
?>```
... Please tell me what I can do to make it send to multiple emails.
Thanks in advance!
Dan
I have the attached contact form PHP script working but I would like the form to redirect to a sucess page: /form-confirmation.php
http://www.frlaw.co.uk/Contact-Test.php
Hope you can help.
Rich
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "sales#adelantedesign.co.uk";
$email_subject = "Your email subject line";
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['comments'])) {
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']; // 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(!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 .= "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 .= "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 very soon.
<?php
}
?>
You can redirect in a php by using the following lines:
header('Location: form-confirmation.php');
exit();
However you must do this before the page headers are sent (before any content is rendered on the page).
You can use javascript so that you can also put a timer delay on it so that people will see your Thank you message.
<script>
setTimeout(function(){
window.location.href = 'form-confirmation.php';
}, 5000); //5 seconds
</script>
You can tell the browser to redirect to a certain page by using PHP header()
So, if the mail is accepted for delivery then redirect the user to form-confirmation:
if(mail($email_to, $email_subject, $email_message, $headers)){
header("Location: form-confirmation.php");
exit;
}
I have a contact form which consists of following fields:
First name
Last name
Phone
Email
and Comment
Whenever the user submitting this form to my email so the subject value should take an auto
Number automatically generated by PHP.
Here is what I have so far
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "wouldn't want some spam mail would ya?";
$email_subject = "New Property From Customer";
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['comments'])) {
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']; // 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 (!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 .= "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 .= "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 -->
Your Property has been Posted please check your email address.
<?php
}
?>
Well you don't specify where you want to get this number from.
Since you said generate I'll assume you're creating it on the spot. So maybe add onto the end of $email_subject something like $rand(100,1000)?
Maybe you should clarify what you're asking for?
Side note:
Consider using filter_var($email_a, FILTER_VALIDATE_EMAIL) to check emails, not some preg_match. More info on that here.
to generate random number simply user rand() function as below
echo rand($fromRange,$toRange);
and you will get random number between the range you specified
I am making an email me form using PHP and when you don't fill required fields you are sent to another page with errors that you need to correct.
At the bottom of these errors I want to make return link, but I can't figure out what I am doing wrong, because Dreamweaver shows me an error at the line I wrote. So here is the code and I will mark the line with comment.
<?php
if(isset($_POST['email'])) {
$email_to = "example#example.com";
$email_subject = "website html form submissions";
function died($error) {
echo "Atsiprašome, bet yra klaidų Jūsų užpildytoje formoje.<br /><br /> ";
echo $error."<br /><br />";
echo "Grįžkite ir ištaisykite klaidas.<br /><br />";
echo "Atgal"; //this is the line
die();
}
if(!isset($_POST['first_name']) || !isset($_POST['last_name']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['comments'])) {
died('Atsiprašome, bet yra klaidų Jūsų užpildytoje formoje.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_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 .= 'Neteisingai įvestas el. paštas.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'Neteisingai įvestas vardas.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'Neteisingai įvesta pavardė.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'Neteisingai įvesta žinutė.<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 .= "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";
$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 -->
Ačiū už Jūsų žinutę.
<?php
}
die();
?>
echo "Atgal"; //this is the line
What you are doing here is ending the string literal at <a href=.
What you need to do is change it to:
echo "<a href='contacts.html'>Atgal</a>";
In PHP, quotation marks are used to mark the start and end of a string. You can use either single or double quotes.
'This is a string'
"This too!"
As you can see, you can use either single or double quotes. However, we also expect quotes to appear within strings. So how do we deal with that?
Well, one option is to use Chris Cooney's answer and make sure that your quotes inside the string are different from those that mark the start and end:
"I'm a string"
'"Murder," she wrote'
Those are both valid.
But what about times when you need to use the same type of quotation inside your string that you opened it with? For instance, maybe you need both types in your string. In that case, you must escape the quotations. All that means is adding \ in front of them.
'I\'m a valid string!'
Based on this, you could fix your issue on this line:
echo "Atgal";
by writing any of these:
echo "<a href='contacts.html'>Atgal</a>";
echo "Atgal";
echo '<a href=\'contacts.html\'>Atgal</a>';
just to name a few.
I'm creating a basic contact form with a few required fields and a required selection from a drop-down menu. The fill-in fields are working correctly, however the drop-down menu selection requirement is causing a parse error.
I commented out any instances of the drop-down menu requirement to find that the error is gone. So the error has something to do with the drop-down menu selection. According to the error logs, the problem is in line 49. I tried rewriting that line a few times without much success.
Is the error caused by something specifically in line 49 or is elsewhere in my syntax?
This is my first time writing PHP, so any help is greatly appreciated.
<?php
if(isset($_POST['email'])) {
// EMAIL and SUBJECT
$email_to = "xxx#xxx.com";
$email_subject = "Test Form Dev";
function died($error) {
// ERROR CODE
echo "We apologize for the inconvenience, but there were error(s) found with your form submission. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and correct the error(s).<br /><br />";
die();
}
// VALIDATION EXPECTED DATA EXISTS
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['inquiry']) ||
!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
$last_name = $_POST['last_name']; // REQUIRED
$email_from = $_POST['email']; // REQUIRED
$telephone = $_POST['telephone']; // NOT REQUIRED
$inquiry_type = $_POST['inquiry']; // 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(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$inquiry_exp = 'Charter, Media, Broker,'; // drop-down menu options
if(strlen($inquiry) < 1) {
$error_message .= 'Please select inquiry type.<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 .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Inquiry Type: ".clean_string($inquiry)."\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);
?>
<!-- RETURN MESSAGE (HTML): SUCCESSFUL FORM SUBMISSION -->
<p>Thank-you message goes here.</p>
<?php
}
die();
?>
Edit: I'm building this form in a MAMP environment. I've read elsewhere that I need to create an htaccess file, but is that necessary for a local dev?
Edit 2: After looking around other forums, I learned that I have to break down the dropdown menu items individually in PHP. I got that accomplished, but am still getting Parse:syntax errors on line 98 (the last line) stating "unexpected $end". However, I cannot get the menu selection to populate in the generated email nor figure out what specifically is causing the error. I originally fixed my code according to the error log without success.
Here's my updated code:
<?php
if(isset($_POST['email'])) {
// EMAIL and SUBJECT
$email_to = "xxx#xxx.com";
$email_subject = "XXX";
function died($error) {
// ERROR CODE
echo "We apologize for the inconvenience, but there were error(s) found with your form submission. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and correct the error(s).<br /><br />";
die();
}
// VALIDATION EXPECTED DATA EXISTS
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['inquiry']) ||
!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
$last_name = $_POST['last_name']; // REQUIRED
$email_from = $_POST['email']; // REQUIRED
$telephone = $_POST['telephone']; // NOT REQUIRED
$comments = $_POST['comments']; // REQUIRED
$inquiry = $_POST['inquiry'];
if( empty( $inquiry ) || $inquiry == "null" )
// If there isn't a value for the dropdown, or they've selected the option
// that reads "Please select one" then return an error
die( "Please select your reason for inquiring on the drop-down menu." );
switch( $inquiry ){
case "Broker" : die(); break;
case "Press" : die(); break;
case "Charter" : die(); break;
default : die();
}
}
$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 .= "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 .= "Inquiry Type: ".clean_string($inquiry)."\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();
?>
Can anyone provide any insight to what's causing the form to error out?
CHange if(strlen($inquiry) < 1){ ... on line 49 to if(strlen($inquiry_type) < 1)
Also change clean_string($inquiry) to clean_string($inquiry_type) on line 69
You haven't declared a $inquiry variable so the following lines will report errors:
if(strlen($inquiry) < 1) {
$email_message .= "Inquiry Type: ".clean_string($inquiry)."\n";
You do have a $inquiry_type variable so this is probably a typo.