PHP Form Submission: Recognize Image Input Buttons

By  on  

As you probably know, you can recognize a form submission from a "submit" input type by placing the following code in the "processing" PHP script:

if(isset($_POST['submit'])) { /* do stuff */ }

Did you know, however, that when using an "image" input type to submit the form, the above wont work? You need to add a "_x" to the field name in PHP:

if(isset($_POST['submit_x'])) { /* do stuff */ }

Odd, huh? This works the same when using a form "GET" method.

Recent Features

  • By
    CSS Animations Between Media Queries

    CSS animations are right up there with sliced bread. CSS animations are efficient because they can be hardware accelerated, they require no JavaScript overhead, and they are composed of very little CSS code. Quite often we add CSS transforms to elements via CSS during...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Get Slick with MooTools Kwicks

    When I first saw MooTools graphical navigation, I was impressed. I thought it was a very simple yet creative way of using Flash. When I right-clicked and saw that it was JavaScript, I was floored. How could they achieve such...

  • By
    Animated AJAX Record Deletion Using MooTools

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with MooTools JavaScript. The PHP - Content & Header The following snippet goes at the...

Discussion

  1. The _x and _y represent the coordinate location you clicked the image at.

  2. Think this is only an issue with IE.

  3. Yeah. The _x- and _y-coordinates are great for improving the security of a form! I’ve used this to determine if the form has been filled by a human. A spam-bot won’t submit any coordinates but a human has to click on the button and so there will always be coordinates (you’ll have to deactive submitting with the ENTER-button).

  4. @Matthias: Good point on the security enhancement — I’ve never though of that!

  5. Braxo

    @ Matthias

    Thanks for posting your comment. I think telling the user that the ENTER button has been deactivated for bot protection is easier than having the user type in a captcha.

    I’ll definitely be looking into that method and most likely incorporating it into my projects.

  6. @Braxo – Wait – “Enter button” is deactivated? How would this affect someone who cannot use a mouse/relies on accessibility tools to fill out forms and the like?

    Some sites cannot get away with it (coughtargetcough).

  7. You can save yourself the trouble and just give the input a name attribute and check for that. Saves from changing code in two places (the input and the PHP submit validation).

    <input type="image" src="image.png" name="submitted" value="Submit" />

  8. I should clarify that…

    It saves from changing code in 2 places should you want to change to/from an image submit or a standard submit.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!