Jump to content

PHP Same Page Form Validation


morocco-iceberg

Recommended Posts

Ok, so I was going smoothly until all of a sudden I started getting parse errors on line 21. I have no clue what is wrong with my code, so any help is very muchly appreciated!

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>File Writing</title>
</head>

<body>
<?php
if(isset($_POST['submit'])){		
	$name=$_POST['name'];
	$email=$_POST['email'];
	$message=$_POST['message'];

	$to="[email protected]";
	$re=$_POST['subject'];
	$msg = "$message \r\n \r\n $name, \r\n $email";

	if(!$name){
		$errn="Please enter your name.";
	}else if(!$email){
		$erre="Please enter an email.";
	}else if(!$re){
		$errr="Please enter a subject.";
	}else if(!$message){
		$errm="Please enter a message.";
	}else{
		$email = trim($email);
		$_ename = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_'{|}~]+";
		$_host = "([0-9A-Z]+\.)+";
		$_tlds = "([0-9A-Z]){2,4}$/i";

		if ( !preg_match($_ename."@".$_host.$_tlds,$email)){
			echo "Sorry, it appears the e-mail address you have entered is not valid. Please enter a valid e-mail address.";
		}else{
			mail($to, $re, $msg);
			echo "Message sent.";
		}
	}
}
?>

<form action="file.php" method="post">
<strong>Name</strong>
<input type="input" name="name" value="<?php if(isset($_POST['submit'])){echo $name;} ?>" /><?php if(isset($_POST['submit'])){echo $errn;} ?><br/>
<strong>E-Mail</strong>
<input type="input" name="email" value="<?php if(isset($_POST['submit'])){echo $email;} ?>" /><br/><?php if(isset($_POST['submit'])){echo $erre;} ?>
<strong>Subject</strong>
<input type="input" name="subject" value="<?php if(isset($_POST['submit'])){echo $re;} ?>" /><br/><?php if(isset($_POST['submit'])){echo $errr;} ?>
<strong>Message</strong>
<input type="input" name="message" value="<?php if(isset($_POST['submit'])){echo $message;} ?>" /><br/><br/><?php if(isset($_POST['submit'])){echo $errm;} ?>
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/195562-php-same-page-form-validation/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.