enter key clears form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

enter key clears form

Post by fariquzeli »

I have a simple forgotten password page, the problem is when the user hits return the script is executed but the form is just cleared.

When the user clicks on the actual submit button rather than hitting enter, the form works fine. Any fixes for this?

Code: Select all

<?php
	if(!IsSet($submit)) &#123;
	?>
            <div align="center">
              <h3 align="left">If you have forgotten your password and/or username:</h3>
              <table width="68%" border="0" cellspacing="0" cellpadding="5">
                <tr>
                  <td><li>Enter your email in the form below</li>
                    &nbsp;</td>
                </tr>
                <tr>
                  <td><li>Check your email to recover your username/password</li>
                    &nbsp;</td>
                </tr>
                <tr>
                  <td><li>Proceed to the <a href="http://www.pancorp.com/techs/index.php">Login 
                      Page</a> and enter your username/password combination</li>
                    &nbsp;</td>
                </tr>
              </table>
              <br>
              <br>
              <font size="2" face="Verdana, Arial, Helvetica, sans-serif"> </font></div>
            <form action="<?=$PHP_SELF?>" method="post" name="forgetPW" id="forgetPW">
              <table width="250" border="0" align="center" cellpadding="0" cellspacing="6">
                <tr> 
                  <td width="32%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email:</font></td>
                  <td width="68%"><input name="email" type="text" id="email"></td>
                  <td width="68%"><input type="submit" name="submit" value="Submit"></td>
                </tr>
              </table>
              <br>
            </form>
            <?php
&#125;
else &#123;
	include("webvars.php");
	mysql_connect($hostname, $user, $pass)
	or die ("the site database is down.");
	mysql_select_db("pancorp");
//connect to db
	$sql = "SELECT * FROM technicians WHERE email = '$email'";
	$result = mysql_query($sql) or die(mysql_error());		
	$info = mysql_fetch_array($result);
	$firstname = $info&#1111;'firstname'];
	$username = $info&#1111;'username'];
	$password = $info&#1111;'password'];
	//fetch info pertaining to email in an array
	$address = ($email);
	$body = "$firstname:

	Your username and password are as follows:
	
	Username: $username
	Password: $password
	
	To login to your account proceed to http://techs.pancorp.com
	
	Sincerely,
	Pancorp.com Team";
	
	mail($address, "Pancorp.com Password Retrieval", $body, "From: Pancorp.com Username/Password <pancorp@pancorp.com>");	
//email information
?>
your password has been emailed

<?php
&#125;
?>
that's my code.
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

instead of checking for the submit, why not check for the email variable

Code: Select all

if(!IsSet($email)) &#123; 
   ?>
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It's because some browsers don't consider the enter key being pressed to have anything to do with the submit button. This means that you are just refreshing the form because $submit isn't set.

One way of dealing with this is to add a hidden field to your form,

Code: Select all

<input type="hidden" name="action" value="submit" />
Then change this:

Code: Select all

if(!IsSet($submit)) &#123;
to this

Code: Select all

if (!isset($action)) &#123;
Mac
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

worked great, thanks.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Rather, use

Code: Select all

if (!isset($_POST&#1111;'action'])) &#123;
or

Code: Select all

if (!isset($_GET&#1111;'action'])) &#123;
depending on if you are submitting via POST or GET and using PHP 4.1+. Yes, use these over just $action. It makes it easier to know where variables come from.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

Thanks, I think we're using php 4.0.1 or something weird like that, or else i would have used the $_POST variables.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

fariquzeli wrote:Thanks, I think we're using php 4.0.1 or something weird like that, or else i would have used the $_POST variables.
You can use $HTTP_POST_VARS in that version. It's best to try and use these predefined variables because, like Jason said, it's easier to read your code and know where things are coming from.
Post Reply