html and php contact form trouble - php

Can somebody please tell me what is wrong with my code. I cant figure out what the error is.
contact form
<form action="mail.php" method="POST">
<p>Name</p> <input type="text" name="name">
<p>Email</p> <input type="text" name="email">
<p>Phone</p> <input type="text" name="telephone">
<p>Request Phone Call:</p>
Yes:<input type="checkbox" value="Yes" name="call">
No:<input type="checkbox" value="No" name="call"><br />
<p>Priority</p>
<select name="priority" size="1">
<option value="Low">Low</option>
<option value="Normal">Normal</option>
<option value="High">High</option>
<option value="Emergency">Emergency</option>
</select>
<br />
<p>Type</p>
<select name="type" size="1">
<option value="update">Website Update</option>
<option value="change">Information Change</option>
<option value="addition">Information Addition</option>
<option value="new">General Enquiries</option>
</select>
<br />
<p>Message</p><textarea name="message" rows="10" cols="40"></textarea><br />
<input type="submit" value="Send" class="button"/><input type="reset" value="Clear" class="button"/>
</form>
mail.php
<?php
if(isset($_POST['email'])) {
$email_to = "myemail#sky.com";
$email_subject = "Tip Top Music";
function died($error) {
// Error codes
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();
}
// Required fields
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('Required Fields are not complete');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
if(!isset($_POST['call'])) {
$call = "No"; // if checkbox was left unchecked. Default is No
}
else {
$call = ($_POST['call'])
}
$priority = $_POST['priority']; // Will already have default value
$type = $_POST['type']; // Will already have default value
$message = $_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,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 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 .= "Request Callback: ".($call)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Priority: ".($priority)."\n";
$email_message .= "Type: ".($type)."\n";
$email_message .= "Message: ".clean_string($message)."\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);
?>
<!-- Message success -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
I get undefined index call pointing to $call = "No"; line. HOWEVER the script should not even run at all because I posted a blank form?? When I echo the input fields they are blank but they if put IF statements in to check if they are set (isset) it goes into the statement as if they have been set? Should I be using something other than isset to check for empty inputs? This usually works so im confused as to why its not now?

Your code is working fine after adding missing semicolon to $_POST['call'];
correct one should be $call = ($_POST['call']);
when submitting empty form it gives error messages. so i think validation is working fine.

Missing semicolon on $_POST['call']
if(!isset($_POST['call'])) {
$call = "No"; // if checkbox was left unchecked. Default is No
}
else {
$call = $_POST['call']; // Missing semicolon here
}

Related

How to pass checkbox selections to php mailer form?

The checkbox fields are not passing to my email from my html form.
When a user fills in the form all the information is received in my email (user name, email, phone, comments) except for the boxes they checked on the form.
I have tried putting the [] like this name="feature[]" in the name field but did not work so I reverted back.
My form....
<form name='quote' method="post" action="quote-request.php">
...
<center><h4 style='color:#C42D35'>Check each feature you will need:</h4></center>
<div class='row'>
<div class='col-md-6'>
<input type="checkbox" name="feature" value="something" checked> Basic <br>
<input type="checkbox" name="feature" value="something1"> Option 1 <br>
<input type="checkbox" name="feature" value="something2"> Option 2 <br>
</div>
<div class='col-md-6'>
<input type="checkbox" name="feature" value="something3"> Option 3 <br>
<input type="checkbox" name="feature" value="something4"> Option 4 <br>
</div>
</div>
...
</form>
My handler (quote-request.php)...
<?php
if(isset($_POST['email'])) {
$email_to = "myemail#mydomain.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['firstName']) ||
!isset($_POST['email']) ||
!isset($_POST['phone']) ||
!isset($_POST['feature']) ||
!isset($_POST['comment'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$firstName = $_POST['firstName']; // required
$email_from = $_POST['email']; // required
$phone = $_POST['phone']; // not required
$feature = $_POST['feature']; // required
$comment = $_POST['comment']; // 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,$firstName)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(strlen($comment) < 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($firstName)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($phone)."\n";
$email_message .= "Comments: ".clean_string($comment)."\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
}
?>

How to SIMPLY validate a very simple drop down in PHP, no need for arrays

I want to validate a drop down select on a form, I want the visitor to select one value, any value is ok. I can put values manually, so no need for anything complicated like arrays.
I can understand the validation for email and name, but how can I do this for a drop down select?
I put the form online over here:
http://satearn.com/question/landing_page.html
<form name="contactform" method="post" action="send_form_email.php">
<div class="section">
<span>1</span>First & Last Name</div>
<div class="inner-wrap testbruno">
<input type="text" name="first_last_name" class="form-style-10-input">
</div>
<div class="section">
<span>2</span>Email Address</div>
<div class="inner-wrap">
<input type="email" name="email" class="form-style-10-input">
</div>
<div class="section">
<span>3</span>Budget</div>
<div class="inner-wrap">
<select name="budget_selection" class="form-style-10-drop-down">
<option value="$499">$499</option>
<option value="$999">$999</option>
</select>
</div>
<div>
<input name="Sign Up" type="submit" value="Send">
</div>
</form>
Under is the PHP page:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "bruno#xuzo.com";
$email_subject = "My own subject Line Bruno";
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_last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['budget_selection'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_last_name = $_POST['first_last_name']; // required
$email_from = $_POST['email']; // required
$budget_selection = $_POST['budget_selection']; // 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_last_name)) {
$error_message .= 'The First Name 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 .= "Customer First Name: ".clean_string($first_last_name)."\n";
$email_message .= "Customer Email: ".clean_string($email_from)."\n";
$email_message .= "Budget Selection: ".clean_string($budget_selection)."\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
}
?>
add like this
<select name="budget_selection" class="form-style-10-drop-down" required='required' >
<option value="">select</option>
<option value="$499">$499</option>
<option value="$999">$999</option>
</select>
It just client side browser validation you have to validate in server side also.

PHP form with checkbox and verification does't work

i"m new here and i don't understand php at all, but i need to do a contact form with checkbox and verification, this is my code ( is a frankenstein code).
I need that my form send an email with the information avoiding spam, and missing information, and when mail arrive has all the details in header like a mail to reply, subject and all.
if you can post an correct code will be wonderful.
thanks in advance
<form name="htmlform" method="post" action="app.php">
<label for="name">*Nombre Completo</label>
<input type="text" name="name" maxlength="150" size="50">
<label for="company">*Empresa</label>
<input type="text" name="company" maxlength="150" size="50">
<label for="telephone">Teléfono</label>
<input type="text" name="telephone" maxlength="150" size="50">
<label for="email">*Email</label>
<input type="text" name="email" maxlength="180" size="50">
<label for="services">*Servicios de Interes</label>
<input type="checkbox" name="services[]" value="apps" />apps<br />
<input type="checkbox" name="services[]" value="Marketing Movil" />Marketing Móvil<br />
<input type="checkbox" name="services[]" value="video juegos" />video Juegos<br />
<label for="comments">*Comentarios</label>
<textarea name="comments" maxlength="1000" cols="40" rows="6"></textarea>
<div class="hide">Leave this empty:<input name="url" /></div>
<center><input class="button large" type="submit" value="Submit Form"></center>
</form>
<?php
// if the url field is empty
if(isset($_POST['url']) && $_POST['url'] == '')
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "my#mail.com";
$email_subject = "Contact from web.com";
function died($error) {
// your error code can go here
echo "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['full_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['services']) ||
!isset($_POST['comments']))
{
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$full_name = $_POST['full_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$services = $_POST['services']; // 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,$full_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 .= "Full Name: ".clean_string($full_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Services: ".clean_string($services)."\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);
// hacker defense
function clean($var){//request string cleaner
if(get_magic_quotes_gpc()) $var=stripslashes($var); //clean
$var=mysql_real_escape_string($var); //clean
return strip_tags($var, '<b><a>');//returning clean var
}
function hackerDefense(){//thanks to Allen Sanford
// begin hacker defense
foreach ($_POST as &$postvalue){ //checking posts
$postvalue = clean($postvalue);//cleaning the value
}
} // end hacker defense
?>
<!-- include your own success html here -->
<center>
<img src="images/logo.png" />
<br>
<br>
Thank you for contacting us. We will be in touch.<br>
<br>
HOME</center>
<?php
}
?>

html php send form doesn't send email

My form doesn't generate an E-Mail it just redirects me to a blank page.
i have my .php form in a folder named php on my server
thank you for your help.
here is my html code
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" value="">
</label>
<label><strong>Email:</strong>
<input type="text" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" value="">
</label>
<label><strong>Message:</strong>
<textarea></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>
here is the php code i'm using:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "###.com";
$email_subject = "havok security contact form";
function died($error) {
// your error code can go here
echo "We are very sorry, but there was an error 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['message'])) {
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['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,$name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(strlen($message) < 2) {
$error_message .= 'The Message 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 .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Phone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".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
}
?>
The input type under email needs to have a name attribute called "email". Your PHP script is looking to see if $_POST["email"] is set, and it is not. So, this should work:
<form id="form" method="post" action="php/send_form_email.php">
<fieldset>
<label><strong>Name:</strong>
<input type="text" name="name" value="">
</label>
<label><strong>Email:</strong>
<input type="text" name="email" value="">
</label>
<label><strong>Phone:</strong>
<input type="text" name="phone" value="">
</label>
<label><strong>Message:</strong>
<textarea name = "message"></textarea>
</label>
<div class="btns">ClearSend</div>
</fieldset>
</form>

How to send Email with HTML form and PHP Script

Someone please help me with my code everything seems to be fine but, when ever I click the Submit button the browser displays the PHP scripts instead of Sending the email to my mail account. PHP is installed I thought that was the problem. please help?????? here is my PHP script
<?php
if(isset($_POST['submit'])) {
$email_to = "example#domain.com";
$email_subject = "Wedding Wishes";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you";
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['txtName']) ||
!isset($_POST['txtSurname']) ||
!isset($_POST['txtEmail']) ||
!isset($_POST['txtRegion']) ||
!isset($_POST['cmbRelation']) ||
!isset($_POST['txtMessage'])) {
died('We are sorry, but there appears to be a problem with the form you');
}
$first_name = $_POST['txtName']; // required
$last_name = $_POST['txtSurname']; // required
$email_from = $_POST['txtEmail']; // required
$region = $_POST['txtRegion'];
$relation = $_POST['txtRelation'];// not required
$message = $_POST['txtMessage']; // 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($message) < 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 .= "Region: ".clean_string($region)."\n";
$email_message .= "Relation: ".clean_string($relation)."\n";
$email_message .= "Message: ".clean_string($message)."\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);
?>
And my Html form is as follows.
<div class="grid_16" style="width:400px;">
<div class="top-1">
<form id="form" method="post" action="SendEmail_Wishes.php" style="width: 400px;" >
<fieldset>
<p>
<label><strong>Name:</strong>
<input type="text" name="txtName" style= "width:300px; height:25px;">
<strong class="clear"></strong></label>
<label><strong>Surname:</strong>
<input type="text" name="txtSurname" style= "width:300px; height:25px;">
<strong class="clear"></strong></label>
<label><strong>Email:</strong>
<input name="txtEmail" type="text" style= "width:300px; height:25px;">
<strong class="clear"></strong></label>
<label><strong>Region:</strong>
<input name="txtRegion" type="text" id="txtRegion" value="Country & City" >
<strong class="clear"></strong></label>
<label><strong>Relation:</strong>
<select name="cmbRelation" id="cmbRelation" accesskey="2" tabindex="2">
<option value="Family">Family</option>
<option value="Friend" selected>Friend</option>
</select>
<br>
</label>
<label><strong>Message:</strong>
<textarea name="txtMessage" id="txtMessage" style= "width:300px;"></textarea>
<font size="1"><br />
<br />
<br />
<br />
<br />
<br />
(Maximum characters: 250)<br>
<input name="submit" type="submit" value="submit" style="width:50px;">
<br />
<strong class="clear"></strong></label>
</p>
<script type="text/javascript">
function blank(a) { if(a.value == a.defaultValue) a.value = ""; }
function unblank(a) { if(a.value == "") a.value = a.defaultValue; }
</script>
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
</fieldset>
</form>
You don't close the if(isset($_POST['submit'])) statement.
You are missing } at the end.
You server does not seems to be configured correctly.
I recommend before trying to send an email in php to make work an hello world example :
<?php
echo phpinfo();
?>
Try to call directly your page from your browser before using any html page.
Download SMTP server, run the server before you click submit.

Categories