Jump to content

Show user errors on the web page when requiered fields are empty


Recommended Posts

I am fairly new to php, altho do have 1 years experience in php coding.

 

I am having great difficulty  in trying to sort out a problem.

 

I am making a simple registration form.

If the user doesnt fill out the requiered fields, how am i supposed to bring errors back for this?

currently, i have this and it doesnt seem to be doing what its supposed to do:

 

switch ($_POST["submit"]) {
case "Register":
	if(empty($_POST["firstName"])) { $errors[] = "please insert a name"; }
                //some sql goes here
                break;
}

 

some of the html code

<div id="register">
<h2>Register</h2>
<?php 
foreach($errors as $error) {
    echo $error;
}
?>

<?php
$errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : '');

//...then...

?>

<form>
<?php echo (!empty($errors['first_name']) ? $errors['first_name'] .'<br />' : ''); ?>
<input type="firstName" />
</form>

you get the idea?

but the page seems to reload

 

Your form is probably not submitting the data you think it is or you have some other logic error in your code. You are having a page-wide problem. It would take seeing the code on that page to be able to help with what it is or is not doing.

in case you did a copy paste of my code, there is an error in the input tag:

 

<input type="firstName" />

 

should at least be:

 

<input name="firstName" type="text" />

 

i had type="firstName" ..  i fear my code gets taken too literally sometimes.  had to make sure and clear that up.

here is the page that i have...

 

<?php
include($_SERVER['DOCUMENT_ROOT'] . "kers/includes/session-set.php");

if (!isset($_POST["submit"])){
$_POST["submit"] = "";
}

$errors = array();

switch ($_POST["submit"]) {
case "Register":
	$errors['first_name'] = (empty($_POST['emailAddress']) ? 'Please enter a name.' : '');

	if(!$errors) { 
		$sqlInsertMember = "
		INSERT INTO member (firstName, lastName, emailAddress, password)
		VALUES ('" . mysql_real_escape_string($_POST["firstName"]) . "', 
		'" . mysql_real_escape_string($_POST["lastName"]) . "', 
		'" . mysql_real_escape_string($_POST["emailAddress"]) . "', 
		'" . mysql_real_escape_string(sha1($_POST["password"])) . "', 
		";

		mysql_query($sqlInsertMember);

		Header("Location: /kers/account/");
	}
	break;
}
?>
<!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>Kapil Gohil</title>
</head>
<body>
	<div id="main-body">
		<h2>Register</h2>
		<form name="input" action="" method="post">
			<?php echo (!empty($errors['first_name']) ? $errors['first_name'] .'<br />' : ''); ?>
			<p>First Name:</p>
			<input class="input-field" type="text" name="firstName" />
			<p>Last Name:</p>
			<input class="input-field" type="text" name="lastName" />
			<p>Email Address:</p>
			<input class="input-field" type="text" name="emailAddress" />
			<p>Password:</p>
			<input class="input-field" type="password" name="password" />
			<input type="submit" name="submit" value="Register" />
		</form>
	</div>
</body>
</html>

couple things:

 

what is this:

 

if (!isset($_POST["submit"])){
$_POST["submit"] = "";
}

 

and you are mishandling this:

 

$errors['first_name'] = (empty($_POST['emailAddress']) ? 'Please enter a name.' : '');

 

as you are assigning $errors['first_name'] if $_POST['emailAddress'] is empty.

The code you posted is doing what the logic in it says to do. It echoes Please enter a name. when you don't put anything in the email field (which I'll assume is logically that way just because you are trying get anything to work at this point.)

 

You do have a problem with your setting of the $errors elements. You are setting them to an empty string when there is no error. This however will cause if(!$errors){ to always skip the database query code. Don't set the $errors element at all if the validation test passes.

i believe you are just over-complicating your code by using a switch(), unless you are planning on having multiple submit buttons?

 

get rid of this:

 

if (!isset($_POST["submit"])){
$_POST["submit"] = "";
}

 

instead, do this:

 

<?php
include($_SERVER['DOCUMENT_ROOT'] . "kers/includes/session-set.php");

$errors = array();

if (isset($_POST['submit']))
{
$errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : '');

if (!is_array($errors))
{
	$sqlInsertMember = "
		INSERT INTO member (firstName, lastName, emailAddress, password)
		VALUES ('" . mysql_real_escape_string($_POST["firstName"]) . "', 
		'" . mysql_real_escape_string($_POST["lastName"]) . "', 
		'" . mysql_real_escape_string($_POST["emailAddress"]) . "', 
		'" . mysql_real_escape_string(sha1($_POST["password"])) . "', 
	";

	if ($res = mysql_query($sqlInsertMember))
	{
		header('Location: /kers/account/');
		exit(0);
	}
}
}
?>
<!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>Kapil Gohil</title>
</head>
<body>
	<div id="main-body">
		<h2>Register</h2>
		<form name="input" action="" method="post">
			<?php echo (!empty($errors['first_name']) ? $errors['first_name'] .'<br />' : ''); ?>
			<p>First Name:</p>
			<input class="input-field" type="text" name="firstName" />
			<p>Last Name:</p>
			<input class="input-field" type="text" name="lastName" />
			<p>Email Address:</p>
			<input class="input-field" type="text" name="emailAddress" />
			<p>Password:</p>
			<input class="input-field" type="password" name="password" />
			<input type="submit" name="submit" value="Register" />
		</form>
	</div>
</body>
</html>

 

is a start;  can obviously be improvements;

An assignment statement will always create a variable if it does not already exist, even if NULL is used as the value. Using logic like $errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : ''); will always create the array entry.

 

To keep it simple and avoid adding extra logic to test if the errors array is actually empty, you will need to use an actual if(){} statement -

		if(empty($_POST['first_name'])){
		$errors['firstName'] = 'Please enter a name.';
	}

Using logic like $errors['first_name'] = (empty($_POST['firstName']) ? 'Please enter a name.' : ''); will always create the array entry.

 

man, you totally right.  it's been a loooong day.  i was just trying to keep things short with the ternary, but my brain just wasn't making the connection.

 

good call.

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.