Jump to content


Photo

Dropdown Menu form validation


  • Please log in to reply
5 replies to this topic

#1 CharlieJW

CharlieJW
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 07 April 2006 - 10:50 PM

Hi, im stuck! I would like to create a form that includes a drop down menu to determin the validation type. For example, i have a dropdown menu with 3 options: Home / Shop / Contact. If the home is selected then when i press the submit button i would like it to take me to home.html, if shop was selected then i would like the submit button to take me to shop.html, if contact was selected then i would like the submit button to take me to shop.html, i think php is the correct language because i cant find a way of doing it in html.

Thankyou in advance!
Charlie

#2 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 07 April 2006 - 11:37 PM

Hi, this is completely off the top of my head so I can't guarantee that this will work - I'm going to attempt to write the script...

<?php
if ($_POST['subchoose']) {
  $opt=$_POST['opt'];
  switch ($opt) {
    case 1:
      header("Location: home.php");
      exit;
      break;
    case 2:
      header("Location: shop.php");
      exit;
      break;
    case 3:
      header("Location: contact.php");
      exit;
      break;
  }
}
?>
<html>
<body>
  <form action="menu.php" method="post">
  <select name="opt"><option value="1">Home</option><option value="2">Shop</option><option value="3">Contact</option></select> <input type="submit" name="subchoose" value="Make Choice" />
  </form>
</body>
</html>
Thats the basic idea. The header() function is to change part of the header of the document, in this case the URL location and pointing it to the correct script. I've used exit() after to stop the script running any further should it have a problem changing the header for whatever reason.

When you run the script the if() function will be false because the submit button hasn't been pressed. When it is, the if() becomes true and the rest is checked. If the menu option isn't valid (1, 2 or 3) then the menu appears again.

One final word on header() - you can only change the header if nothing has been sent to the browser before its called so avoid using anything that will send output to the browser. If you don't, you'll get a message telling you the headers couldn't be modified.

Hope this helps.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#3 CharlieJW

CharlieJW
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 08 April 2006 - 09:15 AM

Thank's but now I would like to create a form that includes 2 drop down menus to determin the validation type. For example, i have 2 dropdown menus: 1 that has colour and 1 that has size, if blue is selected i would like to display image: blue.gif and If i have blue and large selected then when i press the submit button i would like it to take me to bl.html , how can i do this?

#4 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 08 April 2006 - 09:32 AM

If you look at the above script you can see that the definitions for the drop-down box are enclosed in <select> and </select> tags. The options are defined usign <option> and </option> tags. All you need to do is create another set of these tags inside the same form with a list of what you want and modify the PHP part to take action on the selected option(s).

For example:
<form action="menu.php" method="post">
<select name="color"><option value="red">Red</option><option value="green">Green</option><option value="blue">Blue</option></select> <select name="size"><option value="sm">Small</option><option value="md">Medium</option><option value="lg">Large</option></select> <input type="submit" name="subchoose" value="Make Choice" />
</form>
Then in the PHP section you would have something like:
<?php
$img="";
if ($_POST['subchoose']) {
  $optcol=$_POST['color'];
  $optsize=$_POST['size'];
  if ($optcol=="blue"&&$optsize=="lg") {
    header("Location: bl.html");
    exit;
  }
  if ($optcol=="blue") {$img='<img src="blue.gif" border="0" />';}
}
Now all you need is to display the image in the HTML section. Just after the </form> tag add this line:
<?php echo $img; ?>
You can really place that pretty much wherever you want but it must be between the <body> and </body> tags.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them

#5 CharlieJW

CharlieJW
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 08 April 2006 - 10:04 AM

thanx


#6 CharlieJW

CharlieJW
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 08 April 2006 - 11:37 AM

Sorry to be a pest, but what would the code be if i wanted size small to come up only on color red and not on green and blue but green and blue only have medium and large?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users