Jump to content

put a * on a field


arden

Recommended Posts

got a problem with forms..i want to put an * or a message to a field where the user inputs a wrong or invalid inputie.<form method="post" action="ie.php">NAme <input type="text" name="name" size= "30" maxlength="255"> // i want to put an error message here if the users input is invalid AGE<input type="text" name="age" maxlength=2 > // same here</form>at the text field the user inputs an invalid age..the error msg appear after the text field.. thx in advance guys..:)

Link to comment
Share on other sites

You need to have the form submit to itself, and you can do the error checking and decide if you need to show the same form again with the error messages, or show the next page. If you are showing several forms, you could use one PHP script for that where you just decide which form to show based on the data you've gotten so far. If you're submitting the form to itself, the easiest way to show errors for each field is to have an error variable for each field and then just set a value for it if there's an error there.

<?php$errors = false;$name_error = '';$age_error = '';if (isset($_POST['name'])){  $name = $_POST['name'];  $age = $_POST['age'];  if ($name == '')  {	$name_error = 'This field is required.';	$errors = true;  }  if ($errors == false)  {	// go to next page  }}?><form method="post" action="ie.php">NAme <input type="text" name="name" size= "30" maxlength="255"> <?php echo $name_error; ?>AGE<input type="text" name="age" maxlength=2 > <?php echo $age_error; ?></form>

Link to comment
Share on other sites

dude.. why did u put the$errors = false; ?i mean.. why dont give it a ' ' instead of false?..

You need to have the form submit to itself, and you can do the error checking and decide if you need to show the same form again with the error messages, or show the next page. If you are showing several forms, you could use one PHP script for that where you just decide which form to show based on the data you've gotten so far. If you're submitting the form to itself, the easiest way to show errors for each field is to have an error variable for each field and then just set a value for it if there's an error there.
<?php$errors = false;$name_error = '';$age_error = '';if (isset($_POST['name'])){  $name = $_POST['name'];  $age = $_POST['age'];  if ($name == '')  {	$name_error = 'This field is required.';	$errors = true;  }  if ($errors == false)  {	// go to next page  }}?><form method="post" action="ie.php">NAme <input type="text" name="name" size= "30" maxlength="255"> <?php echo $name_error; ?>AGE<input type="text" name="age" maxlength=2 > <?php echo $age_error; ?></form>

Link to comment
Share on other sites

In PHP, an empty string (and several other things) are equivalent to false. However, explicitly using the false keyword helps us understand better why we made the assignment (namely, that it is not true that we have errors).

Link to comment
Share on other sites

i cant do it.. here is the code..

<?phprequire_once 'db.php';$page_mode = isset($_POST['page_mode']) ? $_POST['page_mode'] : '';$error_string = '';if ($page_mode == 'register'){	$email = trim($_POST['email']);	$fname = trim($_POST['fname']);	$lname = trim($_POST['lname']);  	$age = trim($_POST['age']);if(!filter_var($age, FILTER_VALIDATE_INT))    	$error_string .= 'Please check your age.<br>'; if ($fname == '')    $error_string .= 'Please enter your first name.<br>'; if ($lname == '')    $error_string .= 'Please enter your Last name.<br>';if (!isValidEmail($email))    	$error_string .= 'Please enter a valid email address.<br />';if ($error_string == ''){    	$result = db_query("SELECT id FROM Persons WHERE email='" . mysql_real_escape_string($email) . "'");    	if (mysql_num_rows($result) > 0)      	$error_string .= 'That email address is already registerd.<br>';    	else{      	$email = mysql_real_escape_string($email);      	$name = mysql_real_escape_string($name);	$name = mysql_real_escape_string($name);      	$age = mysql_real_escape_string($age);            	db_query("INSERT INTO Persons (FirstName, LastName, Age, Email) VALUES ('{$fname}', '{$lname}', '{$age}', '{$email}')");      	header('Location: table.php');      	exit();	}	}}function isValidEmail($email = ''){    	return preg_match("/^[\d\w\/+!=#|$?%{^&}*`'~-][\d\w\/\.+!=#|$?%{^&}*`'~-]*@[A-Z0-9][A-Z0-9.-]{1,61}[A-Z0-9]\.[A-Z]{2,6}$/ix",$email);}?><html><head>    	<title>PERSONAL INFO</title></head><body>   	<div class="error_text"><?php echo $error_string; ?></div> // i just put it here.. but i really wanna put it where the error is..     	<form action="val.php" method="post">    	<input type="hidden" name="page_mode" value="register"> 	<br/>    	<div class="left_box">First Name</div>    	<div class="right_box"><input type="text" name="fname" size="30" maxlength="255"	value="<?php if (isset($fname)) echo $fname; ?>"></div> // I WANT TO PUT THE ERROR MESSAGE FOR "fname" HERE	<div class="left_box">Last Name</div>	<div class="right_box"><input type="text" name="lname" size="30" maxlenght="255"	value="<?php if (isset($lname)) echo $lname; ?>"></div> // FOR "lname" HERE	<div class="left_box">Age</div>	<div class="right_box"><input type="text" name="age" size="3" maxlength="2"	value="<?php if(isset($age)) echo $age;?>" ></div> // FOR "age" HERE	<div class="left_box">Email address</div>	<div class="right_box"><input type="text" name="email" size="30" maxlength="255"	value="<?php if (isset($email)) echo $email; ?>"></div> // AND HERE    	<div class="left_box"> </div>    	<div class="right_box"><input type="submit" value="ADD" size="30"></div>	</form></body></html>

the table.php is where i put a table to show my database..actually that code is from JSG..i just add/edit some.. and can you tell me why when i delete the

     	$result = db_query("SELECT id FROM Persons WHERE email='" . mysql_real_escape_string($email) . "'");    	if (mysql_num_rows($result) > 0)      	$error_string .= 'That email address is already registerd.<br>'; 

it doesnt run..im using a notepad..thats why i cant see the error..

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...