How do I redirect to different pages from a drop down list in php?
When users come to my home page I want them to select which page they want to go to. So, for in开发者_运维技巧stance sports, trivia, etc. from a drop down list. How would I get it to redirect to the specific page that is selected using php? This should be simple but I haven't found the solution by searching google.
PHP run serverside and thus can't handle the redirect in the HTML client. However, you can use PHP to dynamicly print your dropdown as needed. Then you add a onSelect function to your dropdown that either does the redirect itself using javascript:window.location="foobar.html" where foobar.html is the page you want to redirect to. Or you make the onSelect fire a javascript function within which you handle the actual redirect and can look at several elements in your page to decide where to redirect the client to.
Good luck!
<?php
if ($_POST['select']){
header("Location: ".$_POST['select']);
}
?>
<form type="post" action="">
<select name="select">
<option value="http://link1.com">link1</option>
<option value="http://link2.com">link2</option>
<option value="http://link3.com">link3</option>
<option value="http://link4.com">link4</option>
<option value="http://link5.com">link5</option>
</slect>
<input type="submit" />
</form>
You should think of avoiding drop downs to handle this, and use links instead. However, if you are really into it, here is the solution:
PHP:
if(isset($_POST['redirect'])){
header('Location: '.$_POST['link']);
exit;
}
HTML:
<form action="" method="post">
<select name="link">
<option value="http://www.site.com/page1">Page 1</option>
<option value="http://www.site.com/page2">Page 2</option>
<option value="http://www.site.com/page3">Page 3</option>
</select>
<input type="submit" name="redirect" value="GO" />
</form>
精彩评论