如何使用AJAX(不带jQuery)和PHP计算记录数


How to count number of records using AJAX(without jQuery) and PHP

我是一名初级程序员,我想计算表中的记录数。我已经看到了很多代码的摘录,但我似乎无法将它们拼凑在一起,将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>

函数运行后,'document.getElementById("noscan").innerHTML'未收到警报,并且包含'document.getElementById("noscan").inerHTML'的字段在html文件中也为空。在我删除所有json_encoding之后,这些代码是我编程笔记的一部分,因为我不确定它能做什么,也不知道如何更好地遵循示例。我确实安装了jQuery,但jQueryAjax对我来说有点太难了,我可以更好地可视化在没有jQuery的情况下进行Ajax调用。

问题:如何将count(*)值从PHP转移到我的javascript代码中?在什么情况下,在发送结果之前,我必须用JSON对结果进行编码?

谢谢!

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

典型showscan.php=><?php 中的<php标签

javascript函数showscan()document.getElementById("noscan").innerHTML = xmlHttp.responseText;行中的xmlHttp应为xmlhttp。(小h),因为JavaScript是一种区分大小写的语言。

错误 javascript showscan()document.getElementById("noscan").innerHTML = xmlHttp.responseText;中的noscantext input,并且输入文本不具有属性innerHTML

showscan.php

echo mysql_num_rows($result);中的mysql_num_rows;应该是mysqli_num_rows;注意到区别了吗?CCD_ 15&mysqli

建议

var url = serverURL() + "/showscan.php";中的serverURL()如果你在同一个文件夹中有这个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(); 
}