0

I want to submit the form value in 'enter' key.I have a button search now to submit the value value.but I want to do it without search button on enter key press.

  html
    <input class="stxt" type="text" name="searchtxt"><a style="padding-right:2ex;"></a>
   <button name="search" class="search">Search</button></p>

  php
     if(isset($_REQUEST['search']))
       {
         $search=$_REQUEST['searchtxt'];
         some code here
       }
user3386779
  • 6,883
  • 20
  • 66
  • 134

3 Answers3

4

Try the below code it will work.. I have coded without button only.

<?php 
if(isset($_POST['search']))
{
    echo $search = $_POST['search'];
}
?>
<html>
<head>

<script>
    document.onkeydown=function(evt){
        var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
        if(keyCode == 13)
        {
            //your function call here
            document.test.submit();
        }
    }
</script>
</head>
<body>
<form name="test" action="#" method="POST">
<input type="text" name="search" />
</form>
</body>
</html>
Vetrivel
  • 1,149
  • 1
  • 7
  • 16
0

If I understand you correctly you want to trigger php code on every key press in which case good solution will be using AJAX.

 $( "#target" ).keypress(function(e) {
     $.ajax({
            url: "logic.php",
            type: post,
            data: $('form').serialize(),
            success: function( data ) {
                //Process data received from server.
            }
          });
 });

On each key press all form data will be serialized and send over to server where you can perform your logic.

xReprisal
  • 810
  • 8
  • 23
0

The button should be submitting on Enter, if it's not, there's something in the HTML that you have not provided. You have tagged the question with JQuery but have not provided any code, so I can only assume that you are submitting the form through only PHP.

Take a look at the button documentation. There are two paragraphs there that are relevant to your situation.

form HTML5
The form element that the button is associated with (its form owner). The value of the attribute must be the id attribute of a element in the same document. If this attribute is not specified, the element must be a descendant of a form element. This attribute enables you to place elements anywhere within a document, not just as descendants of their elements.

This means that for the button to submit the form, it must be inside the form tags. If it is not, you need to specify the form id like this.

<button form="form_id"> 

The second relevant piece of information.

type
The type of the button. Possible values are:
submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
reset: The button resets all the controls to their initial values.
button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.

Since type defaults to submit, it means that the form will submit with the Enter key.

EternalHour
  • 8,308
  • 6
  • 38
  • 57