使用jquery和sql的下拉列表

时间:2015-10-19 07:07:48

标签: javascript php mysql

这是一个显示类别列表的文件..在我从下拉列表中选择类别后,它会填充子类别列表。我正在使用javascript。第二个文件是getuser3.php,它在下面发布....代码工作正常。 现在这就是我想要的..在提交表单时,我希望选择的值在select标签中显示为选中.. 例如:如果我选择了类别为“ELECTRONICS”而子类别为“MOBILES”,那么在提交表格后,这两个值都应显示为选中。

文件名:categories.php

<script>
function showUser(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","getuser3.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>
<?php


$users="";
if(isset($_POST['users']))
{
    $users=$_POST['users'];
}
$item24="";
if(isset($_POST['item24']))
{
    $item24=$_POST['item24'];
}



?>
<form method="post" action="categories_sample3.php">
<?php
$x="";
$sql2 = "SELECT DISTINCT categories FROM categories_offline ORDER BY categories";
$result2 = mysql_query($sql2);


echo "<select name='users' onchange='showUser(this.value)'>";

echo "<option value='" . "Select Category" ."'".">"."Select Category"."</option>";
while ($row2 = mysql_fetch_array($result2)) 

{       
if($row2['categories'] == $users)

{
    $x="selected";

}
else

    {
        $x="";

    }



    echo "<option value='" . $row2['categories'] ."'".$x.">" . $row2['categories'] ."</option>";
    echo $x;
}
echo "</select>";
echo "<br><br>";

echo "<div id='txtHint' name='sunny'> </div>";

?>

<input type="submit" name="submit" value="submit">
</form>

文件名:getuser3.php

<?php

$q = $_GET['q'];
include("db_connect.inc");
session_start();


$sql="SELECT sub_category FROM categories_offline WHERE categories = '$q' ORDER BY sub_category";
$result = mysql_query($sql);
$x="";
echo "<select name='item24'>";

while($row = mysql_fetch_array($result)) 
{



echo "<option value='" . $row['sub_category'] ."'".$x.">" . $row['sub_category'] ."</option>";  

}
echo "</select>";

?>

1 个答案:

答案 0 :(得分:0)

form

中移动此行
<div id="txtHint"><b>Person info will be listed here.</b></div>

由于您的子类别select位于表单之外,因此您无法发布该值。