javascript - php中获取dropdownlist的值

标签 javascript php jquery html

我有以下 html/php 代码:

<!DOCTYPE html>
<html>
<head>
    <title>Voiture</title>
</head>

<body>
    Welcome<br>
    <form method="post" action="">
        Liste de voiture<select name="selected" id="selected">
            <?php
            $sql = 'select Type from voiture';
            $result = $conn->query($sql);
            $json = array();
            while ($row = $result->fetch_assoc()) {
                if(!in_array($row['Type'], $json)){
                    $json[] = $row['Type'];
                    echo '<option name = "'.$row['Type'].'">'.$row['Type'].'</option>';
                }
            }
               ?>

        </select> <br>

        <span id="sel" name="sel"></span>
        <table border="1">
            <tr id="header">
                <td>Type</td>
                <td>Model</td>
                <td>Couleur</td>
                <td>Prix</td>
                <td>User</td>
                <td>Action</td>
            </tr>
        </table>
        <input type="submit" name="submit" hidden>
    </form>
    <script src="jquery-3.2.1.js"></script>
    <script>
        $(function(){
            $('#selected').on('change',function(){
                $('#sel').text(document.getElementById('selected').value);
                $.getJSON('phpVoiture.php',function(data){
                    for (var x = 0 ; x < data.length ; x++){
                        $('<tr><td>'+data[x]['type']+'</td>'+'<td>'+data[x]['Model']+
                                '</td><td>'+data[x]['Couleur']+'</td>'+
                                '<td>'+data[x]['Prix']+'</td>'+'<td></td></tr>').insertAfter($('#header'));
                    }
                });
            });
        });
    </script>
</body>
</html>

以及以下 php 页面:

<?php

require_once ('./dbConnect.php');
include ('./Voiture.php');

$sel = $_POST['selected'];
$conn = mysqli_connect(servername, username, password, db ,port);
$query = "select * from voiture where Type = '".sel."'";
$result = mysqli_query($conn, $query);
$json = array();
if(mysqli_num_rows($result)>0){
    while($row = mysqli_fetch_assoc($result)){
        $json[] = [
            'type' => $row['Type'],
            'model' => $row['Model'],
            'couleur' => $row['Couleur'],
            'prix' => $row['Prix']
        ];
    }
}
else{
    echo mysqli_num_rows($result);
}
echo json_encode($json);

问题是,当我在下拉列表中选择一个选项时,什么也没有发生。我希望第二个 php 页面中的查询能够选择具有我在下拉列表中选择的类型的汽车。我尝试通过在具有所选选项值的两个页面中回显警报来进行故障排除,但此步骤也失败了,因此我认为检索所选选项的值存在问题。任何帮助将不胜感激。

最佳答案

您没有将所选值发送到服务器。将其添加到 AJAX 调用中:

$.getJSON('phpVoiture.php', { selected: $('#selected').val() }, function(data){
    //...
});

还有,你的<option>元素没有值。您使用了name相反,但这属于 <select> 。使用value :

echo '<option value="'.$row['Type'].'">'.$row['Type'].'</option>';

此外,您使用的是 GET 请求而不是 POST 请求。所以你需要寻找 $_GET 中的值数组:

$sel = $_GET['selected'];

您还有其他拼写错误,例如在 PHP 中错误使用变量:

"...".sel."..."

将是:

"...".$sel."..."

尽管这提出了一个关于SQL injection的观点。你真的不应该像这样直接连接变量。相反,use prepared statements with query parameters .

完全有可能代码中仍然存在我尚未发现的其他错误。您将希望调试包括两件事:

  1. 查看 PHP 日志是否有错误。
  2. 使用浏览器的调试工具观察 AJAX 请求/响应。

关于javascript - php中获取dropdownlist的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47672061/

相关文章:

javascript - AngularJS $http POST 转为 GET

javascript - 如何让 Figma API 与 Google App-script API 一起使用?

php - 如何在 Laravel 5.6 中将第三方 API 数据转换为集合资源?

php - 去除尾随非单词字符

javascript - 如何使用php查找手机号码的重复条目

javascript - 当所有图像正在加载时全局触发 'loading gif'(内联显示)

Javascript 文档.domain

jquery - 将 jquery 中的字符串传递给 asp.net mvc ActionResult

演示中的 jQuery 和 CSS float 问题

Jquery:检查字段集是否为空