Looking for some help on how to create a simple form with php that would redirect the user based on the options selected in the form.

The user would see a series of check-box options and a submit button:

option 1
option 2
option 3
option 4
option 5

SUBMIT


If the user presses submit without making a selection, they should get an "Please make a selection." error message.

If the user checks Option 1 and/or Option 2, the submit button would send them to page1.html.

Otherwise, the submit button sends them to page2.html

Any assistance would be greatly appreciated.

    <form action="processfrom.php" method="post">
        <input type="checkbox" name="option1" value="1" /> Option 1
        <input type="checkbox" name="option2" value="1" /> Option 2
        <input type="checkbox" name="option3" value="1" /> Option 3
        <input type="checkbox" name="option4" value="1" /> Option 4
        <input type="checkbox" name="option5" value="1" /> Option 5
        <input type="submit" name="send" value="Submit" />
    </form>
    
    if($_POST['options1'] == 1 || $_POST['options2']  == 1){
        header("Location: http://www.yoursite.com/page1.html");
    }
    else {
        header("Location: http://www.yoursite.com/page2.html");
    }
    

      Appreciate the help. I'll give it a try and let you know how it works.🙂

        After some quick typo fixes "processform.php" and changing from "options1" to "option1" in the php, it worked! Thanks so much.

          also at the top of kirks little code peiece put

          if(! $_POST['submit']
          {
            // trigger_error();
            // Throw an Exception 
            // call an errors page 
            // re-load the form for processing.
          }
          
          elseif($_POST['option1'] == 1 )
          {
            // go to location
          }
          
          elseif($_POST['option2'] == 1 )
          {
            // go to loaction 2
          }
          
          else {
             // this location if no options selected.
             // or an error becuase nothing was selected.
          }
          
          
          
          
          
          
            9 months later

            Code from test.php (form)

            <form method="post" action="processform.php">
            <input type="checkbox" name="option1" id="option1" value="1" /><br />
            
            <input type="checkbox" name="option2" id="option2" value="1" /><br />
            
            <input type="checkbox" name="option3" id="option3" value="1" /><br />
            
            <input type="checkbox" name="option4" id="option4" value="1" /><br />
            <input type="submit" name="send" value="Submit" />
            </form>

            Code from processform.php

            
            if($_POST['option1'] == 1 ){
                header("Location: http://www.google.com");
            }
            if($_POST['option2'] == 1 ){
                header("Location: http://www.yahoo.com");
            }
            if($_POST['option3'] == 1 ){
                header("Location: http://www.bing.com");
            }
            if($_POST['option4'] == 1 ){
                header("Location: http://www.tinkergraphics.com");
            }
            else {
                header("Location: http://www.floridataxreduction.com");
            } 
            

              Your problem is with the logic. In that code, only two statements will take effect - the tinkergraphics.com or the floridataxreduction.com.

              Each if() statement is independent of the others, so each one will be separately evaluated. Since you probably only want one if() branch to be taken rather than multiple, you should be using 'else if' instead.

              Also, what is supposed to happen if I check boxes 1 and 3? How bout 2 through 4?

                Thanks - the below now works...

                if($_POST['option1'] == 1 ){
                    header("Location: http://www.google.com");
                }
                elseif($_POST['option2'] == 1 ){
                    header("Location: http://www.yahoo.com");
                }
                elseif($_POST['option3'] == 1 ){
                    header("Location: http://www.bing.com");
                }
                elseif($_POST['option4'] == 1 ){
                    header("Location: http://www.tinkergraphics.com");
                }
                else {
                    header("Location: http://www.floridataxreduction.com");
                } 

                ... except for the scenarios you mentioned - choosing multiple boxes - which I hadn't thought about. How would I implement whenever anyone checked more than one box they go to a certain url? This could be the same as the "else" url.

                  It was the client's request to have the check boxes. It does look better for what they're using it for. We could use radio buttons, but I thought there would be a solution with the check boxes.

                    tinkerg;10982364 wrote:

                    I thought there would be a solution with the check boxes.

                    Well sure, there could be a solution if you write the logic to cover all of the situations... but why not use the right tool for the right job? You wouldn't tell a contractor to use duct tape to build you house because you were fond of the silver color, so why use checkboxes when the behavior you want matches radio buttons instead?

                      2 months later

                      If I could just come back to the radio button issue. I have a form where I need a redirect to a different page if TWO specific radio buttons are selected. How will the php code be different in this case (using the "options" example above)?

                        Use logical operators (manual page: [man]operators.logical[/man]) to form a boolean expression that matches the conditions that must be met.

                          Write a Reply...