Solved

How to detect which submit button was being pressed and which hidden input value was being sent?

Posted on 2010-09-21
3
410 Views
Last Modified: 2012-05-10
Hi Experts,

I have a php driven website and html form which contains multiple submit buttons and each submit button is been accompanied with a hidden input field containing data for that specific submit button. Earlier I had each submit button + hidden input field inside separate forms but in order to reduce the page loading time, I'm trying to have only one form covering all the submit buttons and hidden input fields.

Now the question is:
How can I detect which submit button was being pressed and which hidden field should be sent?

Ideas:
- Include the needed data from the hidden field into the submit button id value (each hidden data is unique containing date time info etc.). But this needs a method to access the id value later on and apparently php can only retrieve the value, am I right?
- Hmm.. maybe what if I change the submit button name into the unique hidden data. Then I'd have to have a while loop to go through all the possible choices...

Apparently I need some new ideas here.

The included image illustrates a bit more the situation.


 illustration of the situation
0
Comment
Question by:idusty
3 Comments
 
LVL 26

Accepted Solution

by:
jar3817 earned 500 total points
ID: 33726915
You can tell which submit button is pressed by testing for it's name as a variable. If you hit button with name='submit1' then in the php script check if $_POST['submit1'] has the value of the button.

As for the hidden fields, ALL of them will get sent when the form is submitted. You should name them accordingly so you can easily tell which ones you want. If butt submit1 is pressed, submit1_hiddenX values are the ones you want to check out. Make sense?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33727400
There is almost zero time penalty to using separate forms, and your code will be much more modular and easy to modify.

You can install this script and run it to see the moving parts.  With one form or many on the page, the strategy is the same.
<?php // RAY_submit_buttons.php
error_reporting(E_ALL);
echo "<pre>\n";

// IF ANYTHING HAS BEEN POSTED
if (!empty($_POST))
{
    // TEST TO SEE WHICH SUBMIT BUTTON WAS FIRED
    if (isset($_POST["sb1"]))
    {
        echo "SUBMITTED BUTTON ONE" . PHP_EOL;
    }
    if (isset($_POST["sb2"]))
    {
        echo "SUBMITTED BUTTON TWO" . PHP_EOL;
    }
    if ( (!isset($_POST["sb1"])) && (!isset($_POST["sb2"])) )
    {
        echo "NO NAMED SUBMIT BUTTON USED" . PHP_EOL;
    }

    // VISUALIZE THE POSTED DATA
    var_dump($_POST);

}
// END OF PHP, PUT UP THE FORM
?>
<form method="post">
<input name="st" />
<input type="submit"            value="Button With No Name" />
<input type="submit" name="sb1" value="Button One" />
<input type="submit" name="sb2" value="Button Two" />
</form>

Open in new window

0
 
LVL 1

Author Closing Comment

by:idusty
ID: 33728926
Ah... it sure was that simple. Of course. Thank you though for putting it down in such a simple way.

I was able to chop 40 to 80kb from every page load. When you have to load the page many times in a row it suddenly starts to make a difference. Great. Thanks.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question