duanjiu1003 2017-02-20 14:28
浏览 24
已采纳

来自select表单的Ajax请求

I'm trying to pull a value from my database when a select value is selected. This select is also populated from the same database.

To be clear, I'm new to AJAX requests and don't really understand what I'm doing wrong. All I get is blank when a value should appear.

This is my html form:

function showStock(str) {
    if (str == "") {
        document.getElementById("stockValue").innerHTML = "";
        return;
    } else {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("stockValue").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET", "getstock.php?q=" + str, true);
        xmlhttp.send();
    }
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>

    <?php $link=mysqli_connect( "localhost", "root", "", "cocktails"); // Check connection if($link===f alse){ die( "ERROR: Could not connect. " . mysqli_connect_error()); } ?>

    <script src='menu.js'></script>

    <form action="addstock.php" method="post">
        Product:
        <br>
        <select name="products" onChange="showStock(this.value)">
            <option value="">Select a product:</option>
            <?php $sql=m ysqli_query($link, "SELECT name FROM inventory ORDER BY name"); while ($row=$ sql->fetch_assoc()){ echo "
            <option value=\ "".$row[ 'name']. "\">" . $row['name'] . "</option>"; } ?>
        </select>
        <br> Stock Value:
        <div id="stockValue">
            <b>0</b>
        </div>

    </form>
</body>

</html>

This is my php file:

<!DOCTYPE html>
<html>

<head> </head>

<body>
    <?php
        $q = intval($_GET['q']);

        $con = mysqli_connect('localhost','root','','cocktails');
        if (!$con) {
            die('Could not connect: ' . mysqli_error($con));
        }

        $sql="SELECT * FROM inventory WHERE name = '".$q."'";
        $result = mysqli_query($con,$sql);
        $row = mysqli_fetch_array($result);

        echo $row['stock'];
        mysqli_close($con);
        ?>
</body>

</html>
</div>
  • 写回答

3条回答 默认 最新

  • dquh37673 2017-02-20 15:46
    关注

    Well It wasn't that evident but your problem is :

    $q = intval($_GET['q']);
    

    your $_GET['q'] is a string and you are converting it to an intval so it becomes a 0.

    And your SQL query is getting nothing because of that.

    Change this line to $q = $_GET['q']; and it will work :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?
  • ¥15 关于#matlab#的问题:训练序列与输入层维度不一样
  • ¥15 关于Ubuntu20.04.3LTS遇到的问题:在安装完CUDA驱动后,电脑会进入卡死的情况,但可以通过键盘按键进入安全重启,但重启完又会进入该情况!
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥15 C++数组中找第二小的数字程序纠错