Solved

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

Posted on 2010-09-21
3
405 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 108

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now