PHP页面未返回值
我试图通过ajax和php从数据库中检索一个值。 ajax代码如下所示:PHP页面未返回值,php,jquery,ajax,Php,Jquery,Ajax,我试图通过ajax和php从数据库中检索一个值。 ajax代码如下所示: <script> $(document).ready(function() { $("#buyprice").change(function() { if ($("#sname").val() == "") { alert("Enter Stock name.");
<script>
$(document).ready(function() {
$("#buyprice").change(function() {
if ($("#sname").val() == "") {
alert("Enter Stock name.");
} else {
var sn = $("#sname").val();
alert(sn);
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var x = xmlhttp.responseText;
};
};
xmlhttp.open("GET", "getstockprice.php?q="+sn, true);
xmlhttp.send();
alert("here");
};
alert("here");
var bp = $("#buyprice").val();
alert(bp);
alert(x.val());
if(bp>(1.1*x)||bp<(1.1*x)){
alert("Price violating 10% constraint.");
}
alert("here");
});
});
</script>
<?php
$q = $_GET['q'];
$con = mysqli_connect('localhost','root','','stock_market');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT stock_price FROM live_prices WHERE stock_name = '".$q."'";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
mysqli_close($con);
?>
$(文档).ready(函数(){
$(“#购买价格”).change(函数(){
if($(“#sname”).val()=“”){
警告(“输入股票名称”);
}否则{
var sn=$(“#sname”).val();
警报(sn);
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var x=xmlhttp.responseText;
};
};
open(“GET”、“getstockprice.php?q=“+sn,true”);
xmlhttp.send();
警报(“此处”);
};
警报(“此处”);
var bp=$(“#买入价”).val();
警报(bp);
警报(x.val());
如果(bp>(1.1*x)| | bp,则应使用echo或return从php返回内容
<script>
$(document).ready(function() {
$("#buyprice").change(function() {
if ($("#sname").val() == "") {
alert("Enter Stock name.");
} else {
var sn = $("#sname").val();
alert(sn);
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var x = xmlhttp.responseText;
};
};
xmlhttp.open("GET", "getstockprice.php?q="+sn, true);
xmlhttp.send();
alert("here");
};
alert("here");
var bp = $("#buyprice").val();
alert(bp);
alert(x);
if(bp>(1.1*x)||bp<(1.1*x)){
alert("Price violating 10% constraint.");
}
alert("here");
});
});
</script>
$(文档).ready(函数(){
$(“#购买价格”).change(函数(){
if($(“#sname”).val()=“”){
警告(“输入股票名称”);
}否则{
var sn=$(“#sname”).val();
警报(sn);
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
var x=xmlhttp.responseText;
};
};
open(“GET”、“getstockprice.php?q=“+sn,true”);
xmlhttp.send();
警报(“此处”);
};
警报(“此处”);
var bp=$(“#买入价”).val();
警报(bp);
警报(x);
如果(bp>(1.1*x)| | bpphp脚本需要回显该值。这不会在页面上显示该值,它只会使该值对javascript可用
我建议使用jquery并使用内置的ajax功能,这样做容易得多
请参阅,并从中直接举出一个示例:
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
没有通过echo
、print
或PHP中的任何内容输出此PHP代码的输出是什么,为什么?我看不到任何实际输出的迹象。我不想显示结果,我只想让值进行一些处理。在这里,echo不会在浏览器中显示任何内容,因为您正在调用使用ajax的php文件。echo在这里充当返回语句。@Jonah:您在哪里进行“处理”?我看不到您在PHP或JavaScript中使用任何值……它是ajax部分的x值。因此,基本上输入值不应小于或大于数据库中的值10%,这是我想要实现的。并且数据库中的值不断变化。@Jonah:您为此做了什么调试?使用类似F的东西ireBug或Chrome工具。当您实际回显PHP中的值时,服务器的实际响应是什么?.responseText
包含什么?太好了。祝您好运。
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});