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

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
LVL 1
idustyAsked:
Who is Participating?
 
jar3817Commented:
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
 
Ray PaseurCommented:
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
 
idustyAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.