Page 1 of 1

HELP what is wrong with my code?

Posted: Tue Sep 07, 2010 10:03 pm
by Mikephp

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
</head>
<style>
.basictext {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	color:#000066;
}
.errortext {
	font-family: Arial, Helvetica, sans-serif;
	font-size: 14px;
	color:#C00000;
	font-weight: bold;
}
</style>
<body>
<?php

echo phpversion();

$error_text_firstname = "basictext";
$error_text_lastname = "basictext";
$error_text_email = "basictext";

if (isset($_POST['validate_user_data_button'])){

	$email_pattern = '/.*@.*\..*/';
	$firstname_pattern = '/[^A-Za-z-]/';
	$lastname_pattern = '/[^A-Za-z-]/';
	
	$user_firstname = trim($_POST['first_name']);
	$user_lastname = trim($_POST['last_name']);
	$user_email = trim($_POST['email_address']);

//	echo "Variables FIRST NAME___" . "$user_firstname" . "__LAST NAME__" . "$user_lastname" . "__EMAIL__" . "$user_email";
	
	if (preg_match($lastname_pattern, $user_lastname) > 0 )
	{
			$error_text_lastname = "basictext";
	}
	else
	{
			$errot_text_lastname = "errortext";
	}

	if (preg_match ($email_pattern, $user_email) > 0) 	
	{
			$error_text_email = "basictext";	
	}
	else
	{
			$error_text_email = "errortext";
	}
	

	if (preg_match($firstname_pattern, $user_firstname) > 0)
	{

			$error_text_firstname = "basictext";
	}
	else
	{

			$error_text_firstname = "errortext";
	}
	
		if ($error_text_firstname == "basictext" && $error_text_lastname == "basictext" && $error_text_email == "basictext")
		echo "this is where to store stuff into database";	
}
?>

<form action="validate_dbstore_user_data.php" method="post">

  <span class="<?php print $error_text_firstname; ?>">Firts name:</span>
  <input type="text" name="first_name" class="<?php print $error_text_firstname; ?>">
  <br>
  
  <span class="<?php print $error_text_lastname; ?>">Last name:</span>
  <input type="text" name="last_name" class="<?php print $error_text_lastname; ?>">
  <br>
  
  <span class="<?php print $error_text_email; ?>">E-mail address:</span>
  <input type="text" name="email_address" class="<?php print $error_text_email; ?>">
  <br>
  
  <input type="submit" name="validate_user_data_button" value="Submit.">
</form>
</body>
</html>

Re: HELP what is wrong with my code?

Posted: Tue Sep 07, 2010 10:08 pm
by Jonah Bron

Re: HELP what is wrong with my code?

Posted: Tue Sep 07, 2010 10:11 pm
by Mikephp
My problem are the if statements they are identical but they work completely different! I have no idea what is wrong with the code.

Re: HELP what is wrong with my code?

Posted: Tue Sep 07, 2010 10:14 pm
by Jonah Bron
What do you mean "don't work"? Do you mean that in doesn't validate input like you expect it to?

After a quick peek at the code, (just a wild guess) it looks like you need to replace

Code: Select all

$firstname_pattern = '/[^A-Za-z-]/';
$lastname_pattern = '/[^A-Za-z-]/';
With

Code: Select all

$firstname_pattern = '/^[A-Za-z-]+$/';
$lastname_pattern = '/^[A-Za-z-]+$/';

Re: HELP what is wrong with my code?

Posted: Tue Sep 07, 2010 10:20 pm
by Mikephp
Thanks, yes but that was not my main problem.

... and OMG!!! it was this
$errot_text_lastname = "errortext";
errot <- was suppose to be error