Hello,

I'd like to be able to have two submit buttons which call the same script and process the same form. What I am not understanding is how I can determine which button was clicked in the subsequent script. A simple code snippet of form is below:

<form name="form" method="post" action="doScript.php">
<input type="text" name="field1">
<input type="text" name="field2">
<input type="text" name="field3">
<input type="submit" name="addMore" value="Add More">
<input type="submit" name="saveTask" value="Finished">
</form>

I want to be able to determine which submit button was clicked ("addMore" or "saveTask") in the subsequent doScript.php script.

Note: when I proccess the forms input as follows (doScript.php snippet), there are no references to the submit buttons

$keys = array_keys($POST);
for ( $i = 0; $i < sizeof($keys) - 1; $i++ ) {
$$keys[$i] = $
POST[$keys[$i]];
}

Any ideas?

Thanks

Andesa

    why not use a radiobutton just before submitting?
    would make things easier

      $_POST contains the submit vars:
      addMore => Add More
      or
      saveTask => Finished

        Hi ir4z0r -

        Thanks for your reply. I thought about using a checkbox but that does not produce the desired asthetic effect. It's easier for a user to choose between buttons than it is to remeber to click (or unclick) a checkbox every time based on the action they want to perform.

        Appreciate your quick response though. Thanks!

        Andesa

          if (isset ($_POST['addMore'])) {... do something...}
          if (isset ($_POST['saveTask'])) {... do something else...}
          

            Hi RedDragon -

            Thanks for your quick post. I forget totally forgot about a direct reference to the specific variables because I have written a piece of code to retrieve all the $POST variables (see snippet below). Which leads me to another question...why would the values for the submit button not show up in the $POST array dump in this code snippet?:

            $keys = array_keys($POST);
            for ( $i = 0; $i < sizeof($keys) - 1; $i++ ) {
            $$keys[$i] = $
            POST[$keys[$i]];
            }

            Thanks again for your timely and helpful response!

            Andesa

              Hi devinemke -

              Got it. Thanks a bunch!

              Andesa


                $keys = array_keys($POST);
                for ( $i = 0; $i < sizeof($keys) - 1; $i++ ) {
                $$keys[$i] = $
                POST[$keys[$i]];
                }

                if you simply want to create variables out of the $POST array then just use the extract() function:

                extract ($_POST);
                

                this acheives the same as your code snippet with just one line. of course if you have register_globals on you don't even need the extract() function.

                  Hi devinemke -

                  That's handy! extract...how profoundly simple!

                  I do have register_globals set to OFF for improved security. This required me to rewrite some of my session code but in the end I believe the payoff is worth it.

                  Thanks again!

                  Andesa

                    Originally posted by devinemke
                    if you simply want to create variables out of the $POST array then just use the extract() function:

                    extract ($_POST);
                    

                    this acheives the same as your code snippet with just one line. of course if you have register_globals on you don't even need the extract() function. [/B]

                    OR

                    import_request_variables("gpc");

                    this command does the same as extract, but only for GET,POST and COOKIE. An optional 2nd parameter will append text to the beginning of similarly named variables.

                    so if you have a cookie and a POST variable named myvar
                    import_request_variables("pc","cookie_");
                    will grab the POST vars first, then the Cookies and create
                    $myvar and $cookie_myvar.

                    • keith

                      1 way that I've done this in the past is to name the submit buttons "formaction" and then test for the value of $_POST[formaction'] in a switch or if-else statement.

                      I found it a little easier to maintain doing it that way rather than assigning a different name to each button.

                      • keith

                        keith73

                        That's a great idea Keith73...I think I will go with that!

                        Where in NJ are you? I'm just over the PA border on I-78...

                        Andesa

                          Write a Reply...