1

I looked over stack for an answer with no success. Im looking to make a form with radio buttons. Depending on the radio buttons that are checked, pressing the submit button will direct you to a different page. Right now im using php with the get option. However the best im able to do is make dynamic pages based of the get info. Any help would be great

Thanks

2
  • PHP can't do this, since PHP executes only on the server and has no control over the client once the page has been loaded. You'll need to use Javascript to dynamically redirect. Commented Jun 10, 2012 at 18:32
  • 1
    With the GET info, do a conditional statement and use HEADER to redirect someone to the desired page. php.net/manual/en/function.header.php Commented Jun 10, 2012 at 18:33

2 Answers 2

6

In your PHP you could check $_GET['radio_option'] and based on the value redirect to other pages using header function, something like this:

switch($_GET['radio_option']) {
case 'val1':
    header('location: page1.php');
    exit;
case 'val2':
    header('location: page2.php');
    exit;
case 'val3':
    header('location: page3.php');
    exit;
}

//here handle everything else - although normally you shouldn't get here

The other alternative would be to use javascript to set the action attribute of the form before it is submitted. For example:

$(document).ready(function() {
    $("input:radio[name=your_radio_naem]").click(function() {
        var value = $(this).val();
        var target = "main.php";

        if(value == 'val1')
            target = "page1.php";
        else if(value == 'val2')
            target = "page2.php";
        else if(value == 'val3')
            target = "page3.php";

        $('#myform').attr('action', target);
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Another way would be to set each radio button with a value of the redirect page.

$page=$_GET['radio_buttons'];
header('location: $page');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.