Creating Drop-down Form With Redirection Using PHP

I don’t know how best to title this post, anyway that by the way side. while writing a script days back, i created a form with drop-down containing list of my friends sites such that when one is selected and the submit button is clicked, he/she will be redirected to the said site.

Below is the PHP code to handle the redirection when an option is chosen. since it is sending a header, you need to make sure that the script doesn’t output anything before the it else, you get an Header already sent error.


<?php if(isset($_POST['submit'])) 
{ $part = $_POST['website'];
switch ($part) 
{ case "1" : header( "Location: http://www.google.com/" );
break;
case "2" : header( "Location: http://www.facebook.com/" );
break;
}
}
?>

Below is a sample HTML form.


<form method="post" action="">
<select name="website">
<option value="1">google</option>   
<option value="2">facebook</option>
</select><input type="submit" name="submit"/>
</form>
Don’t miss out!
Subscribe to My Newsletter
Invalid email address