www.webdeveloper.com
Results 1 to 11 of 11

Thread: Validate Selected Option in PHP Only

  1. #1
    Join Date
    Nov 2004
    Location
    Washington, DC
    Posts
    49

    Exclamation Validate Selected Option in PHP Only

    I'm looking to validate whether or not a user selects an option from a drop down menu:

    PHP Code:
    <select name="state[]">
    <
    option value="NULL">-- Please select a state --</option>
    <
    option value="AK">AK Alaska</option>
    <
    option value="AL">AL Alabama</option>
    ...
    <
    option value="WY">WY Wyoming</option>
    </
    select
    How do I check if they leave the default state selected? I really am having a hard time with how PHP interprets selected options (Please no Javascript.)

    Thanks!

  2. #2
    Join Date
    Aug 2003
    Location
    In my house
    Posts
    4,004
    Something like this?
    PHP Code:
    <?php
        
    if($_POST) {
            if(isset(
    $_POST['state'])) {
                if(
    $_POST['state'] == 'NULL') {
                    echo 
    '<p>Please select an option from the select box.</p>';
                }
                else {
                    echo 
    '<p>You have selected: <strong>'$_POST['state'], '</strong>.</p>';
                }
            }
        }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <legend>Please select a state</legend>
            <select name="state">
                <option value="NULL">-- Please select a state --</option>
                <option value="AK">AK - Alaska</option>
                <option value="AL">AL - Alabama</option>
                <option value="WY">WY - Wyoming</option>
            </select>
            <input type="submit" name="submit">
        </fieldset>
    </form>
    Thousand different paths
    So many sterile ends
    I chose the Devil's path

    Never shall the sun kiss my face
    And caress me with it's burning light
    For I dwell in the shadows
    And sleep side by side with death

  3. #3
    Join Date
    Nov 2004
    Location
    Washington, DC
    Posts
    49
    Something like that. Thank you very much.

    Out of curiosity, why didn't you name the "name" field in select tag to "state[]"? How would you validate $_POST['state'] if it were stored in an array?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    Originally posted by kouki0196
    Something like that. Thank you very much.

    Out of curiosity, why didn't you name the "name" field in select tag to "state[]"? How would you validate $_POST['state'] if it were stored in an array?
    The string you assign in the HTML form's SELECT element's NAME parameter is simply an identifier that is associated with the value that the user selects. Depending on which method the form uses (POST or GET), PHP adds it to the $_POST or $_GET array, using the NAME as the array element key and the VALUE as the array element value.

    If you used the <select name="state[]"> syntax from your example, PHP would create an array element called $_POST['state[]'], which I think you would agree would be a bit confusing?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Nov 2004
    Location
    Washington, DC
    Posts
    49
    Originally posted by NogDog
    The string you assign in the HTML form's SELECT element's NAME parameter is simply an identifier that is associated with the value that the user selects. Depending on which method the form uses (POST or GET), PHP adds it to the $_POST or $_GET array, using the NAME as the array element key and the VALUE as the array element value.

    If you used the <select name="state[]"> syntax from your example, PHP would create an array element called $_POST['state[]'], which I think you would agree would be a bit confusing?
    Thanks, NogDog. I appreciate you taking the time to explain this part to me.

    So, for sake of simplicity, it is better to use "state" instead of "state[]", right? However, if I wanted to provide multiple selections to the drop down, then "state[]" would become vital since you need to store all of the selected values in the menu, right?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    If you want multiple selections, in that case your HTML needs the "multiple" keyword, and you do want to use the array-style name (I just learned something new today ):
    PHP Code:
    <select multiple name="state[]"
    See this page and this FAQ for a bit more info.

    In your example, I guess validation would become more complex:
    PHP Code:
    if(! isset($state) or count($state) == or in_array("NULL"$state))
    {
      echo 
    "<p>ERROR: You must select a state.\n";

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Nov 2004
    Location
    Washington, DC
    Posts
    49
    Double Thank you!!!

  8. #8
    Join Date
    Jan 2014
    Posts
    1
    This code is working fine for me. But i could not understand of using three if loops in start. Can i use only one if loop instead of three?
    PHP Code:
    if($_POST)
            {
                if(isset(
    $_POST['category'])=='NULL')
                {
                    if(
    $_POST['category']=='NULL')
                    {
                        echo 
    " <font color=red><b>Please select one category</b></font>";
                    }else
                    {
                        if(!empty(
    $_POST['subcat']))
                        {            
                            
    $category $_POST['category'];            
                            
    $subcategory $_POST['subcategory'];
                            if(!empty(
    $subcategory))
                            {
                                
    $query1 "select * from subcategories where catname = '$category' and subcat = '$subcategory'";
                                
    $run mysql_query($query1);
                                
    $nums mysql_num_rows($run);
                                if(
    $nums 0)
                                {
                                    echo 
    "<center><font color=red>Sub Category Already Exists.</font></center>";
                                }
                                else
                                {
                                    
    $query2 "insert into subcategories values('$category','$subcategory')";
                                    
    mysql_query($query2);
                                    echo 
    "<center><font color=green>Sub-Category Added.</font></center>";
                                }
                            }
                            else
                            {
                                echo 
    "<center><font color=red>Please fill Sub-Category field.</font></center>";
                            }
                        }
                    }
                }
            } 
    Last edited by NogDog; 01-18-2014 at 08:26 AM. Reason: added [php] tags

  9. #9
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    You can do whatever makes logical sense and works for you.

    Certainly in you example it could be cleaned up and simplified a bit to something like:
    PHP Code:
    if(empty($_POST['category']) or $_POST['category'] === 'NULL') {
        
    // error condition
    }
    else {
        
    // continue processing...

    PS: Please use [php] ... [/php] tags around your code in this forum.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  10. #10
    Join Date
    Nov 2013
    Location
    Chennai
    Posts
    17
    Something like this?

    php code:

    <select name="favFruit[]" size="4" multiple="">
    <?php
    $options = array("apple", "banana", "plum", "pomegranate", "strawberry", "watermelon");
    foreach ($options as $option) {
    echo '<option value="' . $option . '"';
    if (in_array($option, $favFruit)) {
    echo " selected";
    }
    echo ">" . ucfirst($option) . "</option>";
    }
    ?>
    </select>

  11. #11
    Join Date
    Jan 2014
    Posts
    2
    Hi Nogdog

    I am having serious problems sorting a table according to the users option. I already have a working php code to display the table, I just have problems with the code to display the table with options the user has selected.

    The user can select an option of either asc or desc order from a drop box and the column they want arranged in the specified way.

    I have been able to develop the following code and keep getting an error message.Below is the code I generated n the error:
    <?php
    if (isset($_POST['submitted']))

    $walkIn = $_POST['WalkIn'];

    $walkOut = $_POST['WalkOut'];

    $result = mysql_query("SELECT * FROM Results ORDER BY $walkIn $walkOut ")
    or die(mysql_error());


    ?>

    the html

    <tr>

    <td>
    Display table in =
    <select name="WalkIn">
    <option value="Please Choose">Please Choose</option>
    <option value="asc">Ascending order</option>
    <option value="desc">Descending order</option>
    </select>
    </td>

    <td>


    on =
    <select name="WalkOut">
    <option value="Please Choose">Please Choose</option>
    <option value="Goliath">Goliath</option>
    <option value="Samson">Samson</option>
    </select>
    </td>

    </tr>

    would appreciate a guide in the right direction

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles