1

I have a problem with my form. I need it to redirect user to different pages basing on which radio button was selected. User has to choose one of two options and click next, and according to his choice, page should redirect him to other page.

Here is the code as it looks for now

        <fieldset>
        <legend>Select option</legend>

        <center>
        <form method="post" action="">
        <input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
        </br>
        <input type="radio" name="radio2" value="Firma"/>Company
        </br>
        <input type = "submit", class = "buttonStyle2", value=""/>
        </form>
        </center>
    </fieldset>

and then php code

 if(isset($_POST['Company'])

header("Location: http://myaddress.com/company.php"); 

Big thanks in advance for your help

ltantonov
  • 145
  • 1
  • 6
  • 13
  • `if(isset($_POST['radio1']) && ($_POST['radio1']) == "Osoba fizyczna"){ // redirect }` but name both radio buttons the same name `name="radio1"` and change the condition. – Funk Forty Niner Aug 15 '14 at 17:49

2 Answers2

0
<fieldset>
    <legend>Select option</legend>
    <center>
    <form method="post" action="">
    <input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
    </br>
    <input type="radio" name="radio1" value="Firma"/>Company
    </br>
    <input type = "submit" class = "buttonStyle2" value=""/>
    </form>
    </center>
</fieldset>

and

if ( isset($_POST['radio1']) ) {
    $filename = $_POST['radio1'] . "php";
    header("Location: http://myaddress.com/".$filename); 
}

Might be even better to set up an array with allowed values and check if radio1 is in that array.

RST
  • 3,899
  • 2
  • 20
  • 33
0

Here is one way to achieve this.

Sidenote: Make sure you're not outputting before header. Consult this page on Stack about possible Headers already sent..., should this occur and making sure error reporting is set/on.

Otherwise, PHP will fail silently.


if(isset($_POST['radio1']) && ($_POST['radio1']) == "Osoba fizyczna"){

    header("Location: http://www.example.com/non_company.php"); 

 }

elseif(isset($_POST['radio1']) && ($_POST['radio1']) == "Firma"){

    header("Location: http://www.example.com/company.php"); 

 }

 else{
        header("Location: http://www.example.com/redirect_to_home.php"); 
}

Nota: The else would be if the person did not make a choice and simply clicked on submit without making a selection.

while using radio buttons of the same group name in your form:

<input type="radio" name="radio1" value="Osoba fizyczna"/>Non-company
</br>
<input type="radio" name="radio1" value="Firma"/>Company

Note about

<input type = "submit", class = "buttonStyle2", value=""/>

remove the commas

<input type = "submit" class = "buttonStyle2" value=""/>

Since HTML source in FF will reveal No space between attributes in red/as an error.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141