Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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 …

920 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

16 Experts available now in Live!

Get 1:1 Help Now