Page 1 of 1

New to PHP-Send to URL based on what Radio btn is selected

Posted: Sat Oct 31, 2009 2:03 pm
by tjphilli
Hello all, i have a simple result to attain, but I'm new to PHP and i'm struggling figuring out how to approach this.

I need to have a form that the user fills out, and there is a section of radio buttons, and depending on the interests of the user, it will send the user to a page relevant to his interests, where there is more info on that topic. I know there is a simple conditional approach to this, but I'm struggling integrating it with the form, and the script i have to process the form and email it to the company for records. Thanks so much in advance!!!

Re: New to PHP-Send to URL based on what Radio btn is selected

Posted: Sat Oct 31, 2009 3:22 pm
by Mirge
tjphilli wrote:Hello all, i have a simple result to attain, but I'm new to PHP and i'm struggling figuring out how to approach this.

I need to have a form that the user fills out, and there is a section of radio buttons, and depending on the interests of the user, it will send the user to a page relevant to his interests, where there is more info on that topic. I know there is a simple conditional approach to this, but I'm struggling integrating it with the form, and the script i have to process the form and email it to the company for records. Thanks so much in advance!!!
Example form:

Code: Select all

 
<form action="redirect.php" method="post">
<input type="radio" name="site" value="1" /> Google<br/>
<input type="radio" name="site" value="2" /> Yahoo<br/>
<input type="radio" name="site" value="1" /> MSN<br/>
<input type="submit" name="submit" value="Submit" />
</form>
 
redirect.php:

Code: Select all

 
<?php
$site = $_POST['site'];
 
switch($site) {
    case '1':
        header("Location: http://www.google.com/"); exit;
 
    case '2':
        header("Location: http://www.yahoo.com/"); exit;
 
    case '3':
        header("Location: http://www.msn.com/"); exit;
}
?>
 

Hope this helps.

Re: New to PHP-Send to URL based on what Radio btn is selected

Posted: Sat Oct 31, 2009 3:29 pm
by markusn00b
I will assume you are POSTing the form data (that is, the form's method attribute is set to 'post').

You may use the header() function to redirect a user to a specific location:

Code: Select all

 
<?php
 
// I assume $_POST['choice'] is the form data
switch ($_POST['choice']) {
    case 'sports':
        header("location: http://yoursportssite/
        break;
    case 'porn':
        header("location: http://yourpornsite/
        break
    case 'internalsports':
        header("location: /yourinternalsportssite/");
        break;
}
 
Mark.

Edit: Whoops - beaten to it.