Question

I was wondering if it is possible to make each radio button in a form link to a different action php page? say if I had 2 radio buttons, one named 'basketball' and one named 'football' would I be able to have them link to different php? here is my code;

<form action="football.php" method="post">
<p>Please select your first Sport:</p>
<input type="radio" name="sport" value="football">Football<br></input>
<input type="radio" name="sport" value="basketball">Basketball<br></input>
<input type="radio" name="sport" value="tennis">Tennis</input>
<br><input type="Submit" name="Submit" value="Submit"></form>
Was it helpful?

Solution

Is there any reason they need to be distinct pages?

form.php

<form action="sport.php" method="post">
<p>Please select your first Sport:</p>
<input type="radio" name="sport" value="football">Football<br></input>
<input type="radio" name="sport" value="basketball">Basketball<br></input>
<input type="radio" name="sport" value="tennis">Tennis</input>
<br><input type="Submit" name="Submit" value="Submit"></form>

sport.php

<?php
    if (isset($_POST["sport"])
    && $_POST["sport"] == "football") {
        //logic specific for football
    } else if (isset($_POST["sport"])
    && $_POST["sport"] == "basketball") {
        //logic specific for basketball
    } else if (isset($_POST["sport"])
    && $_POST["sport"] == "tennis") {
        //logic specific for tennis
    } else {
        //die or some kind of error handling can be done
    }
?>

If they absolutely need to be different pages, you can do something like the below:

sport.php

<?php
    echo "<meta http-equiv='refresh' content='0;url=./dir/subdir/".$_POST["sport"].".php'/>";
    //so if posted form data == football, redirect to football.php, etc
?>

Sure it lacks finesse, but you won't be able to get your desired outcome otherwise, unless you use jQuery/JS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top