DarkPrince2005 Posted February 29, 2008 Share Posted February 29, 2008 Can someone explain and give me an example of how i would do the following: I want to create a form with a drop down box and when the form is submitted the user would be redirected according to the selection he or she made in the drop down box. Here is a sample of what the drop down box would look like: <form method="post" action=""> <center> <table width="600" cellpadding="0" cellspacing="0"> <tr> <td> <table> <tr> <td><b>I need:</td> <td> </td> <td><select name="status"> <option value="-SELECT-">-SELECT-</option> <option value="On Site Support">On Site Support</option> <option value="Website Development">Website Development</option> </select></td> </tr> </table> </td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/ Share on other sites More sharing options...
fooDigi Posted February 29, 2008 Share Posted February 29, 2008 u can use javascript and use the "onchange" event on the <select> tag.... but with php, if you submit this form with the given elements, make a action="this.php" page with this... <?php if($_POST['status'] == "On Site Support") header("Location: url1.php"); elseif($_POST['status'] == "Website Development") header("Location: url2.php"); else exit("You didn't give a valid redirection point."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/#findComment-479765 Share on other sites More sharing options...
voyde Posted February 29, 2008 Share Posted February 29, 2008 <? if (isset($_POST['submit'])) { $site = $_POST[status]; header('Location:' . $site .''); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Redirect from forms</title> </head> <body> <form method="post" action=""> <center> <table width="600" cellpadding="0" cellspacing="0"> <tr> <td> <table> <tr> <td><b>I need:</td> <td> </td> <td><select name="status"> <option value="-SELECT-">-SELECT-</option> <option value="http://google.com">On Site Support</option> <option value="http://yahoo.com">Website Development</option> </select> <input type="submit" name="submit" value="submit" /></td> </tr> </table> </td> </tr> </table> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/#findComment-479767 Share on other sites More sharing options...
vbnullchar Posted February 29, 2008 Share Posted February 29, 2008 heres a simple example hope youll find it usefull <script> function go(){ var k = document.getElementById('sites'); location.href=k.value; } </script> <select id="sites" name='sites' onchange='go();'> <option value="http://google.com">site 2</option> <option value="http://yahoo.com">site 2</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/#findComment-479768 Share on other sites More sharing options...
fooDigi Posted February 29, 2008 Share Posted February 29, 2008 voyde, i think it is better practice to filter and try and examine any possible user input. with your method someone could possibly place whatever they want within the 'status' variable. maybe in this instance, it's not a big problem. but, someone could still play with your code. Quote Link to comment https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/#findComment-479769 Share on other sites More sharing options...
moon 111 Posted February 29, 2008 Share Posted February 29, 2008 Using case would be cleaner <?php switch ($_POST['status']) { // Couldn't use spaces becuase it was messing up the way it showed on the forum case "On Site Support": header("Location: url1.php"); break; case "Website Development": header("Location: url2.php"); break; default: exit("You didn't give a valid redirection point."); break; } ?> or <?php header("Location: {$_POST['status']}"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Redirect from forms</title> </head> <body> <form method="post" action=""> <center> <table width="600" cellpadding="0" cellspacing="0"> <tr> <td> <table> <tr> <td><b>I need:</td> <td> </td> <td><select name="status"> <option value="-SELECT-">-SELECT-</option> <option value="location1.php">On Site Support</option> <option value="location2.php">Website Development</option> </select> <input type="submit" name="submit" value="submit" /> </td> </tr> </table> </td> </tr> </table> </form> </body> </html> On this one change the values of the selects to the location you want them to go to. Quote Link to comment https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/#findComment-479771 Share on other sites More sharing options...
fooDigi Posted February 29, 2008 Share Posted February 29, 2008 great point, moon. was thinking that while writing it. probably going to be more options than just the two. and whenever u can use a switch instead of an if, use it, right? Quote Link to comment https://forums.phpfreaks.com/topic/93636-solved-form-with-if-statements/#findComment-479772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.