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

Link to comment
Share on other sites

Forgot how to do comparisons?

 

if($_GET['error'] = 'noName')

That is just an assignment. It's always true unless you're assigning empty values. Use 2 or 3 equal signs for comparison. You have a bunch of these IF statements. Fix them all.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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 = "ty@leaderexcelsolutions.com";
		//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') ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.