insert form data into table - php

My form emails data to customer ok, I also need it to save that data to a table called card, which it doesn't do. Please let me know where I'm going wrong. I'm almost certain it's something to do with when I connect to the DB.
<?php
if(isset($_POST['email'])) {
$email_to = ($_POST['email']);
$email_subject = "Customer Registration";
function died($error) {
// sese
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();
}
// siati data
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['pay_address1']) ||
!isset($_POST['pay_address2']) ||
!isset($_POST['pay_address3']) ||
!isset($_POST['pay_address4']) ||
!isset($_POST['pay_contact_no']) ||
!isset($_POST['email']) ||
!isset($_POST['password']) ||
!isset($_POST['security'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // manaomia
$last_name = $_POST['last_name']; // manaomia
$address1 = $_POST['pay_address1']; // manaomia
$address2 = $_POST['pay_address2']; // manaomia
$address3 = $_POST['pay_address3']; // manaomia
$address4 = $_POST['pay_address4']; // manaomia
$reference = $_POST['email']; // manaomia
$customer_pass = $_POST['password']; // manaomia
$contact_no = $_POST['pay_contact_no']; // pule oe
$security = $_POST['security']; // manaomia
$dbconn = pg_connect("host=127.0.0.1 dbname=ina user=suser password=ABCDEFG") or die('Could not connect: ' . pg_last_error());
$query = "INSERT INTO card (reference, first_name, last_name, address1, address2, address3, address4, contact_no, customer_pass) VALUES ($reference, $first_name, $last_name, $address1, $address2, $address3, $address4, $contact_no, $customer_pass)";
$result = pg_query($query);
if (!$result)
{
echo "Customer update failed!!";
} else
{
echo "Update was successfull; ";
}
pg_close();
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$reference)) {
$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($security) != 5) {
$error_message .= 'The Translation you entered do not appear to be valid. <br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Your login details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Login ID: ".clean_string($reference)."\n";
$email_message .= "Password: ".clean_string($password)."\n";
// email headers
$headers = 'From: ' . $from . "\r\n";
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
Thank you for registering. An email containing your login id and a password was sent to the provided email address. Please check your mail and then log in to continue.
NEXT Button
<?php
}
?>

$dbconn = pg_connect("host=127.0.0.1 dbname=ina user=suser password=ABCDEFG") or die('Could not connect: ' . pg_last_error());
$query = "INSERT INTO card ('reference'=>$reference, 'first_name'=>$first_name, 'last_name'=>$last_name, 'address1'=>$address1, 'address2'=>$address2, 'address3'=>$address3, 'address4'=>$address4, 'contact_no'=>$contact_no, 'customer_pass'=>$customer_pass,'security'=>$security)";
//or use where condition
//INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
//VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');

Related

php redirect after mail sent header error [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 7 years ago.
I have a form I got working fine but when I try and redirect after a form submission to a thank you page but get and error of " Cannot modify header information - headers already sent by ". I understand the headers are being modified in the mail() part but how do you do this without an error?
Code:
<?php
//if "email" variable is filled out, send email
if ( isset($_REQUEST['email']) ) {
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['phone'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
//Form Variables
//Personal Information
$subject = "Smile Guide";
$email = $_REQUEST['email'];
$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$phone = $_REQUEST['phone'];
$admin_email = "packy#ocularlogic.com,$email";
//Stage 1
$chipped = $_REQUEST['smile-fault-1'];
$color = $_REQUEST['smile-fault-2'];
$gaps = $_REQUEST['smile-fault-3'];
$worn = $_REQUEST['smile-fault-4'];
$crooked = $_REQUEST['smile-fault-5'];
$metal = $_REQUEST['smile-fault-6'];
//Stage 2
$aggressive = $_REQUEST['smile-prefered-1'];
$mature = $_REQUEST['smile-prefered-2'];
$vigorous = $_REQUEST['smile-prefered-3'];
$dominant = $_REQUEST['smile-prefered-4'];
$focused = $_REQUEST['smile-prefered-5'];
$enhanced = $_REQUEST['smile-prefered-6'];
$soften = $_REQUEST['smile-prefered-7'];
$hollywood = $_REQUEST['smile-prefered-8'];
$functional = $_REQUEST['smile-prefered-9'];
$natural = $_REQUEST['smile-prefered-10'];
$youthful = $_REQUEST['smile-prefered-11'];
$oval = $_REQUEST['smile-prefered-12'];
//Stage 3
$shade = $_REQUEST['smile-color'];
//List errors
$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)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$phone)) {
$error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
//The Message
$content = '<html><body><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td align="center">';
$content .= '<img src="http://smiletothemaxdentallab.com/guide/smile-logo.jpg" style="display: block; margin: 10px auto;">';
$content .= '<h1 style="color: #c51f4f;">Smile Guide</h1>';
$content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Client Information</h2><ul style='list-style-type: none; color:#999999;'>";
$content .= "<li>Name:".$last_name.",".$first_name."</li>";
$content .= "<li>Phone:".$phone."</li>";
$content .= "<li>Email:".$email."</li>";
$content .= "</ul>";
$content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Current Teeth Issues</h2><ul style='list-style-type: none; color:#999999;'>";
$content .= isset($_POST['smile-fault-1']) ? "<li>".$chipped."</li>" : "";
$content .= isset($_POST['smile-fault-2']) ? "<li>".$color."</li>" : "";
$content .= isset($_POST['smile-fault-3']) ? "<li>".$gaps."</li>" : "";
$content .= isset($_POST['smile-fault-4']) ? "<li>".$worn."</li>" : "";
$content .= isset($_POST['smile-fault-5']) ? "<li>".$crooked."</li>" : "";
$content .= isset($_POST['smile-fault-6']) ? "<li>".$metal."</li>" : "";
$content .= "</ul>";
$content .= "<h2 style='background-color:#c51f4f; color:#fff; padding:10px 0;'>Desired Color</h2><ul style='list-style-type: none; color:#999999;'>";
$content .= "<li>".$shade."</li>";
$content .= "</ul>";
$content .= "</td></tr><tr><td style='background-color:#F7F7F7; color:#999999; text-align:center; padding: 50px 0;'><p>Brought to you be AOC Dental Lab</p><p>Toll free (800) 729-1593</p><p>1724 E Sherman Ave | Coeur d’Alene, Idaho | 83814</p>";
$content .= "</td></tr></table></body></html>";
//Set up Header
$headers = "From: " . strip_tags($_POST['email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['email']) . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
//Send Email
$send = mail($admin_email, $subject, $content, $headers);
if($send) {
header("Location: http://website.com/smile-guide/thank-you");
}
}
?>
This error is being caused by the fact that you have already sent data to the client at the moment you are trying to send the headers (which should be sent first).
Check your page encoding. Probably it is UTF-8+ (with UTF-8 signature)
If not then you need to check white spaces.
Don't forget to exit after redirect
if($send) {
header("Location: http://website.com/smile-guide/thank-you");
exit();
}

PHP Mail form not generating mail [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
OK, my php email script is hosted on another server to my form. It has worked fine until recently, but a couple of unrelated changes seem to have buggered things up. I think I have an idea what might be going on, but let me explain the problem first.
At first I thought it was an issue with depricated !eregi commands. So, I changed them all to !preg_match, but that apparently wasn't the only issue.
The form appears to work, there are no errors being reported, and the success page is parsing, but no email is ever sent (yes, I checked my spam folder). Around the same time the issues started, I moved the website and domain that the original php mail script is hosted on to a different server that (one that I can't host php files on), but kept the php mail script on the old server. Of course, without the domain pointing to that web server the external referencing stopped working. So, I just dropped the file into a subdomain on the old server, and re-referenced the form accordingly. It now connects fine, and as I said, parses the script through to the success page.
The email hosting for this server was never changed. So, ththere shouldn't be an issue, but I think the problem is related to that domain name change. Any thoughts? Script and form address below:
<?php
ini_set("display_errors", "1");
error_reporting(E_ALL);
if(isset($_POST['email'])) {
// set the EMAIL TO options
$email_to = "jr#creativeheat.co.uk";
$email_bcc = "jr#creativeheat.co.uk";
$email_subject = "Website booking inquiry";
// grab referal info from POST
$path = explode('/', $_SERVER['HTTP_REFERER']);
$referer = $path[2];
// redirect to error page
function died($error) {
$path = explode('/', $_SERVER['HTTP_REFERER']);
$referer = $path[2];
header( 'Location: http://'.$referer.'/'.$error ) ;
exit;
}
// grab the checkbox values and change them to strings
if (isset($_POST['booking_0'])) { $book1 = $_POST['booking_0']; if( $book1 == 'Bedroom(s)') { $book1 = " The Bedroom(s) \n"; }} else {$book1 = "\n";}
if (isset($_POST['booking_1'])) { $book2 = $_POST['booking_1']; if( $book2 == 'Meeting Room') { $book2 = " The Meeting Room \n";}} else {$book2 = "\n";}
if (isset($_POST['booking_2'])) { $book3 = $_POST['booking_2']; if( $book3 == 'Barn') { $book3 = " The Barn \n"; }} else {$book3 = "\n";}
if (isset($_POST['booking_3'])) { $book4 = $_POST['booking_3']; if( $book4 == 'Campsite') { $book4 = " The Campsite \n";}} else {$book4 = "\n";}
// clear the ERRORTYPE & ERROR_MESSAGE variables
$errortype = "";
$error_message = "";
// then check for an all false in the checkbox group
if (!isset($_POST['booking_0']) && !isset($_POST['booking_1']) && !isset($_POST['booking_2']) && !isset($_POST['booking_3'])) {
// provided none of the checkboxes are ticked set the DIED function parameter to ERRORTYPE = BOOKINGERR
$error_message = 'error';
$errortype = 'bookingerr';
if(strlen($error_message) > 0) {
died($errortype) ;
}
// alternate bruteforce redirect to NO BOOKING TYPE SELECTED page
// header( 'Location: http://'.$referer.'/booking/'.$errortype ) ;
}
// check everything else
// reset the ERROR variables
$errortype = "";
$error_message = "";
// check the ISSET state of the remaining required fields
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['number']) ||
!isset($_POST['email']) ||
!isset($_POST['adults']) ||
!isset($_POST['children']) ||
!isset($_POST['from_date']) ||
!isset($_POST['to_date']) ||
!isset($_POST['disabled']) ||
!isset($_POST['parking']) ||
!isset($_POST['general'])) {
// redirect to GENERAL INVALIDATION page
$error_message = 'error';
$errortype = 'requirederror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
// set FIELD variables
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$telephone = $_POST['number']; // required
$email_from = $_POST['email']; // required
$adults = $_POST['adults']; // required
$children = $_POST['children']; // required
$fdate = $_POST['from_date']; // required
$tdate = $_POST['to_date']; // required
$disabled = $_POST['disabled']; // not required
$parking = $_POST['parking']; // not required
$comments = $_POST['general']; // not required
// begin INVALID field character checks
$email_exp = "/^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i";
$errortype = "";
$error_message = "";
if(!preg_match($email_exp,$email_from)) {
// redirect to INVALID EMAIL page
$error_message = 'error';
$errortype = 'emailinvalid';
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$string_exp = "/^[a-z .'-]+$/i";
$errortype = "";
$error_message = "";
if(!preg_match($string_exp,$first_name)) {
// redirect to INVALID FIRSTNAME page
$error_message = 'error';
$errortype = 'fnameerror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$errortype = "";
$error_message = "";
if(!preg_match($string_exp,$last_name)) {
// redirect to INVALID LASTNAME page
$error_message = 'error';
$errortype = 'lnameerror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$errortype = "";
$error_message = "";
if(strlen($comments) < 2 && strlen($comments) > 0) {
// redirect to INVALID COMMENTS page
$error_message = 'error';
$errortype = 'commentserror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$string_exp = "/^[0-9 .-]+$/i";
$errortype = "";
$error_message = "";
if(!preg_match($string_exp,$telephone)) {
// redirect to INVALID TELEPHONE page
$error_message = 'error';
$errortype = 'telephoneerror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
// failsafe
if(strlen($error_message) > 0) {
died($errortype) ;
}
// begin EMAIL MESSAGE creation
$email_message = "Form details below.\n\n";
// remove ILLEGAL data from submitted fields
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// set EMAIL_MESSAGE variable from data gathered from form
$email_message .= "Name: ".clean_string($first_name)." ".clean_string($last_name)."\n";
$email_message .= "Contact number: ".clean_string($telephone)."\n";
$email_message .= "Email address: ".clean_string($email_from)."\n\n";
$email_message .= "Interested in availability of the following: \n";
$email_message .= $book1.$book2.$book3.$book4."\n";
$email_message .= "Date from: ".clean_string($fdate)."\n";
$email_message .= "Date to: ".clean_string($tdate)."\n\n";
$email_message .= "Number of...\n";
$email_message .= "Adults: ".clean_string($adults)."\n";
$email_message .= "Children: ".clean_string($children)."\n\n";
$email_message .= "Disabled? ".clean_string($disabled)."\n";
$email_message .= "Parking? ".clean_string($parking)."\n\n";
$email_message .= "Additional Information: \n\n";
$email_message .= clean_string($comments);
// create EMAIL HEADERS
$headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n".'Bcc: '.$email_bcc."\r\n".'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
// redirect to SUCCESS page
header( 'Location: http://'.$referer.'/success' ) ;
exit;
}
?>
form address: http://www.claverhammeetinghouse.org.uk/booking/
EDIT:
After installing PHPmailer the code now looks like this:
<?php
// new
require_once('class.phpmailer.php');
//end new
ini_set("display_errors", "1");
error_reporting(E_ALL);
if(isset($_POST['email'])) {
// set the EMAIL TO options
$email_to = "jr#creativeheat.co.uk";
$email_bcc = "jr#creativeheat.co.uk";
$email_subject = "Website booking inquiry";
// grab referal info from POST
$path = explode('/', $_SERVER['HTTP_REFERER']);
$referer = $path[2];
// redirect to error page
function died($error) {
$path = explode('/', $_SERVER['HTTP_REFERER']);
$referer = $path[2];
header( 'Location: http://'.$referer.'/'.$error ) ;
exit;
}
// grab the checkbox values and change them to strings
if (isset($_POST['booking_0'])) { $book1 = $_POST['booking_0']; if( $book1 == 'Bedroom(s)') { $book1 = " The Bedroom(s) \n"; }} else {$book1 = "\n";}
if (isset($_POST['booking_1'])) { $book2 = $_POST['booking_1']; if( $book2 == 'Meeting Room') { $book2 = " The Meeting Room \n";}} else {$book2 = "\n";}
if (isset($_POST['booking_2'])) { $book3 = $_POST['booking_2']; if( $book3 == 'Barn') { $book3 = " The Barn \n"; }} else {$book3 = "\n";}
if (isset($_POST['booking_3'])) { $book4 = $_POST['booking_3']; if( $book4 == 'Campsite') { $book4 = " The Campsite \n";}} else {$book4 = "\n";}
// clear the ERRORTYPE & ERROR_MESSAGE variables
$errortype = "";
$error_message = "";
// then check for an all false in the checkbox group
if (!isset($_POST['booking_0']) && !isset($_POST['booking_1']) && !isset($_POST['booking_2']) && !isset($_POST['booking_3'])) {
// provided none of the checkboxes are ticked set the DIED function parameter to ERRORTYPE = BOOKINGERR
$error_message = 'error';
$errortype = 'bookingerr';
if(strlen($error_message) > 0) {
died($errortype) ;
}
// alternate bruteforce redirect to NO BOOKING TYPE SELECTED page
// header( 'Location: http://'.$referer.'/booking/'.$errortype ) ;
}
// check everything else
// reset the ERROR variables
$errortype = "";
$error_message = "";
// check the ISSET state of the remaining required fields
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['number']) ||
!isset($_POST['email']) ||
!isset($_POST['adults']) ||
!isset($_POST['children']) ||
!isset($_POST['from_date']) ||
!isset($_POST['to_date']) ||
!isset($_POST['disabled']) ||
!isset($_POST['parking']) ||
!isset($_POST['general'])) {
// redirect to GENERAL INVALIDATION page
$error_message = 'error';
$errortype = 'requirederror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
// set FIELD variables
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$telephone = $_POST['number']; // required
$email_from = $_POST['email']; // required
$adults = $_POST['adults']; // required
$children = $_POST['children']; // required
$fdate = $_POST['from_date']; // required
$tdate = $_POST['to_date']; // required
$disabled = $_POST['disabled']; // not required
$parking = $_POST['parking']; // not required
$comments = $_POST['general']; // not required
// begin INVALID field character checks
$email_exp = "/^[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}$/i";
$errortype = "";
$error_message = "";
if(!preg_match($email_exp,$email_from)) {
// redirect to INVALID EMAIL page
$error_message = 'error';
$errortype = 'emailinvalid';
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$string_exp = "/^[a-z .'-]+$/i";
$errortype = "";
$error_message = "";
if(!preg_match($string_exp,$first_name)) {
// redirect to INVALID FIRSTNAME page
$error_message = 'error';
$errortype = 'fnameerror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$errortype = "";
$error_message = "";
if(!preg_match($string_exp,$last_name)) {
// redirect to INVALID LASTNAME page
$error_message = 'error';
$errortype = 'lnameerror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$errortype = "";
$error_message = "";
if(strlen($comments) < 2 && strlen($comments) > 0) {
// redirect to INVALID COMMENTS page
$error_message = 'error';
$errortype = 'commentserror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
$string_exp = "/^[0-9 .-]+$/i";
$errortype = "";
$error_message = "";
if(!preg_match($string_exp,$telephone)) {
// redirect to INVALID TELEPHONE page
$error_message = 'error';
$errortype = 'telephoneerror' ;
if(strlen($error_message) > 0) {
died($errortype) ;
}
}
// failsafe
if(strlen($error_message) > 0) {
died($errortype) ;
}
// begin EMAIL MESSAGE creation
$email_message = "Form details below.\n\n";
// remove ILLEGAL data from submitted fields
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
// set EMAIL_MESSAGE variable from data gathered from form
$email_message .= "Name: ".clean_string($first_name)." ".clean_string($last_name)."\n";
$email_message .= "Contact number: ".clean_string($telephone)."\n";
$email_message .= "Email address: ".clean_string($email_from)."\n\n";
$email_message .= "Interested in availability of the following: \n";
$email_message .= $book1.$book2.$book3.$book4."\n";
$email_message .= "Date from: ".clean_string($fdate)."\n";
$email_message .= "Date to: ".clean_string($tdate)."\n\n";
$email_message .= "Number of...\n";
$email_message .= "Adults: ".clean_string($adults)."\n";
$email_message .= "Children: ".clean_string($children)."\n\n";
$email_message .= "Disabled? ".clean_string($disabled)."\n";
$email_message .= "Parking? ".clean_string($parking)."\n\n";
$email_message .= "Additional Information: \n\n";
$email_message .= clean_string($comments);
// create EMAIL HEADERS
$headers = 'From: '.$email_from."\r\n".'Reply-To: '.$email_from."\r\n".'Bcc: '.$email_bcc."\r\n".'X-Mailer: PHP/' . phpversion();
//new
new PHPMailer($email_to, $email_subject, $email_message, $headers);
//end new
// old #mail($email_to, $email_subject, $email_message, $headers);
//end old
// redirect to SUCCESS page
header( 'Location: http://'.$referer.'/success' ) ;
exit;
}
?>
Try using only "\n" instead of "\r\n" on $headers as documented in the PHP reference. http://php.net/manual/de/function.mail.php

How to make a list in a PHP contact form mandatory

I have an existing contact form that uses the PHP file below to send emails.
I need make the list of 'market_segment' mandatory. How can I modify this file so at least one option is selected, otherwise an error is shown?
Many thanks!
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email#email.com";
$email_subject = "Subject";
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['title']) ||
!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['market_segment']) ||
!isset($_POST['company_name']) ||
!isset($_POST['business_address1']) ||
!isset($_POST['business_address2']) ||
!isset($_POST['city']) ||
!isset($_POST['post_code']) ||
!isset($_POST['where']) ||
!isset($_POST['code'])) {
died('We are sorry, but there appears to be a problem with the form your submitted.');
}
$one = $_POST['one']; // not required
$two = $_POST['two']; // not required
$three = $_POST['three']; // not required
$four = $_POST['four']; // not required
$five = $_POST['five']; // not required
$title = $_POST['title']; // not required
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$market_segment = $_POST['market_segment']; // required
$company_name = $_POST['company_name']; // required
$business_address1 = $_POST['business_address1']; // required
$business_address2 = $_POST['business_address2']; // required
$city = $_POST['city']; // required
$post_code = $_POST['post_code']; // required
$current_yes = $_POST['current_yes']; // not required
$current_no = $_POST['current_no']; // not required
$future_yes = $_POST['future_yes']; // not required
$future_no = $_POST['future_no']; // not required
$where = $_POST['where']; // not required
$code = $_POST['code']; // 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 First Name you entered does not appear to be valid.<br />';
}
if(!eregi($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
$string_exp = "^[a-z0-9 .'-]+$";
if(!eregi($string_exp,$company_name)) {
$error_message .= 'The Company Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 0) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
$string_exp = "^[0-9 .-]+$";
if(!eregi($string_exp,$telephone)) {
$error_message .= 'The Telephone Number you entered does not appear to be valid.<br />';
}
$string_exp = "^7575$";
if(!eregi($string_exp,$code)) {
$error_message .= 'The Verification Code 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 .= "President Creme Brulee with Vanilla Pods: ".clean_string($one)."\n";
$email_message .= "President Creme Brulee with Salted Butter Caramel: ".clean_string($two)."\n";
$email_message .= "President Cheesecake: ".clean_string($three)."\n";
$email_message .= "Galbani Panna Cotta: ".clean_string($four)."\n";
$email_message .= "Galbani Tiramisu: ".clean_string($five)."\n";
$email_message .= "Title: ".clean_string($title)."\n";
$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 .= "Market Segment: ".clean_string($market_segment)."\n";
$email_message .= "Company Name: ".clean_string($company_name)."\n";
$email_message .= "Business Address 1: ".clean_string($business_address1)."\n";
$email_message .= "Business Address 2: ".clean_string($business_address2)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "Post Code: ".clean_string($post_code)."\n";
$email_message .= "Current Customer: ".clean_string($current_yes)."\n";
$email_message .= "Existing Customer: ".clean_string($current_no)."\n";
$email_message .= "Future Marketing Accepted: ".clean_string($future_yes)."\n";
$email_message .= "Future Marketing Declined: ".clean_string($future_no)."\n";
$email_message .= "Where: ".clean_string($where)."\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 -->
<link href="example.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body {
background-color: #FFF;
}
-->
</style>
yes
<?
}
?>
You can check if a variable is empty with this function:
bool empty ( mixed $var )
witch return a true if it's empty false otherwise, in your case:
// validation expected data exists
if(empty($_POST['first_name']) ||
empty($_POST['last_name']) ||
empty( $_POST['email']) ||
empty($_POST['telephone']) ||
empty($_POST['market_segment']) ||
empty($_POST['company_name']) ||
empty($_POST['company_name']) ||
empty($_POST['business_address1']) ||
empty($_POST['business_address2']) ||
empty($_POST['city']) ||
empty($_POST['post_code'])) {
died('We are sorry, but there appears to be a problem with the form your submitted.');
}

Two PHP forms on same page

I have two php forms on same page now the problem is I am calling them through an iframe but there captcha is not working though I am using the same script for both the forms with different input fields. Right now what happens is when we click first time on captcha and write correct captcha it take us to YOU HAVE ENTERED WRONG CAPTCHA and then we fill correct captcha then show us thanku...Why not it shows thanks for the first time when we enter correct captch??????
<?php
session_start();
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, $url);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
$tuData = curl_exec($tuCurl);
curl_close($tuCurl);
$userip = explode(',',$tuData);
$ipcountry = str_replace('"', '', $userip[3]);
include "libmail.php";
$errors = '';
//print_r($_POST);
if(isset($_POST['email']))
{
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['captcha']) != 0)
{
$errors .= "You have entered wrong captcha code!";
}elseif($_FILES["userfile"]["size"] > 1048576)
{
$errors .= "You can upload maximum of 800kb file!";
}else{
$productsq = $_POST['productsq'];
$name = $_POST['name'];
$position = $_POST['position'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$companyweb = $_POST['companyweb'];
$address = $_POST['address'];
$country = $_POST['country'];
$brief = $_POST['brief'];
$email = $_POST['email'];
$captcha = $_POST['captcha'];
$sender = $contact_email;
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
if(trim($productsq) !='')
$email_message .= "*I'm interested in : ".clean_string($productsq)."\n"."\n";
if(trim($name) !='')
$email_message .= "Full Name: ".clean_string($name)."\n"."\n";
if(trim($position) !='')
$email_message .= "Position/Title: ".clean_string($position)."\n"."\n";
if(trim($phone) !='')
$email_message .= "Phone: ".clean_string($phone)."\n"."\n";
if(trim($company) !='')
$email_message .= "Company Name: ".clean_string($company)."\n"."\n";
if(trim($companyweb) !='')
$email_message .= "Website URL: ".clean_string($companyweb)."\n"."\n";
if(trim($address) !='')
$email_message .= "Full Address: ".clean_string($address)."\n"."\n";
if(trim($country) !='')
$email_message .= "Country: ".clean_string($country)." (IP Address) : $ipcountry ".$_SERVER['REMOTE_ADDR']."\n"."\n";
if(trim($brief) !='')
$email_message .= "About Myself : ".clean_string($brief)."\n"."\n";
$random = mt_rand();
$m= new Mail; // create the mail
$m->From( $name."<$email>" );
$m->To( "abc#gmail.com" );
$m->Subject( "Form2 - ".$random );
$m->Body( $email_message);
$m->Priority(2) ;
if($_FILES["userfile"]["tmp_name"]){
move_uploaded_file($_FILES["userfile"]["tmp_name"], 'uploadedfiles/'.$_FILES["userfile"]["name"]);
$file_upload = 'uploadedfiles/'.$_FILES["userfile"]["name"];
$m->Attach( $file_upload) ;
}
$m->Send();
header('location:thankyou.php');
if($_FILES["userfile"]["tmp_name"]){
unlink($file_upload);
}
}
}
?>
A captcha image code is usually stored in a session variable. When you display the second form, you are overwriting the captcha from the first form.

Why am I getting an error for only some form fields?

I recently uploaded an html form with its php script to the godaddy server, and after filling out the form fields, I received an error in which there was an "unexpected ';'" at the end of a set of lines. After some troubleshooting, help from a family member who programs, and scouring the PHP.net web site, I changed the "||"s to "&&"s and the error no longer occurred. However, I received a different error, and had not made any other changes than the ones I described. It is this error:
We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.
"The Email Address you entered does not appear to be valid.
The First Name you entered does not appear to be valid.
The Address you entered does not appear to be valid.
The Zip Code you entered does not appear to be valid.
The Email you entered does not appear to be valid.
The Email (in the verify field) you entered does not appear to be valid.
The Phone Number you entered does not appear to be valid.
The Cell Number you entered does not appear to be valid.
The Fax Number you entered does not appear to be valid.
The Time Frame you entered does not appear to be valid.
The Quantity does not appear to be valid.
The Specific Units you entered does not appear to be valid.
The Requested Completion Date you entered does not appear to be valid.
The Billing Address you entered does not appear to be valid.
The Billing Zip Code you entered does not appear to be valid.
Please go back and fix these errors.
I checked my html for max length in characters, and the other settings for the form fields that appear to be invalid, and none of the settings are broken or misconfigured as far as I can tell. Here is the HTML and PHP for those lines (do you need any more information?):
Link to HTML form:
PHP code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "presto423#yahoo.com";
$email_subject = "Request Form Data submitted by...";
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['fname']) &&
!isset($_POST['minit']) &&
!isset($_POST['lname']) &&
!isset($_POST['cname']) &&
!isset($_POST['address']) &&
!isset($_POST['city']) &&
!isset($_POST['state']) &&
!isset($_POST['zipcode']) &&
!isset($_POST['email_from']) &&
!isset($_POST['vemail']) &&
!isset($_POST['phone']) &&
!isset($_POST['cell']) &&
!isset($_POST['fax']) &&
!isset($_POST['workbefore']) &&
!isset($_POST['referral']) &&
!isset($_POST['services']) &&
!isset($_POST['timeframe']) &&
!isset($_POST['budget']) &&
!isset($_POST['position']) &&
!isset($_POST['quantity']) &&
!isset($_POST['units']) &&
!isset($_POST['specunits']) &&
!isset($_POST['reqcompdate']) &&
!isset($_POST['comments']) &&
!isset($_POST['baddress']) &&
!isset($_POST['bcity']) &&
!isset($_POST['bstate']) &&
!isset($_POST['bzipcode']))
$fname = $_POST["fname"];
$minit = $_POST["minit"];
$lname = $_POST["lname"];
$cname = $_POST["cname"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
$zipcode = $_POST["zipcode"];
$email_from = $_POST["email_from"];
$vemail = $_POST["vemail"];
$phone = $_POST["phone"];
$cell = $_POST["cell"];
$fax = $_POST["fax"];
$workbefore = $_POST["workbefore"];
$referral = $_POST["referral"];
$services = $_POST["services"];
$timeframe = $_POST["timeframe"];
$budget = $_POST["budget"];
$position = $_POST["position"];
$quantity = $_POST["quantity"];
$units = $_POST["units"];
$specunits = $_POST["specunits"];
$reqcompdate = $_POST["reqcompdate"];
$comments = $_POST["comments"];
$baddress = $_POST["baddress"];
$bcity = $_POST["bcity"];
$bstate = $_POST["bstate"];
$bzipcode = $_POST["bzipcode"];
$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,$fname)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$minit)) {
$error_message .= 'The Middle Initial you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$lname)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$cname)) {
$error_message .= 'The Company Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$address)) {
$error_message .= 'The Address 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,$zipcode)) {
$error_message .= 'The Zip Code you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$email_from)) {
$error_message .= 'The Email you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$vemail)) {
$error_message .= 'The Email (in the verify field) you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$phone)) {
$error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$cell)) {
$error_message .= 'The Cell Number you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$fax)) {
$error_message .= 'The Fax Number you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$workbefore)) {
$error_message .= 'Your entry does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$referral)) {
$error_message .= 'The Referred By you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$timeframe)) {
$error_message .= 'The Time Frame you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$position)) {
$error_message .= 'The Position you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$quantity)) {
$error_message .= 'The Quantity does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$units)) {
$error_message .= 'The Units you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$specunits)) {
$error_message .= 'The Specific Units you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$reqcompdate)) {
$error_message .= 'The Requested Completion Date 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(!preg_match($string_exp,$baddress)) {
$error_message .= 'The Billing Address you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$bcity)) {
$error_message .= 'The Billing City you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$bstate)) {
$error_message .= 'The Billing State you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$bzipcode)) {
$error_message .= 'The Billing Zip Code 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 .= "First Name: ".clean_string($fname)."\n";
$email_message .= "Middle Initial: ".clean_string($minit)."\n";
$email_message .= "Last Name: ".clean_string($lname)."\n";
$email_message .= "Company Name: ".clean_string($cname)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "State: ".clean_string($state)."\n";
$email_message .= "Zip Code: ".clean_string($zipcode)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Verify Email: ".clean_string($vemail)."\n";
$email_message .= "Phone: ".clean_string($phone)."\n";
$email_message .= "Cell: ".clean_string($cell)."\n";
$email_message .= "Fax: ".clean_string($fax)."\n";
$email_message .= "Have you ever worked with Home Turf before? ".clean_string($workbefore)."\n";
$email_message .= "How did you hear about Home Turf? ".clean_string($referral)."\n";
$email_message .= "Services Being Requested: ".clean_string($services)."\n";
$email_message .= "Time Frame: ".clean_string($timeframe)."\n";
$email_message .= "Budget Range: ".clean_string($budget)."\n";
$email_message .= "Your Position: ".clean_string($position)."\n";
$email_message .= "Quantity: ".clean_string($quantity)."\n";
$email_message .= "Units: ".clean_string($units)."\n";
$email_message .= "other units, please specify: ".clean_string($specunits)."\n";
$email_message .= "Requested Completion Date: ".clean_string($reqcompdate)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
$email_message .= "Billing Address: ".clean_string($baddress)."\n";
$email_message .= "Billing City: ".clean_string($bcity)."\n";
$email_message .= "Billing State: ".clean_string($bstate)."\n";
$email_message .= "Billing Zip Code: ".clean_string($bzipcode)."\n";
$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.
<?php
}
?>
Thank you for your time and consideration.
Two things I spot:
You should not have changed the || to &&. The if condition checks to see if any of those fields is empty, and you caused it to check if all of them are empty.
Following that, the whole next block should be enclosed in {}, OR a block of code is missing. It appears that if any of the fields is empty (!isset()), some error condition is executed, and if all of them are filled in, it should proceed to the last long block and validate each.
.
// Possibly something like:
// validation expected data exists
if(!isset($_POST['fname']) ||
!isset($_POST['minit']) ||
!isset($_POST['lname']) ||
...
...
!isset($_POST['bstate']) ||
!isset($_POST['bzipcode']))
{
// Echo an error that all fields must be filled in.
}
else
{
// All were filled in, execute the rest and validate
$fname = $_POST["fname"];
$minit = $_POST["minit"];
$lname = $_POST["lname"];
$cname = $_POST["cname"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
// etc...
}
That must be some form! :-)
Seriously, it would probably help you to reduce the size of this code. For a start, why not store those expected variable names in an array:
$vars = array('fname', 'minit', ...);
which you can then use to a) check they've been sent:
foreach ($vars as $var)
{
if (!isset($var))
{
// Do whatever happens if at least one value hasn't been sent
}
}
b) set those local variables:
foreach ($vars as $var)
{
$$var = $_POST[$var];
}
(although, this really isn't necessary; why not just refer to $_POST['fname'], etc.?)
c) in conjunction with another array, or a more sophisticated initial one, handle the validation, e.g.
define('RE_STRING', "/^[A-Za-z .'-]+$/");
$vars = array
(
'fname' => array('type' => RE_STRING, 'full' => 'First Name'),
...
);
foreach ($vars as $var => $details)
{
if(!preg_match($details['type'], $_POST[$var])) {
$error_message .= 'The '.$details['full'].' you entered does not appear to be valid.<br />';
}
}
d) build the email message:
foreach ($vars as $var => $details)
{
$email_message .= $details['full'].": ".clean_string($_POST[$var])."\n";
}
If you combine that all into a single foreach loop, you'll save yourself a lot of code and reduce the chance of a tiny typo causing a lot of headaches.

Categories