Jump to content

Dropdown Menu form validation


CharlieJW

Recommended Posts

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
Link to comment
Share on other sites

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...

[code]<?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>[/code]
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.
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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:
[code]<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>[/code]
Then in the PHP section you would have something like:
[code]<?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" />';}
}[/code]
Now all you need is to display the image in the HTML section. Just after the </form> tag add this line:
[code]<?php echo $img; ?>[/code]
You can really place that pretty much wherever you want but it must be between the <body> and </body> tags.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.