如何在参数列表后修复Uncaught SyntaxError:missing)

时间:2017-08-28 12:21:54

标签: javascript php

else if($('#operator').val() == "choose"){
                <?php  $terminal_query = "SELECT distinct Terminal from mytable";?>
                <?php  $terminal_result = $connect->query($terminal_query); ?>
                <?php  while ($terminal_row = mysqli_fetch_array($terminal_result)) 
                {
                ?>

                      $('#terminal').append('<option value='<?php echo $terminal_row["Terminal"]; ?>'>'<?php echo $terminal_row["Terminal"]; ?>'</option>');          
                <?php
                }
                 ?>

        }

我想更改MYSql查询取决于所选的选项

1 个答案:

答案 0 :(得分:0)

Like I explained in my comment, use AJAX to do this, more specifically, $.getJSON中的链接:

首先创建一个基本API:

<强> api.php

<?php
$action = $_GET['action'];
if ($action == "choose") $terminal_query = "SELECT distinct Terminal from mytable";
// other actions here

$terminal_result = $connect->query($terminal_query);
$response = [];
if ($terminal_result) {
    while ($terminal_row = mysqli_fetch_array($terminal_result)) $response[] = $terminal_row['Terminal'];
}
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=utf-8");
die(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK));

在这个例子中,我发回的JSON看起来像这样:
[ "Terminal 1", "Terminal 2", "Terminal 3" ]

现在运行数据请求,并将其添加到HTML文档中,如下所示:

else if ($('#operator').val() == "choose") {
    $.getJSON("api.php?action=choose", function(response) {
        for (var i in response) {
            $('#terminal').append('<option value="' + response[i] + '">' + response[i] + '</option>');
        }
    });
}