0

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!

aimme
  • 6,385
  • 7
  • 48
  • 65
jeeeee
  • 229
  • 1
  • 5
  • 18
  • 1
    It has nothing to do with PHP. Look for javascript form `onsubmit` event – Inurosen Sep 02 '15 at 14:15
  • 1
    Plus, your code suggest your have a form, or a form only require one submit, so the first one will be counted as the only one and other are ignore, so you might require JavaScript for one of the two, or for both depending on the action behind these buttons. – Anwar Sep 02 '15 at 14:22
  • 1
    @zera: you can have as many submits as you want, and only the clicked one gets submitted. but OP isn't clicking EITHER button, hence the problem. – Marc B Sep 02 '15 at 14:23
  • thank you i'll try to look it up! thanks for the idea! – jeeeee Sep 02 '15 at 14:24
  • @MarcB yup. i wasn't clicking thats the problem. because most of the time "enter/return" in the keyboard is used so i would want enter to use the search button instead of print button. – jeeeee Sep 02 '15 at 14:25
  • possible duplicate of [How to trigger HTML button when you press Enter in textbox?](http://stackoverflow.com/questions/12955222/how-to-trigger-html-button-when-you-press-enter-in-textbox) – aimme Sep 02 '15 at 18:54

2 Answers2

1

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) {
    $('#your-form').submit(); 
    }
    });
Parsa Mir Hassannia
  • 351
  • 2
  • 6
  • 17
  • Search should be the default behaviour. Therefore print should use JavaScript. Also, `function(e)`. – rybo111 Sep 02 '15 at 16:19
0

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.

rybo111
  • 12,240
  • 4
  • 61
  • 70