我正在尝试使用AJAX从数据库检索数据,但没有成功。这些是我正在使用的代码。我在控制台中看不到任何特定的错误。
HTML:
<button type="button" name="result_submit" id="result_submit" >Submit</button>
<div class="result" id="result" name="result"> </div>
jQuery:
$(document).ready(function(e) {
$('#result_submit').click(function() {
$.ajax({
url :"Income.php",
type :'POST',
success: function(data){
$("#result").html(data);
}
});
});
});
Income.php内容:
<?php
include_once 'dbConnection.php';
$stmt = mysqli_stmt_init($conn);
$income = "select SUM(amount) as incomeNumber FROM wp_formdata WHERE entry_type='Income'";
if(!mysqli_stmt_prepare($stmt,$income))
{
$message = '<h1 style="color:red;padding-top:5%;">SQL Error !!</h1>';
}
else
{
mysqli_stmt_execute($stmt);
$result= mysqli_stmt_get_result($stmt);
$income_sum=mysqli_fetch_assoc($result);
$TotIncome= "Total Income is ".$income_sum['incomeNumber'];
}
?>
dbConnection.php具有连接详细信息:
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "wordpress";
$conn= mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
有人可以指导我如何解决问题
答案 0 :(得分:3)
您需要echo
来自PHP文件的数据:
$income = "select SUM(amount) as incomeNumber FROM wp_formdata WHERE entry_type='Income'";
$response = '';
if (! mysqli_stmt_prepare($stmt,$income)) {
$response = '<h1 style="color:red;padding-top:5%;">SQL Error !!</h1>';
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$income_sum = mysqli_fetch_assoc($result);
$response = "Total Income is ".$income_sum['incomeNumber'];
}
echo $response;
答案 1 :(得分:0)
首先将所有内容添加到页面的开头:
error_reporting(E_ERROR | E_WARNING | E_PARSE); ini_set('error_reporting',E_ALL);
和 printf(“ Errormessage:%s \ n”,mysqli_error($ income_sum));
进一步查询数据库。 您对ajax调用的页面有任何回应吗?
答案 2 :(得分:-1)
您使用AJAX调用的脚本不会呈现任何内容。
npm install tslint --save
npm install codelyzer --save