PHP页面没有返回值

时间:2014-01-16 20:14:12

标签: php jquery ajax

我试图通过ajax和php从数据库中检索一个值。 ajax代码如下:

<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页面如下:

<?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);
?>

有人可以告诉我哪里出错了。

2 个答案:

答案 0 :(得分:0)

你应该使用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>

PHP

<?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);

        echo $row['stock_price'];


    ?>

答案 1 :(得分:0)

php脚本需要回显值。这不显示页面上的值,它只是使值可以使用javascript。

我建议使用jquery并使用内置的ajax功能。这更容易。

请参阅jquery ajax page,并直接从中获取示例:

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
    alert( "Data Saved: " + msg );
});