tags:

views:

24

answers:

3

Hello all, 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>

​

+1  A: 

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>
hopeseekr
This is if he wants to redirect client-side.
Brian Ramsay
The question was tagged PHP, and this does not *require* javascript.
timdev
His question is very ambivalent.
hopeseekr
Ok timdev's response was dead on but now I'm noticing that the responses to the form are not being posted to the next page when redirecting. Any ideas why? forgive my ignorance but I'm only about 2 days into my PHP learning.
NewB
Because browsers are dumb, unfortunately. I believe the RFCs say that when given a redirect status, browsers are supposed to submit the same request to the new URI, but instead they just GET it, which is dumb. Hopeseekr's answer might be a good place to start to work around it. You might want to use javascript to change the action attribute of the form when different options are selected. That way there's no redirect, the form just submits to the appropriate script directly.
timdev
+1  A: 

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');
    }
timdev
timdev hit the nail on the head, thank you! thanks to all else who replied as well.
NewB
A: 

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

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

And then add this function to your javascript blocks / file

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