twilitegxa Posted July 27, 2009 Share Posted July 27, 2009 Is there a way to keep the form data a user inputs into a field when they try to submit the form if they have errors? Here is my form example: <?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" /></td> </tr> <tr> <td>Password (6-20 chars):</td> <td><input type="password" name="password" /></td> </tr> <tr> <td>Your name:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>Email address:</td> <td><input type="text" name="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> I am using php to check the fields and produce errors if applicable, but it erases answers if there's an error. Is there a way to keep the input and display error messages with php instead of using Javascript? Link to comment https://forums.phpfreaks.com/topic/167609-solved-php-form-validation-vs-javascript/ Share on other sites More sharing options...
KevinM1 Posted July 27, 2009 Share Posted July 27, 2009 Simply output the current post data in the right place: <input name="name" type="text" value="<?php if(isset($_POST['name'])) echo $_POST['name']; ?>" /> Link to comment https://forums.phpfreaks.com/topic/167609-solved-php-form-validation-vs-javascript/#findComment-883835 Share on other sites More sharing options...
JonnoTheDev Posted July 27, 2009 Share Posted July 27, 2009 The value is contained in the POST array however I would clean the value (remove tags, etc) before placing back into the form field <td><input type="text" name="username" value="<?php print $_POST['username']; ?>" /></td> Link to comment https://forums.phpfreaks.com/topic/167609-solved-php-form-validation-vs-javascript/#findComment-883836 Share on other sites More sharing options...
twilitegxa Posted July 28, 2009 Author Share Posted July 28, 2009 Thank you so much! That works perfectly! Link to comment https://forums.phpfreaks.com/topic/167609-solved-php-form-validation-vs-javascript/#findComment-884382 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.