我是初学程序员,我想计算一个表中的记录数。我已经看到了大量的代码提取,但我似乎无法拼凑它们将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对结果进行编码?
谢谢!
答案 0 :(得分:1)
好吧你有一些拼写错误&amp;代码中的一些错误:
<强>错字强>
您<php
=&gt;中的showscan.php
标记<?php
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();
}