简体   繁体   中英

php redirect using a form

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

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);
    });
});

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM