Jump to content

Reset Button Doesn't Work


twilitegxa

Recommended Posts

I am using php to keep the posted data in a field when the user has an error message displayed, but now my reset button doesn't seem to be working. Can I fix this without having to use JavaScript to validate my form? Here is my form:

 

<?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>

Link to comment
Share on other sites

instead of using $_POST['username'] use submit, which is the name of your submit button.

 

<?php if(isset($_POST['submit'])) echo $_POST['username']; ?>

 

also check if submit is in the POST array before you do any proccessing at the top of your script.

Link to comment
Share on other sites

if the submit button has been pressed, why would you want to reset it ?

 

The reset button will only clear out the form of anything that has been changed since the last time the form was loaded.  If you submit the form to the same page with pre-loaded values, the reset button wont do anything.

Link to comment
Share on other sites

Well, I was just thinking that in case they pressed the submit button and received an error because they left something out, and then they might try pressing the reset button for who knows what reason, and might get aggravated that it didn't work. I am not very good with JavaScript, so I would rather use PHP to validate my forms, but some forms might be long and you never know when someone will decide they want to reset everything after an error pops up. I don't know. Maybe the problem would never arise.

Link to comment
Share on other sites

you can always try to validate it twice.  use javascript to perform an initial validation when the submit button is pressed which will catch all of the silly errors.  this way the reset button should still work.  If the initial validation passes, you can then push the form through to the backend for a more strict validation in php.  at that point it is unlikely that the user would want to erase the entire form, however if you wanted you could always have a third button there which would call a javascript function to clear out all of the form entries at any time

Link to comment
Share on other sites

if all proccessing was wrapped in if(isset($_POST['submit'])) your reset button should always work.

 

then to redisplay content you could have

 

if(isset($_POST['username'] && !empty($errors))

{

    //redisplay username

}

 

this way it would never remember anything if they simply clicked the reset button. It could simply be a link back to the same page. Otherwise a javascript reset button would work too, setting each field to "" when clicked.

Link to comment
Share on other sites

Well, this is what I have after making the changes earlier:

 

<?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['submit'])) echo $_POST['username']; ?>" /></td>
</tr>
<tr>
<td>Password (6-20 chars):</td>
<td><input type="password" name="password" value="<?php if(isset($_POST['sumbit'])) echo $_POST['password']; ?>" /></td>
</tr>
<tr>
<td>Your name:</td>
<td><input type="text" name="name" value="<?php if(isset($_POST['submit'])) echo $_POST['name']; ?>" /></td>
</tr>
<tr>
<td>Email address:</td>
<td><input type="text" name="email" value="<?php if(isset($_POST['submit'])) echo $_POST['email']; ?>" /></td>
</tr>
<tr><td> </td></tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Register!" />
<input type="reset" 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 don't know to display it the way you were suggested. Can you help me out?

Link to comment
Share on other sites

i had a quick tidy up for you...

 

<?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['submit'])) 
{
unset($_POST['submit']);
foreach($_POST as $key => $value)
{
	$$key = $value;
}

$result = @mysql_query('SELECT username FROM `users` WHERE username = \''.mysql_real_escape_string($username).'\'');
if($row = @mysql_fetch_row($result)) 
{
	array_push($error, 'Your username is already being used. Please select another.');
}
$len = strlen($username);

if($len < 3 || ($len > 20)) 
{ 
	array_push($error, 'Your username must be between 3 and 20 characters long.'); 
}
$len = strlen($password);

if($len < 6 || ($len > 20)) 
{ 
	array_push($error, 'Your password must be between 6 and 20 characters long.'); 
}
$len = strlen($name);

if($len > 100) 
{ 
	array_push($error, 'Sorry, your name can be no longer than 100 characters long.'); 
}
if(empty($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', $email) == false) 
{
	array_push($error, 'Your e-mail address is incorrect');
}

$len = strlen($email);
if($len > 255) 
{ 
	array_push($error, 'Sorry, your e-mail address is too long.'); 
}

if(empty($error)) 
{
	@mysql_query("INSERT INTO users (username, password, name, email) VALUES 
	(" . mysql_real_escape_string($username) . ", " . mysql_real_escape_string(md5($password)) .
	", " . mysql_real_escape_string($name) .", " . mysql_real_escape_string($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['submit']) && !empty($errors)) echo $_POST['username']; ?>" /></td>
</tr>
<tr>
<td>Password (6-20 chars):</td>
<td><input type="password" name="password" value="<?php if(isset($_POST['sumbit']) && !empty($errors)) echo $_POST['password']; ?>" /></td>
</tr>
<tr>
<td>Your name:</td>
<td><input type="text" name="name" value="<?php if(isset($_POST['submit']) && !empty($errors)) echo $_POST['name']; ?>" /></td>
</tr>
<tr>
<td>Email address:</td>
<td><input type="text" name="email" value="<?php if(isset($_POST['submit']) && !empty($errors)) echo $_POST['email']; ?>" /></td>
</tr>
<tr><td> </td></tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Register!" />
<input type="reset" 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>

 

a quick google will show up plenty of examples for resetting a form with javascript.

 

With the code you have now a link back to the form page will reset it....

Ben

Link to comment
Share on other sites

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.