0

元の質問:<select>ドロップダウンは、JS関数を使用してMYSQLデータを適切に出力します

フォローアップを書こうとしましたが、代わりにこれを回答として追加しました。これが私の質問です:

MassiveAttackと同様の/同一のスクリプトを使用する。私の問題は、2番目のドロップダウンを選択すると、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>

    > 
4

1 に答える 1

0

2番目のドロップダウンで選択したものを使用するのを忘れました。

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

最初のドロップダウンを変更したときに機能しますか?

于 2012-05-20T19:00:26.227 に答える