使用AJAX从PHP processor.php

时间:2016-01-01 23:21:07

标签: php ajax post

它被问到了一百万种不同的方式,但这是特定于此代码的。这不是一个重复的帖子。

的index.html

<html>
<head>
<script>
function ajax_post(){
    // Create our XMLHttpRequest object
    var hr = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var url = "process.php";
    var fn = document.getElementById("s").value;
    var vars = "s="+fn;
    hr.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    hr.onreadystatechange = function() {
        if(hr.readyState == 4 && hr.status == 200) {
            var return_data = hr.responseText;
            document.getElementById("status").innerHTML = return_data;
        }
    }
    // Send the data to PHP now... and wait for response to update the status div
    hr.send(vars); // Actually execute the request
    document.getElementById("status").innerHTML = "processing...";
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>
Domain: <input id="s" name="s" type="text">  <br><br>
<input name="myBtn" type="submit" value="Submit Data" onclick="ajax_post();"> <br><br>
<div id="status"></div>
</body>
</html>

process.php

<?php
if($_GET["s"])
{
echo '<table>';
  echo '<thead>';
    echo '<tr>';
      echo '<th width="200">CLASS</th>';
      echo '<th width="150">HOST</th>';
      echo '<th width="150">TYPE</th>';
      echo '<th width="150">TTL</th>';
      echo '<th width="150">TARGET</th>';
      echo '</tr>';
      echo '</thead>';
  echo '<tbody>';

$result = dns_get_record($_GET['s'], DNS_ALL, $authns, $addtl);
foreach($result as $key=>$value){
    echo '<tr>';
    echo '<td>' . $value['class'] . '</td>';
    echo '<td>' . $value['host'] . '</td>';
    echo '<td>' . $value['type'] . '</td>';
    echo '<td>' . $value['ttl'] . '</td>';
    echo '<td>' . $value['target'] . $value['ip'] . '</td>';
    echo '</tr>';

}

  echo '</tbody>';
  echo '</table>';
}
?>

问题

我无法获得process.php来返回回声?如果您直接访问process.php?s = google.com,那么它可以正常工作,并且可以毫不费力地回应响应。数字它可能是一些小但我无法想象我们可以有人请指出我的错误吗?谢谢!

样本

表格: http://main.xfiddle.com/14eb8a38/ajax/index.html

证明: http://main.xfiddle.com/14eb8a38/ajax/process.php?s=google.com

0 个答案:

没有答案