Newb
Newb

Reputation: 29

How do I do Textbox Submit

I have a search box and a buttion. currently a user enter some text and press the search button. But I want to add another feature that instead of clicking the search button people can hit enter to search. How can I do that?

Here is my code sample:

<form method="post" action="">

     <input id="search" name="search" type="text" />
     <input id="search_btn" name="search_btn" type="submit" />

</form>

Thanks in advance

Upvotes: 2

Views: 795

Answers (3)

AungAung
AungAung

Reputation: 1

<form method="POST" action="<?php $_SERVER['PHP_SELF'];?>">
<input type='text' name='text1' value='' size='30'>
<input type='submit' name='search' value='Search'>
</form>
<?php
include("connect.php");
mysql_select_db("example",$con);

if(isset($_POST['search'])){
$text=$_POST['text1'];

$result=mysql_query("SELECT * FROM example_tbl WHERE inputdata LIKE '$text'");
While($row=mysql_fetch_array($result)){
echo $row[0]."".$row[1]."".$row[2];
}
}
?>

Upvotes: -1

Salil
Salil

Reputation: 47512

You can do it using following ways

<script type="text/javascript">
  function submitenter(myfield,e)
  {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;
    if (keycode == 13)
    {
        myfield.form.submit();
        return false;
    }
    else
     return true;
  }

</script>

<form method="post" action="">

     <input id="search" name="search" type="text" :onKeyPress=>"return submitenter(this, event);"/>
     <input id="search_btn" name="search_btn" type="submit"  />

</form>

Upvotes: 0

Teej
Teej

Reputation: 12883

Pressing 'enter' would submit most forms without hocus pocus.

Upvotes: 3

Related Questions