Jump to content


PHP Form validation


2 replies to this topic

#1 jm2008

    Young Padawan

  • Members
  • Pip
  • 6 posts

Posted 22 March 2008 - 08:10 AM

Hi Guys,

Currently I've been working on a form I've got it validating client side but I want server side also.

Now I'm not overly confident nor familiar with PHP, so I think i may have it set up correctly.

Here is the code:

<?php
	
	// Check To See If Form Was Submitted Correctly
		if(isset($_POST['submit'])) {
		
		$valid = 1;

	// Variables
		$name = $_POST['name'];
		$email = $_POST['email'];
		$telephone = $_POST['telephone'];
		$topic = $_POST['topic'];
		$budget = $_POST['budget'];
		$description = $_POST['description'];
			
		if ( $name == "Name..." || " " ) {
			$valid = 0;
			$error1 = '<img src="images/errorformicon.gif" alt="Error Form Icon" />';		
		}
	
		if ( $email == "E-mail..." ) {
			$valid = 0;
			$error2 = '<img src="images/errorformicon.gif" alt="Error Form Icon" />';
		}
		
		else {
			$valid = 1;
		}
		
		if ( $valid == 1 ) {
		
	// The E-mail Message
		$mailmsg = 'The following E-mail has been sent to you via your contact form:' . "\n\n";
		$mailmsg.= 'Name: ' . $name . "\n\n";
		$mailmsg.= 'Email: ' . $email . "\n\n";
		$mailmsg.= 'Telephone: ' . $telephone . "\n\n";
		$mailmsg.= 'Topic: ' . $topic . "\n\n";
		$mailmsg.= 'Budget: ' . $budget . "\n\n";
		$mailmsg.= 'Project Description: ' . $description . "\n\n";
		}
		
	// Sending the E-mail
		if(mail('mail@domain.com',$topic,$mailmsg,"From: $email")) {
		header("Location: thankyou.php");
			}
		}
?>

Now the email is formatting correctly however it doesn't appear to be checking if the form is valid before sending the email, it just sends it no matter the input.

Any ideas?

Thanks,

PS. Please ignore stuff like == "Name..." this will be changed, I'm just trying to get it working correctly first.

Edited by jm2008, 22 March 2008 - 08:11 AM.


#2 tones

    Young Padawan

  • Members
  • Pip
  • 10 posts

Posted 23 March 2008 - 02:11 AM

if (empty($name)) {

$valid = 0;

$error1 = 'error here'; 

}



if (empty($email)) {

$valid = 0;

$error2 = 'error here';

}



else {

$valid = 1;

}


Give that a try

#3 Korndawg

    Young Padawan

  • Members
  • Pip
  • 111 posts
  • Gender:Male
  • Location:Texas, USA

Posted 23 March 2008 - 07:01 PM

This isn't going to fix the validation part, but it will make sure the email isn't send unless $valid == 1...

if ( $valid == 1 ) {
		
	// The E-mail Message
		$mailmsg = 'The following E-mail has been sent to you via your contact form:' . "\n\n";
		$mailmsg.= 'Name: ' . $name . "\n\n";
		$mailmsg.= 'Email: ' . $email . "\n\n";
		$mailmsg.= 'Telephone: ' . $telephone . "\n\n";
		$mailmsg.= 'Topic: ' . $topic . "\n\n";
		$mailmsg.= 'Budget: ' . $budget . "\n\n";
		$mailmsg.= 'Project Description: ' . $description . "\n\n";

	// Sending the E-mail
		if(mail('mail@domain.com',$topic,$mailmsg,"From: $email")) {
		header("Location: thankyou.php");
			}
		}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users