3 Replies - 1054 Views - Last Post: 09 February 2013 - 02:00 PM Rate Topic: -----

#1 josefroyadvincula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-December 12

Form Validation Error Message

Posted 09 February 2013 - 05:59 AM

Hi guys!

I've been doing some tests with form validation. I have 3 textboxes, all of which need to have data. Whenver the user clicks submit, the form validates on the same page. If no problem was found, it would continue on to insert.php. However, if one or more textbox is left blank, the blank textbox would display a message like "Please enter a first name". It works out fine. The trouble is, those with text lose their text. I'm not sure if you can imagine what I mean, but anyway, here's the code:

<?php
	session_start();
	
	$fname_errormsg = '';
	$mname_errormsg = '';
	$lname_errormsg = '';
	
	if (isset($_POST['submit'])) {
		if (!empty($_POST['fname'])){
			$_SESSION['fname'] = $_POST['fname'];
			if (!empty($_POST['mname'])) {
				$_SESSION['mname'] = $_POST['mname'];
				if (!empty($_POST['lname'])) {
					$_SESSION['lname'] = $_POST['lname'];
					header('location: insert.php');
				} else {
					$lname_errormsg = 'Enter a last name';
				}
			} else {
				$mname_errormsg = 'Enter a middle name';
			}
		} else {
			$fname_errormsg = 'Enter a first name';
		}
	}
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<title>Database Operations</title>
	</head>
	<body>
		<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="POST" />
			<table>
				<tbody>
					<tr>
						<td>First Name:</td>
						<td><input type="text" name="fname" value="<?php echo $fname_errormsg; ?>" maxlength="20" /></td>
					</tr>
					<tr>
						<td>Middle Name:</td>
						<td><input type="text" name="mname" value="<?php echo $mname_errormsg; ?>" maxlength="20" /></td>
					</tr>
					<tr>
						<td>Last Name:</td>
						<td><input type="text" name="lname" value="<?php echo $lname_errormsg; ?>" maxlength="20" /></td>
					</tr>
				</tbody>
				<tfoot>
					<tr>
						<td colspan="2"><input type="submit" value="SUBMIT" name="submit" /></td>
					</tr>
				</tfoot>
			</table>
		</form>
	</body>
</html>



Oh, and I'm not quite sure if I've done things neatly. Any suggestion would be highly appreciated. :)/>

Cheers!

Is This A Good Question/Topic? 0
  • +

Replies To: Form Validation Error Message

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: Form Validation Error Message

Posted 09 February 2013 - 06:13 AM

Simple! Mixing PHP and HTML is actually pretty bad, but it seems to be the case here.

It's a bad idea to put your error messages inside of the text box. Your users will be confused and you might actually receive "valid" data that's actually your error messages.

But the quick and dirty way would be adding this right before that HTML:
$fname_errormsg = (isset($fname_errormsg)) ? $fname_errormsg : $_POST['fname'];
$mname_errormsg = (isset($mname_errormsg)) ? $mname_errormsg : $_POST['mname'];
$lname_errormsg = (isset($lname_errormsg)) ? $lname_errormsg : $_POST['lname'];



These are ternary operators. Basically the first line is the same as:
if(isset($fname_errormsg)){
    $fname_errormsg = $fname_errormsg;
} else {
    $fname_errormsg = $_POST['fname'];
}



So if you have an error message, that will be displayed. But if you don't, the data that was sent would be displayed.

This post has been edited by creativecoding: 09 February 2013 - 01:59 PM

Was This Post Helpful? 1
  • +
  • -

#3 josefroyadvincula  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-December 12

Re: Form Validation Error Message

Posted 09 February 2013 - 06:31 AM

Well, you do have an excellent point which I never thought of, lol. Thanks!

I don't think I'm familiar with the 1st 3 lines of code though, I might need to do a little more reading on that. However, I do understand your explanation at the bottom, lol. Thanks!

One question though.

isn't

if(isset($fname_errormsg)){
	$fname_errormsg = $fname_error_msg
}


going to return... well, nothing? Or is that the whole point? Or am I totally missing something? Confused. lol
Was This Post Helpful? 0
  • +
  • -

#4 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: Form Validation Error Message

Posted 09 February 2013 - 02:00 PM

no it's essentially the same logic as "if fname has a value, keep it that way", just in a little more... complicated and shortened fashion.

You can read more about ternary operators here (you may have to scroll down a bit)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1