NewB
NewB

Reputation: 125

php page redirect dependant on select box choice

I'm trying to redirect to a page but based on the answer from a select box. Basicaly in the example below, how can I get the page to redirect to "thispage.php" if p1 is selected and "thatpage.php" if p2 is selected upon submitting the form via button? I apreciate any and all comments, thank you.

<html> 
<body> 
<form name="form1"> 
<select name="select1"> 
<option value="p1">p1</option> 
<option value="p2">p2</option> 
<input type="submit"/>
</select> 
</form> 
</body> 
</html>

Upvotes: 2

Views: 5642

Answers (3)

RobertPitt
RobertPitt

Reputation: 57268

why dont you add the page location as the value, for example

<select name="location" onchange="redirect()">
    <option value="http://google.com">Google</option> 
    <option value="profile.php">Profile</option> 
</select>

And then add this function to your javascript blocks / file

function redirect()
{
    document.location.href = this.value;
}

Upvotes: 0

timdev
timdev

Reputation: 62884

Basic:

   <?PHP
    function redirect($where){      
       header("Location: $where");
    }
    if ($_REQUEST['select1'] == 'p1'){
        redirect('http://example.com/somewhere.php');
    }elseif($_REQUEST['select1'] == 'p2'){
        redirect('http://example.com/elsewhere.php');
    }

Upvotes: 1

Theodore R. Smith
Theodore R. Smith

Reputation: 23231

This requires javascript. Do this:

<html> 
<head>
    <script type="text/javascript">
function redirect(page)
{
    if (page == 'p1')
    {
        window.location = '/thispage.php';
    }
    else if (page == 'p2')
    {
        window.location = '/thatpage.php';
    }
}
    </script>
</head>
<body> 
<form name="form1"> 
<select name="select1" onchange="redirect(this.value)"> 
<option> -- select option -- </option>
<option value="p1">p1</option> 
<option value="p2">p2</option> 
<input type="submit"/>
</select> 
</form> 
</body> 
</html>

Upvotes: 1

Related Questions