weixin_33713707 2014-01-16 20:14 采纳率: 0%
浏览 21

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.

  • 写回答

2条回答 默认 最新

  • weixin_33686714 2014-01-16 20:15
    关注

    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'];
    
    
        ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致