Jump to content

'Enter' Key doesn't work but 'submit' does


Acute Chaos

Recommended Posts

So I have various things I'm putting together with 'submit' buttons.

 

Why... when I have the users log in they can either click the "log in" button or just hit enter on their keyboard and it works fine.

 

But

 

When they search in the directory the enter key doesn't work.  You have to click the 'search' button.  If you hit 'enter' on the keyboard, the field resets to blank and nothing happens.

 

What am I missing here??  Is there a common issue I don't know about?

 

thanks!

Link to comment
Share on other sites

 

Here's the code:

 

 

<?php   
if(isset($_POST['submit'])){   
if(isset($_GET['go'])){   
if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){   
$name=$_POST['name'];   
$db=mysql_connect  ("server", "user",  "pass") or die ('I cannot connect to the database  because: ' . mysql_error());   
$mydb=mysql_select_db("mydb");   
$sql="SELECT * FROM user WHERE f_name LIKE '$name%' OR l_name LIKE '$name%'";  
$result=mysql_query($sql); 
$numrows=mysql_num_rows($result);   
echo  "<p><b>" .$numrows . " </b> results found for " . "<i>" ."<b>'" . $name . "':</b>". "</i>" . "</p><p> </p>";   
while($row=mysql_fetch_array($result)){   
$FirstName=$row['f_name'];   
$LastName=$row['l_name'];  
$Email=$row['email'];       
echo  "<ul>\n";   
echo  "<li><strong>" . $FirstName . " " . $LastName .  "</strong></li>\n";   
echo  "<li>" . "<a href=mailto:" . $Email .  ">" . $Email . "</a></li>\n"; 
echo  "</ul>";   
}   
}   
else{   
echo  "<p><font color='red'>Please enter a search query</font></p>";   
}   
}   
}

 

If I hit 'enter' without anything in the field, it echos 'Please enter a search query'  - - so I know it is recognizing the key being hit but if you hit 'enter' with text in the field it resets the page.  Put in the same search term and click the 'search' button and you get the right result.

 

hmmmm.

Link to comment
Share on other sites

From what I can see, it should work, but try a couple things. First change the way you check to see if the form is submitted. There's a bug that isn't seen too often, but it can affect the way certain browsers handle sending the value of a submit button when the enter key is used.

 

// FROM
if(isset($_POST['submit'])) {

// TO
if(strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {

 

If that doesn't make any difference, start echoing messages at different points in the code to see where it's stopping.

 

if(strtolower($_SERVER['REQUEST_METHOD']) === 'post' ) {
   echo 'POST is set<br>'; // <--- ECHO
   if(isset($_GET['go'])) {
      echo 'GET go is set<br>'; // <--- ECHO
      if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])) {
         echo 'POST[\'name\'] validates<br>'; // <--- ECHO
         $name=$_POST['name'];
         echo "$name<br>"; // <--- ECHO
         $db=mysql_connect  ("server", "user",  "pass") or die ('I cannot connect to the database  because: ' . mysql_error());

 

See what happens, and post the results . . .

Link to comment
Share on other sites

Actually .... I haven't been testing cross browser and I just tried a few different ones.  So far it only seems to be a problem in IE8.  Which still doesn't make a lot of sense considering the same browser works fine with other forms I have put together which are virtually the same.  At least it doesn't make sense to me...

 

Ok back to work!!

Link to comment
Share on other sites

I'm not doing a good job at getting back to my other work!  lol

 

I tried that link mikosiko.  Interesting for sure.  Three suggestions there. Two don't work.  One did - putting in the hidden field to trick IE but I haven't tested it thoroughly.  I'll do that in a bit but for starters it seemed good.

 

Thanks again.  Still want to look into Pikachu's suggestions.

 

tx folks.

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.