Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Drop down menu with case statement assigning result to variable

Options
  • 21-12-2006 11:05am
    #1
    Closed Accounts Posts: 78 ✭✭


    Hi,

    I am a PHP beginner. This is due to no other options being available to me. I know little or nothing about how to use it and really need some help.

    I don't know if what I need to do is possible.

    I need to have a form on my page. The results of the form need to be assigned to variables. All of them will be txt boxes except one!!

    The hard part (at least for me) is that one of the fields in the form needs to be a drop down menu.

    When users pick that option, I need to run a case statement where when there is a match, it will assign an email address to a variable so I can use this to send an email to the person selected.

    Any help would be greatly appreciated.
    J


Comments

  • Registered Users Posts: 683 ✭✭✭Gosh


    The PHP Case statement is simple to use

    [php]
    switch ($variable_to_be_tested) {
    case "value_1" :
    // action to be taken when $variable_to_be_tested = "value_1"
    break;
    case "value_2" :
    // action to be taken when $variable_to_be_tested = "value_2"
    break;
    case "value_3" :
    // action to be taken when $variable_to_be_tested = "value_3"
    break;
    default :
    // action to be taken when $variable_to_be_tested is none of the above values
    }
    [/php]


  • Registered Users Posts: 1,393 ✭✭✭Inspector Gadget


    You need to look at HTML forms - namely, the <SELECT> tag for the drop-down menu. A simple example of this would be to stick a simple form into the page (inside the <BODY> tags), such as:
    <form action="processmyform.php">
    <input type="text" name="textfield_1" value="Text Field 1 Contents">
    <select name="dropdown_1">
    <option value="returned_value_1">Drop down option 1</option>
    <option value="returned_value_2">Drop down option 2</option>
    <input type="submit" value="Submit this form for processing">
    </select>
    </form>
    

    That'll give you a form containing:
    1. A text field, containing "Text Field 1 Contents" by default.
    2. A drop-down list, containing two options: "Drop down option 1" and "Drop Down option 2"
    3. A button with "Submit this form for processing" written on it.

    Then, once you've clicked "Submit this form for processing" to send the form data to your PHP script ("processmyform.php" in the example above), you can extract the selections you made by looking at the contents of the "magic" $_REQUEST variable. This variable contains the stuff submitted to the form; in this case, the contents of the text box (accessible as [PHP]$_REQUEST["textfield_1"][/PHP] - yes, $_REQUEST an array - use print_r($_REQUEST) to see exactly what's in it) and the value assigned to the selected option in the drop-down list (stored as [PHP]$_REQUEST["dropdown_1"][/PHP], after the name of the <SELECT> tag). Then you can use code such as the example provided by Gosh above.

    Hope this helps,
    Gadget


Advertisement