HTML / PHP更改按下enter时使用的按钮

so i have 2 buttons

<button type="submit" name="print">print</button>
<button type="submit" name="search">search</button>

beside the button search there is a textbox

<input type="text" name="txtSearch" autofocus>

now what i want to do is whenever I press enter i'd like to submit using the button search. but what happens is the button print is always getting submitted because it is on top of button search. I cant put button search on top of print because of the design. Is it possible to do it in php? or do I need javascript? i'm just starting to program and trying to learn more. thank you very much for the help!

You can't do that with php , you should use javascript or jquery .

add an id for your search input like this :

<button id='search' type="submit" name="search">search</button>

then you can use this code in jquery :

$('#search').keydown(function(e) {
    var key = e.which;
    if (key == 13) {
    $('#search').submit(); 
    }
    });

You can...

Separate into two forms:

<form>
  <button type="submit" name="print">print</button>
</form>
<form>
  <button type="submit" name="search">search</button>
  <input type="text" name="txtSearch" autofocus>
</form>

Or change the print button to:

  • <a href="print.php?id=123">Print</a> or
  • <button type="button">print</button>

Or you could put your search button first, and use float or some kind of position:absolute and margin-top CSS.

Or use JavaScript, as Parsa suggests.