【问题标题】:Getting the value of dropdownlist in php在php中获取下拉列表的值
【发布时间】:2018-05-20 04:51:59
【问题描述】:

我有以下 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 页面中的查询选择具有我在下拉列表中选择的类型的汽车。我尝试通过在具有所选选项值的两个页面中回显警报来进行故障排除,但是此步骤也失败了,因此我认为检索所选选项的值存在问题。任何帮助将不胜感激。

【问题讨论】:

  • 请小心...Look at this first,然后添加$ ,这样你的行看起来像:$query = "select * from voiture where Type = '".$sel."'";

标签: javascript php jquery html


【解决方案1】:

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

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

另外,您的&lt;option&gt; 元素没有 值。您改用了name,但这属于&lt;select&gt;。使用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 请求/响应。

【讨论】:

  • 他的选项使用name而不是value,这也是不正确的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 2015-03-02
  • 1970-01-01
相关资源
最近更新 更多