Hi,

I have a HTML form with 3 buttons, Save, clear and search. I added action ="submitdata.php" to the form, then I wrote different php code for save and clear buttons, but when click search button, I want to invoke another search.php. What should I do?

Thanks

hongmei

    There should be only one ACTION for your form, but your SUBMIT buttons are actually passed as variables with values.

    Therefore if you have a form with three buttons:
    SEARCH
    SAVE
    CLEAR
    then the variable $POST['submit'] can have three possible values dependant upon which button was clicked. Make the ACTION of the form be ACTION="process.php" METHOD=POST. So if you create a php file called "process.php" and inside it have a simple switch;

     
    //File: process.php
    //Function: Checks form submission and performs relevant actions
    switch ($_POST['submit']) {
      case "SEARCH":
      include "search.php";
      break;
      case "SAVE":
      include "savedata.php";
      break;
      case "CLEAR":
      include "clear.php";
      break;
      default:
      include "formerror.php";
    }
    

    In this example I assume that your CLEAR button actually does something other than reset the form. If you simply want the form reset then that button should be an <input type="reset" value="RESET"> which subsequently does not require any receiving script.

      Thanks a lot , It worked very well!

      hongmei

        You can also use onClick mouse-event to direct to another php page.

          7 days later

          Hi,

          Could you please tell me how to code onClick event?

          if I have a button called test and I added onClick="test.php", why didnot it work?
          <input type="Submit" name="test" value="test" onClick="test.php"> --this is mypage.php

          Look at the following code please:

          if (isset($_POST['test']))
          {

          include "test.php";

          }

          When I click test button, it shows me the test.php on screen. But the url address still points to the original php --http//myserver/mypage.php.
          How can I change url address to http//myserver/test.php ?
          I think I need a window.open("test.php"); not include "test.php";
          But I donot think "window.open" is working with php.

          What should I do?

          Thanks again!

          hongmei

            This is pretty basic html really, NOT php.

            A FORM has an action. That action is invoked by the clicking of a button of Type="submit". Therefore if you have a type=submit button with an onclick javascript event attached to it, you'll simply confuse your form.

            <form action=test.php method=post>
            <!-- Rest of form -->
            <input type=submit value=SUBMIT>
            </form>

            Thats it. If you want your porm to have different possible outcomes, simply add another standard submit button to it with a new value eg <input type=submit value=SEARCH>

            then just use the "switch()" I posted above (modify) to determine what to do according to which value $_POST['submit'] gets passed with.

            If the "SUBMIT button is clicked the value of $_POST['submit'] will be "SUBMIT"

            If the "SEARCH button is clicked the value of $_POST['submit'] will be "SEARCH"

              Mouse events are not PHP. They are often called dynamic HTML based on javascript. They could be handy in some cases. Here is an example. Just copy it and fill in a valid php file name, and test it out.

              <FORM><INPUT TYPE="button" VALUE="Take me to that file" onclick="location.href('file.php')"></FORM>
              

                Your location.href() won't work in php at all.
                $POST['Submit'] didnot get any value. You only get value for button if you code isset($POST['save']) or isset($POST['search']). Submit is type of the item, is not name of the item. $POST only links with name not type.

                Is there any other way to invoke new php except using "include test.php"? When I use "include", it does call test.php, but url address still display old.php page.

                Thanks

                  There will be no variable named $_POST['save'];

                  Granted , my button was slightly incomplete as I neglected to include the name=submit ie;
                  <input type=submit name=submit value=SUBMIT> but as that's basic html I assumed you might have spotted it.

                  Bear in mind your $_POST[] variables are cASe sensitive aswell.

                  Pauls' onclick="location.href('file.php')" will work but it will not take any undefined form field name/value pairs with it (ie those not declared in the javascript control).
                  Remember, a form can only have one defined action upon proper submit and can therefore only have one receiving page.

                    Write a Reply...