Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

help with php form

Posted on 2011-02-17
4
Medium Priority
?
248 Views
Last Modified: 2012-08-14
Hi all. This form worked fine when I just needed the first name and email validated. I now need first name, last name and email validated. Please help. PHP code attached.
<?php
if(isset($_POST['email'])) {
	
	// EDIT THE 2 LINES BELOW AS REQUIRED
	$email_to = "me@myemail.com";
	$email_subject = "Test";
		
	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['firstName']) ||
		!isset($_POST['email'])) {
		died('We are sorry, but there appears to be a problem with the form your submitted.');		
	}
	
	$firstName = $_POST['firstName']; // required
	$lastName = $_POST['lastName']; // required
	$email_from = $_POST['email']; // required
	$company = $_POST['company']; // required
	$phone = $_POST['phone']; // required
	$source = $_REQUEST['source'];
		
	
	$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 .= "Last Name: ".clean_string($lastName)."\n";
	$email_message .= "Email: ".clean_string($email_from)."\n";
	$email_message .= "Company: ".clean_string($company)."\n";
	$email_message .= "Phone: ".clean_string($phone)."\n";
	$email_message .= "Source: ".clean_string($source)."\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);  
      header("Location: thanks.html");
}
?>

Open in new window

0
Comment
Question by:tekgrl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:AriMc
ID: 34919894
If by validation you mean you need to check it is set, like you do for the first name and e-mail, then this could be what you are looking for:

// validation expected data exists
if(!isset($_POST['firstName']) || !isset($_POST['lastName'])
   !isset($_POST['email'])) {
   died('We are sorry, but there appears to be a problem with the form your submitted.');            
      }

0
 
LVL 9

Accepted Solution

by:
AriMc earned 2000 total points
ID: 34919957
Sorry, forgot one OR operator, here's a corrected version:

// validation expected data exists
if(!isset($_POST['firstName']) || !isset($_POST['lastName']) ||
   !isset($_POST['email'])) {
   died('We are sorry, but there appears to be a problem with the form your submitted.');            
      }
0
 

Author Comment

by:tekgrl
ID: 34920017
That's it! Thanks.
0
 

Author Closing Comment

by:tekgrl
ID: 34920020
Beautiful. Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question