2 things: enter doesn't work in IE & empty 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
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

2 things: enter doesn't work in IE & empty form

Post by sirTemplar »

i have this search form that works well except for 2 things.

1) on IE when i hit enter instead of the Submit button, i do not get any result. with firefox, no problem.
2) when i hit submit or enter without putting anything on the search form, it outputs All data from the database.

any help to fix the issue?

Code: Select all

 
 
 
<script type="text/javascript" language="JavaScript">
function countryempty()
{
    if ( document.form.country.value == '' )
    {
        alert('No search was entered!')
        return false;
    }
}
</script>
 
<?
$country = $_POST["country"];
 
 
// if page is not submitted to itself echo the form
if (!isset($_POST['submit'])) {
    
?>
 
<form method="POST" action="<? echo $PHP_SELF; ?>" onSubmit="return countryempty();">
    <p><b>SEARCH</b> a City or Country <input type="text" name="country" size="32"><p>
    <p><br>
    <input type="submit" value="submit" name="submit"><input type="reset" value="Reset" name="B2"></p>
 
</form>
 
<?
    } else {
?>
 
<?
 
$db_host  = ''; 
$db_user  = ''; 
$db_pass  = ''; 
$db_name  = '';
$db_table = ''; 
$conn = mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db($db_name,$conn);  
 
 
// The database query follows
 
 
 
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: 2 things: enter doesn't work in IE & empty form

Post by markusn00b »

The first problem is because you are checking for the 'submit' form element in the POST array. Evidently, IE does not send this if the enter key is pressed. You might create a hidden element in the form and check for that.

Code: Select all

 
<!-- form stuff ... -->
<input type="hidden" name="submit_fallback" />
 
<?php
if (isset($_POST['submit']) || isset($_POST['submit_fallback'])) {
    // ... do something
}
?>
 
To your second question - I don't know because you cut that part of the script off.

Mark.
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Re: 2 things: enter doesn't work in IE & empty form

Post by sirTemplar »

what is the best way to do just when they hit submit or enter without any text on the search field, they go back to the form. thanks
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Re: 2 things: enter doesn't work in IE & empty form

Post by sirTemplar »

i added the ff. code and it works,

Code: Select all

<? if($_POST['country'] == "")
{
print "An error has occured. No search input";
  exit(); 
}
is it possible that it just show the form again?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: 2 things: enter doesn't work in IE & empty form

Post by markusn00b »

sirTemplar wrote:i added the ff. code and it works,

Code: Select all

<? if($_POST['country'] == "")
{
print "An error has occured. No search input";
  exit(); 
}
is it possible that it just show the form again?
Sure. You can redirect the user back to the form:

Code: Select all

 
if ($_POST['country'] == '') {
    header("location: your_form.php?error=1");
    exit;
}
 
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Re: 2 things: enter doesn't work in IE & empty form

Post by sirTemplar »

thank you.
just for info. the hitting Enter not working in IE was resolved by adding the ff line on the form

Code: Select all

<!--[if IE]><input type="text" style="display: none;" disabled="disabled" size="1" /><![endif]-->
Post Reply