twilitegxa Posted July 28, 2009 Share Posted July 28, 2009 In the following form, how can I display the error message beside the field the error is generated from rather than all listed at the top f the form? Here is the code: <?php //Access Tracking Snippet //set up static variables $page_title = "register.php"; $user_agent = getenv("HTTP_USER_AGENT"); $date_accessed = date("Y-m-d"); //connect to server and select database $conn = mysql_connect("localhost", "root", "") or die(mysql_error()); $db = mysql_select_db("smrpg", $conn) or die(mysql_error()); //create and issue query $sql = "insert into access_tracker values ('', '$page_title', '$user_agent', '$date_accessed')"; mysql_query($sql,$conn); ?> <?php session_start(); // Connect to MySQL database: $access = mysql_connect('localhost','root','') or die ('Could not connect to database'); mysql_select_db('smrpg',$access) or die ('Could not select table'); # # $error = array(); if(isset($_POST['username'])) { $result = @mysql_query('SELECT username FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\''); if($row = @mysql_fetch_row($result)) { array_push($error, 'Your username is already being used. Please select another.'); } $len = strlen($_POST['username']); if($len < 3 || ($len > 20)) { array_push($error, 'Your username must be between 3 and 20 characters long.'); } $len = strlen($_POST['password']); if($len < 6 || ($len > 20)) { array_push($error, 'Your password must be between 6 and 20 characters long.'); } $len = strlen($_POST['name']); if($len > 100) { array_push($error, 'Sorry, your name can be no longer than 100 characters long.'); } if(!$_POST['name']) { array_push($error, 'You must provide your name'); } if(preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i', $_POST['email']) == false) { array_push($error, 'Your e-mail address is incorrect'); } $len = strlen($_POST['email']); if($len > 255) { array_push($error, 'Sorry, your e-mail address is too long.'); } if(!$error) { @mysql_query('INSERT INTO `users` (username, password, name, email) VALUES (\''.mysql_real_escape_string($_POST['username']).'\', \''.mysql_real_escape_string(md5($_POST['password'])).'\', \''.mysql_real_escape_string($_POST['name']).'\', \''.mysql_real_escape_string($_POST['email']).'\')'); header('Location: user_created.php'); die('<a href="login.php">Login</a>'); } } ?> <!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=iso-8859-1" /> <title>Sailor Moon RPG - Register A Username</title> <style type="text/css" media="screen"> /*<![CDATA[*/ @import url(global.css); /*]]>*/ </style> </head> <body> <!-- HEADER --> <h1 class="logo">Sailor Moon RPG</h1> <!-- /HEADER --> <?php include("topnav.php"); ?> <div id="main"> <?php include("includes/log.php"); ?> <?php include("mainnav.php"); ?> <h1>Registeration</h1> <table cellspacing="2" cellpadding="0" border="0"> <form method="post" action="register.php"> <?php if(isset($error) && $error) { ?> <tr> <td colspan="2"> <ul><?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?></ul> </td> </tr><?php } ?> <tr> <td>Username (3-20 chars):</td> <td><input type="text" name="username" value="<?php if(isset($_POST['username'])) echo $_POST['username']; ?>" /></td> </tr> <tr> <td>Password (6-20 chars):</td> <td><input type="password" name="password" value="<?php if(isset($_POST['password'])) echo $_POST['password']; ?>" /></td> </tr> <tr> <td>Your name:</td> <td><input type="text" name="name" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" /></td> </tr> <tr> <td>Email address:</td> <td><input type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /></td> </tr> <tr><td> </td></tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Register!" /> <input type="submit" name="reset" value="Reset" /></td> </tr> </form> </table> </div> <?php include("bottomnav.php"); ?> <!-- FOOTER --> <div id="footer_wrapper"> <div id="footer"> <p>Sailor Moon and all characters are<br> trademarks of Naoko Takeuchi.</p> <p>Copyright © 2009 Liz Kula. All rights reserved.<br> A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p> <div id="foot-nav"><!-- <ul> <li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li> <li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li> </ul> --></div> </div> </div> <!-- /FOOTER --> </body> </html> Quote Link to comment Share on other sites More sharing options...
ldougherty Posted July 28, 2009 Share Posted July 28, 2009 The logic behind your code is that you have a form and action on the same page. A user submits the form and then the action is executed which in your case includes error checking. All you need to do is add form fields next to the code checking and only display the error if it occurs. Here is a real simple example.. #test.php if ($action) { # check for any errors. if ($var1 <> 'WHATEVER' || $var2 <> 'SOMETHING') { ## ERROR FOUND ## PRINT FORM FIELDS WITH ERROR ON APPROPRIATE FIELD echo "<form action=test.php method=POST>"; echo "<input type='text' name='var1'>"; if ($var1 <> 'WHATEVER') { echo "var1 does not equal WHATEVER"; } echo "<input type='text' name='var2'> if ($var2 <> 'SOMETHING') { echo "var2 does not equal SOMETHING"; } echo "<input type='hidden' name='action' value='TRUE'>"; echo "<input type='submit'>"; echo "</form>"; } else { ## NO ERRORS FOUND ## DO QUERY HERE } } else { # display the form echo "<form action=test.php method=POST>"; echo "<input type='text' name='var1'>"; echo "<input type='text' name='var2'> echo "<input type='hidden' name='action' value='TRUE'>"; echo "<input type='submit'>"; echo "</form>"; } Hope that makes sense. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 28, 2009 Author Share Posted July 28, 2009 I think it does. I will try to figure it out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.