Ajax调用返回整个页面,而不仅仅是回声值 [英] Ajax call returns entire page rather than just the echo value
问题描述
我想提出一个AJAX调用一个PHP页面,我想提出一个数据库查询。然而,当我回声从PHP页面的值,则返回整个HTML文件,而不仅仅是数值我所需要的。
I am making an ajax call to a .php page where I am making a query to the database. However, when I echo a value from the .php page, the entire html file is returned rather than just the numerical value I need.
这是我的AJAX脚本:
This is my ajax script:
<script type="text/javascript">
$(document).ready(function()
{
$("#valuebutton").click(function()
{
var id1=$('.player1').val();
$.ajax
({
type: "POST",
url: "updatevaluebox.php",
data: ({g1: id1}),
cache: false,
success: function(value)
{
//alert(value);
$('#valuebox').val(value);
}
});
});
});
</script>
这是PHP页面updatevaluebox.php:
And this is the php page updatevaluebox.php:
<?php
require("connect_db.php");
$q="SELECT price FROM playerlist where id=".$_POST['g1'];
$r=mysqli_query($dbc,$q);
$price=mysqli_fetch_array($r,MYSQLI_NUM);
mysqli_close($dbc);
echo $price[0];
?>
这两个文件都在同一个目录。
Both files are in the same directory.
我已经检查了其他的答案就计算器这个问题,但没有一个似乎工作。
I have checked other answers to this question on stackoverflow but none seem to work.
我从警报语句得到的输出如下所示:
The output I am getting from the alert statement looks like:
<html>
<head><title>
</title></head>
<body>
</body>
</html>5.5
5.5时,到底是我唯一需要的价值!
The 5.5 at the end is the only value I need!
我已经设置了ajax数据类型设置为文本,但即使这样也无济于事。
I have set the ajax dataType to text but even that doesn't help.
推荐答案
是的!您需要删除任何HTML在connect_db.php文件。我建议存储变量在该文件中检查,如果你的连接成功。例如:
Yep! You need to remove any HTML in the connect_db.php file. I recommend storing a variable in that file checking if your connection was successful. For example:
connect_db.php
$isConnected = false;
$dbError = "";
if ($dbc=mysqli_connect('localhost','*****','*****','ff') {
$isConnected = true;
} else {
$dbError = mysqli_connect_error();
}
等文件
require("connect_db.php");
if($isConnected) {
//DO ALL YO STUFF!!
} else {
//HANDLE THE ERROR AS YOU LIKE, LIKE PRINTING IT OUT
echo $dbError;
}
这样做可以让你如果无法连接,所以你可以得体的处理失败的连接处理数据库逻辑文件。
Doing this will allow you to handle the DB logic in your files if it fails to connect so you may handle failed connections gracefully.
这篇关于Ajax调用返回整个页面,而不仅仅是回声值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!