0

原始问题:<select> 下拉菜单使用 JS 函数正确输出我的 MYSQL 数据

我试图写一篇后续文章,但将其添加为答案。这是我的问题:

使用与 MassiveAttack 相似/相同的脚本;我的问题是,一旦我选择第二个下拉菜单,这两个下拉菜单就会自行重置(URL 正确);我如何“保持”我的价值观?

这是我的脚本:

    function reload(form) {
        var val=form.cat.value;
        var val2=form.subcat.value;
        self.location='dd.php?cat=' + val + '&subcat=' + val2;
     }

     </script>
     </head>

     <body>
     <?

    @$cat = $_GET ['cat']; // Use this line or below line if register_global is off
    if (strlen ( $cat ) > 0 and ! is_numeric ( $cat )) { // to check if $cat is
                                                 // numeric data
                                                 // or not.
     echo "Cat Data Error";
     exit ();
     }

    $quer2 = mysql_query ( "SELECT DISTINCT name,category_id FROM shop_categories order by name" );

// ///// for second drop down list we will check if category is selected else we
// will display all the subcategory/////
    if (isset ( $cat ) and strlen ( $cat ) > 0) {
     $quer = mysql_query ( "SELECT name,product_id,priority,description FROM products WHERE category_id=$cat order by name" );
    } else {
    $quer = mysql_query ( "SELECT name,product_id,priority,description FROM products order by name" );
    }
// //////// end of query for second subcategory drop down list box
    echo "<form method=post name=f1 action='dd-check.php'>";
// / Add your form processing page address to action in above line. Example
// action=dd-check.php////
// //////// Starting of first drop downlist /////////
    echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select      Category</option>";
     while ( $noticia2 = mysql_fetch_array ( $quer2 ) ) {
         if ($noticia2 ['category_id'] == @$cat) {
             echo "<option selected value='$noticia2[name]'>$noticia2[name]</option>" . "<BR>";
          } else {
          echo "<option value='$noticia2[category_id]'>$noticia2[name]</option>";
          }
     }
          echo "</select>";
// //////////////// This will end the first drop down list ///////////

// //////// Starting of second drop downlist /////////
      $myList1 = array ();
      echo "<select name='subcat' onchange=\"reload(this.form)\"><option value=''>Select   subCategory</option>";
     while ( $noticia = mysql_fetch_array ( $quer ) ) {
          if ($_GET ['subcat'] == $noticia [subcat]) {        
          echo "<option value='$noticia[name]'>$noticia[name]</option>";
          }
     }
     echo "</select>";

     echo "<input type=submit value=Submit>";
     echo "</form>";
     ?>

    </body>

     </html>

    > 
4

1 回答 1

0

您忘记在第二个下拉列表中使用 selected 。

echo "<option selected value='$noticia[name]'>

当您更改第一个下拉菜单时它是否有效?

于 2012-05-20T19:00:26.227 回答