1

我是 Ajax 和一般编程的新手,我目前正在学习中。

我的目标

我目前正在尝试创建一个链式下拉列表,其中第一个下拉列表从我的数据库中自行填充,第二个下拉列表根据用户在第一个下拉列表中的选择自行填充。

我的代码(表单页面)

<?PHP
  session_start();
  if(@$_SESSION['Auth']!=="Yes") {
    echo"You are not authorised to view this page.Please click <a href='Login.php'>here</a> to login.";
    exit();
  }
?>
<?PHP
  //Processing Code goes here
?>
<!DOCTYPE html>
<html>
  <head>
    <script>
    function getcategory() {
      var xmlhttp;
      if(window.XMLHttpRequest) {
        //code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp= new XMLHttpRequest();
      } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if(xmlhttp.readyState==4 && xmlhttp.status==200) {
          alert("This is an alert 1");
          document.getElementById("category").innerHTML=xmlhttp.responseText;
        }
      }
      alert("This is an alert 2");
      xmlhttp.open("GET","AddItemCat.php","true");
      alert("This is an alert 3");
      xmlhttp.send();
      alert("This is an alert 4");
    }
    function getsubcategory(cat) {
      var xmlhttp;
      if(window.XMLHttpRequest) {
        //code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp= new XMLHttpRequest();
      } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if(xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("subcat").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","AddItemSubCat.php?cat="+cat,"true");
      xmlhttp.send();
    }
  </script>
</head>
<body>
  <form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" name="additem" enctype="multipart/form-data" method="POST">
    <table>
    <tr>
      <td>Select Category: </td>
      <td><script>alert("This is an alert 5");</script>
        <select id="category" onclick="getcategory()">
          <script>alert("This is an alert 6");</script>
          <option id="cat"value=""></option>
          <script>alert("This is an alert 7");</script>
        </select>
      </td>
    </tr>
    <tr>
      <td>Select SubCategory</td>
      <td>
        <select id="subcat" onclick="getsubcategory(cat)">
          <option value=""></option>
        </select>
      </td>
    </tr>
  </table>
  </form>
</body>
</html>

我的代码(AddItemCat.php)

<?PHP
  session_start();
  if(@$_SESSION['Auth']!=="Yes") {
    echo"You are not authorised to view this page.Please click <a href='Login.php'>here</a> to login.";
    exit();
  }
?>
<?PHP
  if(@$_SESSION['Auth']=="Yes" && @$_SESSION['Type']=="Admin") {
    include("cxn.inc");
    $userid=$_SESSION['UserId']
    $branchid=0;
    $getcat="SELECT * FROM `Categories` WHERE `Business`=$userid AND Branch=$branchid";
    $runcat=mysqli_query($cxn,$getcat) or die (mysqli_error($cxn));
    while($row=mysqli_fetch_assoc($runcat)) {
      $cat=$row['Category'];
      echo"<option value=$cat>$cat</option>";
    }
  }
?>

我的代码(AddItemSubCat.php)

<?PHP
  session_start();
  if(@$_SESSION['Auth']!=="Yes") {
    echo"You are not authorised to view this page.Please click <a href='Login.php'>here</a> to login.";
    exit();
  }
?>

<?PHP
  if(@$_SESSION['Auth']=="Yes" && @$_SESSION['Type']=="Admin") {
    include("cxn.inc");
    //Gets the category parameter passed from the URL
    $cat=$_GET['cat'];
    $userid=$_SESSION['UserId']
    $branchid=0;
    $getsub="SELECT * FROM `SubCategories` WHERE `Business`=$userid AND Branch=$branchid && Category==$cat";
    $runsub=mysqli_query($cxn,$getsub) or die (mysqli_error($cxn));
    while($row=mysqli_fetch_assoc($runsub)) {
      $sub=$row['Category'];
      echo"<option value=$sub>$sub</option>";
    }
  }
?>

错误(或者说没有错误)

我的 Category 和 SubCategory 下拉列表都是空的,什么都没有显示,我看不出它到底是什么 WTF 是我做错了。谁能指出我的错误并告诉我如何纠正和改进?我一直在看屏幕 2 个小时而不是我没有更接近找出我的错误,因为我什么都没有得到(没有错误消息),只是一个带有 2 个下拉框的表格,完全是空的.

谢谢!

PS:我的最终目标是创建一个类似于在线商店中使用的表单,用户可以在其中上传商品以及随附的图片。我还没有添加表单的其余部分,因为我的保管箱无法正常工作,所以没有任何意义。

编辑:在表单页面中添加了一个以前缺少的表格。结果没有可见的变化,我仍然无法使代码正常工作。

编辑 2:在代码中添加了各种警报。只有警报 5,6 和 7 正在工作并在我加载页面时弹出。javascript 中的警报不起作用,所以问题显然出在 javascript 函数中,尽管我仍然看不到错误是什么。

4

1 回答 1

0

我会推荐 2 个更改 -

1.在页面加载时加载您的类别选择选项,而不是使用onclick.
-添加onload="getcategory()"到您的身体标签。

2.加载您的子类别选择更改类别的选项。
-添加onchange="getsubcategory(this)"到您的<select id="category">,并onclick="getsubcategory(cat)"从您的 - 然后在您中<select id="subcat" >
使用以获取选定的值。var catval = cat.options[cat.selectedIndex].value;getsubcategory()

现在看起来像 -

...
<!DOCTYPE html>
<html>
  <head>
    <script>
    function getcategory() {
      var xmlhttp;
      if(window.XMLHttpRequest) {
        //code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp= new XMLHttpRequest();
      } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if(xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("category").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","AddItemCat.php","true");
      xmlhttp.send();
    }
    function getsubcategory(cat) {
      var xmlhttp;
      var catval = cat.options[cat.selectedIndex].value;
      if(window.XMLHttpRequest) {
        //code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp= new XMLHttpRequest();
      } else {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function() {
        if(xmlhttp.readyState==4 && xmlhttp.status==200) {
          document.getElementById("subcat").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","AddItemSubCat.php?cat="+catval,"true");
      xmlhttp.send();
    }
  </script>
</head>
<body onload="getcategory()">
  <form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" name="additem" enctype="multipart/form-data" method="POST">
    <table>
    <tr>
      <td>Select Category: </td>
        <select id="category" onchange="getsubcategory(this)">
          <option value=""></option>
        </select>
      </td>
    </tr>
    <tr>
      <td>Select SubCategory</td>
      <td>
        <select id="subcat">
          <option value=""></option>
        </select>
      </td>
    </tr>
  </table>
  </form>
</body>
</html>
于 2013-07-21T05:49:14.100 回答