Jump to content

quick problem with code that worked once.. now doesnt


zang8027

Recommended Posts

I have dont many of these but i do not see why this is not working. Its a simple form validation. It simply posts to a process page that checks to see if everything is filled out. It is showing "noName" no matter what and all my errors are being echoed.. as if the "if statement" is not working

 

<form action='processEmail.php' method='get'>

<p>Your Name * <?php
if(isset($_GET['error']))
{
	if($_GET['error'] = 'noName')
	{
		echo "<span class='redText'>You must enter a name</span>";
	}
}
?></p>
<input type='text' name='uName' class='for'/>

<p>Your Email * <?php
if(isset($_GET['error']))
{
	if($_GET['error'] = 'noEmail')
	{
		echo "<span class='redText'>You must enter an email</span>";
	}
	elseif($_GET['error']='invalid')
	{
		echo "<span class='redText'>You must enter a valid email</span>";
	}
}
?></p>
<input type='text' name='email' class='for' />

<p>Type a Message * <?php
if(isset($_GET['error']))
{
	if($_GET['error'] = 'noComment')
	{
		echo "<span class='redText'>You must enter a Message</span>";
	}
}
?></p>
<textarea name='uMsg' class='for'></textarea>
<br/>
<br/>
<input type='submit' value='Send it!'/>
</form>

 

and my process page:

 

<?php
//grab variables being passed and put them into usable variables
$name = $_GET['uName'];
$email = $_GET['email'];
$text = $_GET['uMsg'];

//Validate each field for being blank or wrong character
if($_GET['name'] == "")
{
	header("Location:contact.php?error='noName'");
} 
elseif($_GET['email'] == "") 
{
	header("Location:contact.php?error='noEmail'");
}
elseif($_GET['comment'] == "") 
{
	header("Location:contact.php?error='noText'");
}
elseif(!preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
{
	header("Location:contact.php?error='invalid'");
}else{
//If all checks out, send the email

Except for the 'email' field, none of your other form field names match the names you are using in the tests.

 

For example, form field - uName and testing code - if($_GET['name'] == "")

 

You in fact have a statement $name = $_GET['uName']; but $name is then not being used.

 

You need to go through your code and check it for consistency.

<?php
//grab variables being passed and put them into usable variables
$name = $_GET['uName'];
$email = $_GET['email'];
$text = $_GET['uMsg'];

//Validate each field for being blank or wrong character
if($_GET['uName'] == "")
{
	header("Location:contact.php?error='noName'");
} 
elseif($_GET['email'] == "") 
{
	header("Location:contact.php?error='noEmail'");
}
elseif($_GET['uMsg'] == "") 
{
	header("Location:contact.php?error='noText'");
}
elseif(!preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $email))
{
	header("Location:contact.php?error='invalid'");
}else{
//If all checks out, send the email


	//------------------Email-----------------


		//define the receiver of the email
		$to = "[email protected]";
		//define the subject of the email
		$subject = 'A message from'.$name.'via www.GregSReed.com';
		//define the message to be sent. Each line should be separated with \n
		nl2br($EmailBody = $text);

		$headers  = "MIME-Version: 1.0\r\n"; 
		$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
		$date 			= date("m/d/Y H:i:s");
		$EmailFooter	= "<br />Sent: $date<br /><br />";
		$Message 		= $EmailBody.$EmailFooter;
		$ok = mail($to, $subject, $Message, $headers . "From:$name <".$to.">");

		if($ok){
		header("Location:contact.php?status=Success");
		}else{

		}


} //End the else all validated
?>

 

 

<?php include('header.php') ?>
<div id='contact'>
<?php
if(isset($_GET['status']))
{
	if($_GET['status'] == 'success')
	{
		print "<h2>Success! Your message was sent.</h2>";
	}
}
?>
<p class='yellowText'>Got a question or comment? Simply fill out the form below!</p>

<form action='processEmail.php' method='get'>

<p>Your Name * <?php
if(isset($_GET['error']))
{
	if($_GET['error'] == 'noName')
	{
		print "<span class='redText'>You must enter a name</span>";
	}
}
?></p>
<input type='text' name='uName' class='for'/>

<p>Your Email * <?php
if(isset($_GET['error']))
{
	if($_GET['error'] == 'noEmail')
	{
		echo "<span class='redText'>You must enter an email</span>";
	}
	elseif($_GET['error'] =='invalid')
	{
		echo "<span class='redText'>You must enter a valid email</span>";
	}
}
?></p>
<input type='text' name='email' class='for' />

<p>Type a Message * <?php
if(isset($_GET['error']))
{
	if($_GET['error'] == 'noComment')
	{
		echo "<span class='redText'>You must enter a Message</span>";
	}
}
?></p>
<textarea name='uMsg' class='for'></textarea>
<br/>
<br/>
<input type='submit' value='Send it!'/>
</form>
</div>
<?php include('footer.php') ?>

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.