PHP generated menu question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Philth
    New Member
    • Oct 2007
    • 38

    PHP generated menu question

    Hi there,

    I've got a a form that includes several drop downs which are generated from rows within a database, which works fine thanks to the very helpful folks on this forum.

    Some of these drops downs are not compulsory and I would like them to be left blank if not needed - or with a default "please select...".

    Is this possible with the following code

    Code:
    		<?
    include("connect.php"); 
    $query="SELECT categoryName FROM category";
    
    $result = mysql_query ($query);
    echo "<select name='eventCategory2'></option>";
    
    while($nt=mysql_fetch_array($result)){
    echo "<option value='$nt[categoryName]'>'$nt[categoryName]'</option>";
    
    }
    echo "</select>";
    ?>

    Secondly, if that person doesn't select anything I don't want anything added to the database. I suspect that even if the user doesn't select an option, then a blank row, or a row containing the phrase "please select" will be entered. Is there a way around this?

    Many thanks. I hope it makes sense.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you can add a default option right after the <select> (once you correct the typo there) but outside the loop.

    Originally posted by Philth
    Secondly, if that person doesn't select anything I don't want anything added to the database. I suspect that even if the user doesn't select an option, then a blank row, or a row containing the phrase "please select" will be entered. Is there a way around this?
    you can check against the default value* (0 is quite a good choice), if that is the case, abort the insert.

    * if you use Filter functions, you can make those default values return false/NULL if you provide the ‘wrong’ input type

    Comment

    • Philth
      New Member
      • Oct 2007
      • 38

      #3
      Hi Dormilich,

      So putting something like this when processing the form would create a null entry when "0" is returned? Or is there more to it?

      Code:
      filter_var('eventCategory2', FILTER_VALIDATE_BOOLEAN));

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by Philth
        So putting something like this when processing the form would create a null entry when "0" is returned?
        no, it would return false (or NULL if you set the FILTER_NULL_ON_ FAILURE flag)

        Originally posted by Philth
        Or is there more to it?
        sure, there are the boolean-like values 0, 1, on, off, true, false, yes & no. see also here

        Comment

        Working...