如何使用AJAX(不使用jQuery)和PHP

时间:2016-08-01 08:09:30

标签: javascript php html ajax

我是初学程序员,我想计算一个表中的记录数。我已经看到了大量的代码提取,但我似乎无法拼凑它们将PHP结果传递给我的javascript代码。以下是我最终得到的代码:

showscan.php

<php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
    $conn = new mysqli("localhost", "root", "root", "bencoolen");

    $query = "SELECT COUNT(*) FROM discountcode";
    $result = $conn->query($query);
    echo mysql_num_rows($result);

    $conn->close();
}
catch(Exception $e) {
    echo "error";

}
?>

的index.html

<head>
<!-- The policy below is cancelled out so the project can run on my android device's version -->    
<!--<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">-->
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" href="js/jquery.mobile-1.4.5.min.css">
<link rel="stylesheet" href="css/jquery.mobile.datepicker.css" />
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/jquery.mobile-1.4.5.min.js"></script>
<script src="js/jquery.ui.datepicker.js"></script>
<script id="mobile-datepicker" src="js/jquery.mobile.datepicker.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.4.1.js"></script> 
<script type="text/javascript" src="barcodescanner.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script src="common.js"></script>

<script type="text/javascript">
    function showscan(){
        var xmlhttp = new XMLHttpRequest();
        var url = serverURL() + "/showscan.php";
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                //The alert below was alerted                           
                alert("readystate and status OK");
                document.getElementById("noscan").innerHTML = xmlHttp.responseText;

                //The alert below was not alerted
                alert(document.getElementById("noscan").innerHTML);             
            }
        };
        xmlhttp.open("GET", url, true);
        xmlhttp.send(); 
    }
</script>
</head>

<body>
    <div data-role="page" id="pageone">
        <div data-role="content" class="ui-content" id="loginsection">
            <form name="LoginForm">
                <div class="ui-grid-a">
                    <div class="ui-block-b">
                        <input id="noscan" type="text" class="input-text" placeholder="Number of scans"/>
                    </div>  
                </div>
            </form>
        </div>
     </div>
</body>

&#39; document.getElementById(&#34; noscan&#34;)。innerHTML&#39; 未收到提醒,而且包含&#39; document.getElementById的字段(&#34; noscan&#34;)。innerHTML&#39; 下面说明html文件在函数运行后也是空白的。在删除所有json_encoding之后,这些代码是我的编程注释的一部分,因为我不确定它的作用并更好地遵循示例。我确实安装了jQuery,但jQuery Ajax对我来说有点太过分了,我可以直观地在没有jQuery的情况下进行Ajax调用。

问题:如何制作以便PHP的count(*)值转换为我的javascript代码? 在发送它们之前,我将在什么情况下使用JSON对结果进行编码?

谢谢!

1 个答案:

答案 0 :(得分:1)

好吧你有一些拼写错误&amp;代码中的一些错误:

<强>错字<php =&gt;中的showscan.php标记<?php

javascript函数xmlHttp的行document.getElementById("noscan").innerHTML = xmlHttp.responseText;中的

showscan()应为xmlhttp。 (小h),因为 JavaScript 区分大小写的语言。

<强>错误 javascript showscan() noscan中的document.getElementById("noscan").innerHTML = xmlHttp.responseText;text input,输入文字没有属性innerHTML

<强> showscan.php

mysql_num_rows;中的{p> echo mysql_num_rows($result);应该mysqli_num_rows;注意到区别吗? mysql&amp; mysqli

<强>宝贵意见

serverURL()中的{p> var url = serverURL() + "/showscan.php";如果您在同一文件夹中有此showscan.php页面,则只需撰写showscan.php

根据您的HTML代码的外观,我看不到任何调用您的函数showscan()的内容。因此,您可以在body加载<body onload="showscan();">上调用它,也可以插入<button>并使用onclick属性<button type="button" onclick="showscan();">submit</button>

进行调用

现在尝试这个代码

<强> showscan.php

<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    error_reporting(E_ERROR);
    try{
        $conn = new mysqli("localhost", "root", "root", "bencoolen");
        $query = "SELECT * FROM discountcode";
        $result = $conn->query($query);
        echo mysqli_num_rows($result);
        $conn->close();
    }
    catch(Exception $e) {
        echo "error";
    }
?>

Javascript函数showscan()

function showscan(){
    var xmlhttp = new XMLHttpRequest();
    var url = "showscan.php";
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            //The alert below was alerted                           
            alert("readystate and status OK");
            document.getElementById("noscan").value = xmlhttp.responseText;
            //The alert below was not alerted
            alert(document.getElementById("noscan").value);
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send(); 
}