Page 1 of 1

enter key clears form

Posted: Wed Jul 10, 2002 9:15 am
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.

Posted: Wed Jul 10, 2002 9:23 am
by martin
instead of checking for the submit, why not check for the email variable

Code: Select all

if(!IsSet($email)) &#123; 
   ?>

Posted: Wed Jul 10, 2002 9:23 am
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

Posted: Wed Jul 10, 2002 10:55 am
by fariquzeli
worked great, thanks.

Posted: Wed Jul 10, 2002 12:22 pm
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.

Posted: Wed Jul 10, 2002 12:48 pm
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.

Posted: Wed Jul 10, 2002 2:01 pm
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.