簡體   English   中英

為什么 Select 框不會填充? 菜鳥 AJAX 問題

[英]Why Won't Select Box Populate? Noob AJAX Question

請幫助我讓我的 AJAX 演示正常工作。

這是前端代碼:

<html>
  <head>

    <title>Using Ajax and XML With Post</title>

    <script language = "javascript">

      var XMLHttpRequestObject = false; 

      if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
      } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
      }

      function getSandwiches1()
      {
        if(XMLHttpRequestObject) {
          XMLHttpRequestObject.open("POST", "sandwiches3.php", 
            true); 
          XMLHttpRequestObject.setRequestHeader('Content-Type', 
            'application/x-www-form-urlencoded'); 

          XMLHttpRequestObject.onreadystatechange = function() 
          { 
            if (XMLHttpRequestObject.readyState == 4 && 
              XMLHttpRequestObject.status == 200) { 
            var xmlDocument = XMLHttpRequestObject.responseXML;

            var sandwiches 
              = xmlDocument.getElementsByTagName("sandwich");
            listSandwiches(sandwiches);
            } 
          } 

          XMLHttpRequestObject.send("type=1"); 
        }
      }

      function getSandwiches2()
      {
        if(XMLHttpRequestObject) {
          XMLHttpRequestObject.open("POST", "sandwiches3.php", 
            true); 
          XMLHttpRequestObject.setRequestHeader('Content-Type', 
            'application/x-www-form-urlencoded'); 

          XMLHttpRequestObject.onreadystatechange = function() 
          { 
            if (XMLHttpRequestObject.readyState == 4 && 
              XMLHttpRequestObject.status == 200) { 
            var xmlDocument = XMLHttpRequestObject.responseXML;

            var sandwiches 
              = xmlDocument.getElementsByTagName("sandwich");
            listSandwiches(sandwiches);
            } 
          } 

          XMLHttpRequestObject.send("type=2"); 
        }
      }

      function listSandwiches (sandwiches)
      {
        var loopIndex;
        var selectControl = document.getElementById('sandwichList');

        for (loopIndex = 0; loopIndex < sandwiches.length; loopIndex++)
        {
            selectControl.options[loopIndex] = new 
               Option(sandwiches[loopIndex].firstChild.data);
        }
      }
    
    </script>
  </head>

  <body>

    <h1>Using Ajax and XML With Post</h1>

    <form>
      <input type = "button" value = "Get sandwiches" 
        onclick = "getSandwiches1()"> 
      <input type = "button" value = "Get vegetarian sandwiches" 
        onclick = "getSandwiches2()"> 
      <select size="1" id="sandwichList">
        <option>Select a sandwich</option>
      </select>
    </form>

  </body>

</html>

這是后端 PHP 代碼:

<?
header("Content-type: text/xml");
if ($_POST["type"] == "1")
  $sandwiches = array('ham', 'turkey', 'cheese');
if ($_POST["type"] == "2")
  $sandwiches = array('cheese', 'avocado', 'spinach');
echo '<?xml version="1.0"?>';
echo '<sandwiches>';
foreach ($sandwiches as $value)
{
  echo '<sandwich>';
  echo $value;
  echo '</sandwich>';
}
echo '</sandwiches>';
?>

下拉框沒有像預期的那樣填充三明治。 兩個按鈕都不做任何事情。

在后端代碼中,當您使用 <?php 而不是 <? 這個對我有用...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM