使用JS下拉输出

时间:2012-05-20 18:53:17

标签: php javascript

原始问题:<select> dropdown output my MYSQL data properly using JS function

我试着写一篇后续内容,但却将其添加为答案。这是我的问题:

使用与MassiveAttack类似/相同的脚本;我的问题是,一旦我选择第二个下拉列表,2个下拉菜单自行重置(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>

    > 

1 个答案:

答案 0 :(得分:0)

您忘记在第二个下拉列表中使用选定内容。

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

更改第一个下拉列表时是否有效?