weixin_33704591 2017-10-18 13:29 采纳率: 0%
浏览 40

AJAX逻辑不起作用

I am new to AJAX and learning it. I am searching a food item in my HTML textbox and trying to communicate with the server to know if the item is available. The respective status of the item should be shown in the div tag below the textbox but it is not showing.

I haven't studied jQuery yet and would like to know the below things:

  1. How to get the response from the server in plaintext using AJAX and JavaScript, and display it in the div tag below the textbox (advise the changes to be made in the code).

  2. What change should I make in JavaScript code to send the AJAX request in POST method (I know about the changes in PHP code)?

//index.html

<head>
  <script type="text/javascript" src="food.js">
  </script>
</head>

<body>
  <h3>The Cheff's Place</h3>
  Enter the food you want to order

  <input type="text" id="userInput" name="input" onkeypress="sendInfo()"></input>

  <div id="underInput"></div>

</body>

</html>

//food.js

var request;

function sendInfo() {
  var v = document.getElementById("userInput").value;
  var url = "index.php?food=" + v;

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

  if (request.readyState == 0 || request.readyState == 4) {
    try {
      request.onreadystatechange = getInfo;
      request.open("GET", url, true);
      request.send(null);
    } catch (e) {
      alert("Unable to connect to server");
    }
  }
}

function getInfo() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      var val = request.responseText;
      document.getElementById('underInput').innerHTML = val;
    }
  }
}

//index.php

<?php
  header('Content-Type: text/plain');
  $food = $_GET['food'];
  $foodArray = array("paneer", "butter", "chicken", "tandoori", "dal");
  if (in_array($food, $foodArray))
  {
    echo "We do have " .$food;
  }

  elseif($food == "")
  {
    echo "Kindly enter some food";
  }

  else
  {
    echo "We do not sell " .$food;
  }
?>
  • 写回答

1条回答 默认 最新

  • weixin_33699914 2017-10-18 15:04
    关注

    I ran your code. It's working fine. Just replace onkeypress with onkeyup.

    <input type="text" id="userInput" name="input" onkeyup="sendInfo()"></input>
    

    Using JQuery (Assuming you have included jquery file or cdn) :

    Include the following snippet in script tag at the end of the body.

        $("#userInput").keyup(function(){
            $.get("index.php", { food: $("#userInput").val() })
                .done(function(data) {
                    $("#underInput").html(data)
                })
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改