select box

Discussion in 'PHP' started by dean5000v, Jul 11, 2008.

  1. #1
    ok well ive done a script that basically echos all of the tables within my database into a combo box, the problem now is i want to validate it so if the user doesn't select a table then i want to echo that there wasn't no selection. i have made sure that i created a blank item within the select box aswel. here is my code.


    <?php
    
    $dbname = 'obso006_itemstock'; 
    $sql = "SHOW TABLES FROM $dbname";
    $result = mysql_query($sql);
    
    if (!$result) {
        echo "DB Error, could not list tables\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }
    echo "<form method=\"post\" action=\"" . $_SERVER['PHP_SELF'] ."\">"; 
    echo "<span class=\"style\">Please Select a Category:</span> &nbsp;&nbsp;&nbsp;<select name=\"dbtables\" id=\"dbtables\">\n";
    echo "<option value=\"\"></option>";
    while ($row = mysql_fetch_row($result)) {
    
    	echo "<option value=\"".$row[0]."\">".$row[0]."</option>\n";
    
    }
    
    echo "<input type=\"submit\" name=\"select_table\" value=\"Submit\"></select></form>";
    
    mysql_free_result($result);
    
    ?>
    Code (markup):

     
    dean5000v, Jul 11, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    $accept = array();
    while ($row = mysql_fetch_row($result)) {
    
    	echo "<option value=\"".$row[0]."\">".$row[0]."</option>\n";
    	$accept[$row[0]] = true;
    }
    
    if(!isset($accept[$_POST['dbtables']])) { ... } else { ... }
    PHP:
    Faster than in_array.
     
    Danltn, Jul 11, 2008 IP
  3. mlkshake

    mlkshake Peon

    Messages:
    73
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?php
    if(empty($_POST['dbtables']))
    {
    echo 'Nothing is selected';
    }
    ?>
    
    PHP:
     
    mlkshake, Jul 12, 2008 IP