View Full Version : Detecting Enter Key Help

Jun 16th, 2008, 09:12 PM
I'm trying to detect the Enter key in a text field so I can type in a name and then press the enter key to submit. Submit calls index_dispatcher.php which sets some session variables for the user name typed in and then calls index.php. Index.php then uses these $_SESSION[] vars to display some data about the user. I actually am detecting the Enter key OK - but - using Firefox and Safari I have to type in the user name (bill in this case) and press Enter - then index.php refreshes and I have enter in the name bill again and press Enter again then it finally works!

So, this works fine on IE 6.0 but takes two trys to get it to work in Firefox and Safari. I'm using windows Vista.

My index.php JS code (seems to be working fine):

function DetectNEnter(e)
var characterCode;
if(e && e.which) // NN4 specific code
e = e;
characterCode = e.which;
e = event
characterCode = e.keyCode; // IE specific code
}if (characterCode == 13) //// Enter key is 13
} else return false;}

My index.php <Form> line:

<FORM NAME="index_form.php" ACTION="index_dispatcher.php" METHOD="post">

My index.php text box code:

Name <INPUT TYPE="text" NAME="frm_name" onKeyDown="DetectNEnter(event);"> <INPUT TYPE="submit" NAME="frm_button_login" VALUE="Login">

Here's my index_dispatcher.php code and this is working ok as my debug line shows 'OK' *every* time but on the first pass in FF/Safari the session vars do not display when the index.php script is called.

$php_string = strtolower($_POST["frm_sc_login_name"]);

// open mysql database connection --------------------------------
// server username password
$php_linkID = mysql_connect("localhost","db16_ralph","atg568h93hf65jn8");
if ($php_linkID != TRUE)
print '<br>database connection failed.<br>';

// select the main database --------------------------------------
$php_resultID = mysql_select_db("db_db",$php_linkID);
if ($php_resultID != TRUE)
print "database selection failed.<br>";

$php_SQL = "SELECT * FROM Salesmen WHERE login_name = '".$php_string."'";
$php_resultID = mysql_query($php_SQL, $php_linkID);
$php_row = mysql_fetch_object($php_resultID);
$php_row_count = mysql_num_rows($php_resultID);
if ($php_row_count > 0)
print 'OK<br>';

$_SESSION['php_g_sc_login_name'] = $php_row->login_name;
$_SESSION['php_g_sc_first_name'] = $php_row->first_name;
$_SESSION['php_g_sc_last_name'] = $php_row->last_name;

// works ok every time
print $_SESSION['php_g_sc_first_name'].'<br>';
print $_SESSION['php_g_sc_last_name'].'<br>';


Can anyone see why it takes two text box tries before the index.php script 'sees' the php variables back in the index.php script?