views:

5879

answers:

5

The problem I'm having is that I have multiple submit inputs in a single form. Each of these submit inputs has a different value and I would prefer to keep them as submit. Whenever the user presses enter, it is as though the topmost submit input is being pressed, and so it is causing problems for the code checking which input was clicked. Is there a way for PHP to determine whether or not the input was clicked, or was just the input that was selected when the user pressed the Enter key?

+5  A: 

Roberto,

PHP is server-side technology and therefore runs on the server - so there is no way for it to determine what keys where pressed at the client (aka the user). That is, of course, unless you specifically code the client-side to include such information with the server requests (posting form data is a form of request too).

One way to accomplish that is by using Javascript in your client code.

See this page as a starting point regarding handling form submit events using Javascript. http://www.w3schools.com/jsref/jsref_onSubmit.asp

You may also have to add a listener for key press events on your page in order to capture the user pressing the Enter key and then recording this information. http://www.howtocreate.co.uk/tutorials/javascript/domevents offers a discussion on the topic of adding/removing event listeners in Javascript but you have to be very careful when using events because improperly used they can be the source of memory leaks which are hard to debug and cause for unhappy users :)

Miky Dinescu
+6  A: 

You can identify which button was used provided you structure your HTML correctly

<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Preview">
<input type="submit" name="action" value="Post">

The $_POST array (or $_GET/$_REQUEST) will contain the key "action" with the value of the enacted button (whether clicked or not).

Now, "clicking" is explicitly a client-side behavior - if you want to differentiate between a click and a keypress, you'll need to add some scripting to your form to aid in that determination.

Edit

Alternatively, you can be "sneaky" and use a hidden submit that should correctly identify a key-pressed for submission, but this probably has some significant impact on accessibility.

<?php

if ( 'POST' == $_SERVER['REQUEST_METHOD'] )
{
    echo '<pre>';
    print_r( $_POST );
    echo '</pre>';
}

?>
<form method="post">

    <input type="text" name="test" value="Hello World">

    <input type="submit" name="action" value="None" style="display: none">
    <input type="submit" name="action" value="Edit">
    <input type="submit" name="action" value="Preview">
    <input type="submit" name="action" value="Post">

</form>
Peter Bailey
This only works when you click on the button. It doesn't work when you press Enter in one of the text inputs.
Um... yes it does. The first one by source-order will be "chosen" by the browser as the submit actor.
Peter Bailey
Yes chosen by the browser, not the user.
How would the hidden input impact accessibility? I already have a default behavior that (I would hope) executes when the user presses the enter key, so if that's what you meant, I have it covered.
Roberto, the first submit-button in the source is chosen when enter is pressed. Thus the user chooses it by pressing enter whether they know it or not.
The Wicked Flea
Ok great. This is the solution I wanted. Thanks.
Well, first of all, if you don't have accessibility as part of your current requirements, don't worry about it. It's silly to address it with a single feature of your site but ignore it everywhere else in your structure.And secondly - I'm not sure. I'm not an accessibility expert - I'm just very cautious of "clever" solutions to web problems because they very often cause troubles for those types of audiences.
Peter Bailey
+2  A: 

PHP alone can't determine how the form submit event was triggered, because that happens on the client-side while PHP is a server-side language. You'd have to implement Javascript to listen for -- and log to the server-side -- key presses and mouse clicks, and then analyze that data to find what you're looking for.

Now, PHP can tell which submit input was triggered, as it will appear in the form data while the others will not. Most browsers make the first submit input the default (the one that is triggered on an Enter key press). You could re-order all your submits so as to control which submit is triggered.

EvanK
I guess I assumed the browser would pass the information on to PHP concerning how the form was submitted. Your solution was one I was considering, and now seems the only solution.
+1  A: 

PHP can't really know what happened on the client side.

I'd recommend using javascript. When the user do the action, catch it and store it in an hidden field that will be submited with the form. You can also keep track of what input is active and store it in an hidden field.

The code would go a bit like that (i didnt checked the syntax)

<input type="text" onfocus="setCurrent(this)" id="1" />
<input type="hidden" id="hid" />

function setCurrent(o){
  $('hid').value = o.id;
}

I think that playing around with events catching and hidden fields should give you the result that you want.

Hope that helps

marcgg
A: 

It's how you write the markup on the client side.

For example, here is one (non-XHTML) way you could do this:

In the HTML file:

<form method="post" action="myform.php" id="myform">
  ... form items here ...

<input type="submit" name="enter_key" value="true" style="display:none">
<input type="hidden" name="pressed_button" id="pressed_button" value="false">
<input type="button" value="Submit"
  onclick="document.getElementById('pressed_button').value='true';document.getElementById('myform').submit();">
</form>

In myform.php:

if ($_POST['pressed_button']=='false') {
   // Logic for enter key
} else {
   // Logic for button press
}
Andy Baird