Page 1 of 1

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

Posted: Thu Oct 22, 2009 8:17 am
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
 
 
 

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

Posted: Thu Oct 22, 2009 8:44 am
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.

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

Posted: Fri Oct 23, 2009 12:55 am
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

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

Posted: Fri Oct 23, 2009 1:14 am
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?

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

Posted: Fri Oct 23, 2009 2:58 am
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;
}
 

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

Posted: Fri Oct 23, 2009 3:27 am
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]-->