PHP页面未返回值

I am trying to retrieve a value from a database through ajax and php. The ajax code is as follows:

<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>

The php page is as follows:

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

Can someone please tell me where I am going wrong.

you should use echo or return to return something from 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'];


    ?>

The php script needs to echo the value. This does not display the value on your page, it merely makes the value avalailble to the javascript.

I would suggest using jquery and use the built in ajax functionality. This works much easier.

See the jquery ajax page, and an example straight from there:

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