简体   繁体   English

从ajax调用中获取数据

[英]get data from ajax call

I have a selectbox where i can choose my clients and each client could have 1 or more sites, so when you change your client selection the sites selectbox should also change, but teh main problem is that i can't fill the sites selectbox with options.我有一个选择框,我可以在其中选择我的客户,每个客户可以有 1 个或多个站点,因此当您更改客户选择时,站点选择框也应该更改,但主要问题是我无法用选项填充站点选择框.

I thought it should be the best way to return an array or something.我认为这应该是返回数组或其他东西的最佳方式。

Try adding something like this to your ajax success:尝试将这样的内容添加到您的 ajax 成功中:

var dropdown = document.getElementById('SiteDropDown');
options.forEach(function(element))
{
    dropdown.options[dropdown.options.length] = new Option(element, '', false, false);
});  

Essentially creating an array with the information you want to be displayed, is a good idea.基本上创建一个包含您想要显示的信息的数组是一个好主意。 But you should try to also work with a value on the select.但是您也应该尝试在选择上使用一个值。 So when you select an an option the system will know it's id.因此,当您选择一个选项时,系统将知道它的 ID。 -> dropdown.options[dropdown.options.length] = new Option(element, 'ID of Array Element', false, false); -> dropdown.options[dropdown.options.length] = new Option(element, 'ID of Array Element', false, false);

I took a little bit of time but wrote something that might work and simple.我花了一点时间,但写了一些可能有用且简单的东西。 Also if you're using AJAX you really don't need to have form tags.此外,如果您使用 AJAX,您真的不需要表单标签。 You can create form with dives and use a JQuery function to fire AJAX to send data to the database.您可以使用潜水创建表单并使用 JQuery 函数触发 AJAX 以将数据发送到数据库。

Main PHP to populate your first drop down.主要的 PHP 来填充你的第一个下拉菜单。

<?php
  include_once("../Services/Mysql.php");
  $result = $conn->query("SELECT klant_firmanaam, id From connectprolivedesk.klanten where reseller_klantnr = '5375' order by klant_firmanaam");
?>

<html>
 <head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
    <div class="form-group">
        <select name="klantenDropDown" id="klantenDropDown" class="form-control">
            <option value="" selected hidden disabled>Select Klant</option>
                <?php
                    if($result->num_rows > 0){
                        while($row = $result -> fetch_assoc()){
                            echo "<option value=".$row["id"]." >".$row["klant_firmanaam"]."</option>";
                        }
                    }
                ?>
        </select>
    </div>
        <div class="form-group">
            <select name="SiteDropDown" id="SiteDropDown" class="form-control" >
            <option value="" selected disabled hidden>Select Site</option>
            </select>
        </div>
 </div>
</body>

This PHP will receive the AJAX post variable and echo out the options which will populate the second drop down.这个 PHP 将接收 AJAX post 变量并回显将填充第二个下拉列表的选项。

include_once("../Services/Mysql.php");

$result = $conn->prepare("SELECT site_naam FROM connectprolivedesk.sites WHERE klant_id = ?");
$result -> bind_param("s",$_POST["klantId"]);
$result -> execute();

while($row = $result -> fetchAll()){
   echo "<option value=".$row['sId'].">".$row['site_name']."</option>:
}

This code is for your AJAX which will send the your set kalntId to the above second PHP onChange of your first drop down.此代码用于您的 AJAX,它将您设置的 kalntId 发送到您的第一个下拉列表的上述第二个 PHP onChange。 The ajax will send the selected clients ID to the PHP so you can sort out the sites only for that client. ajax 会将选定的客户端 ID 发送到 PHP,以便您可以只为该客户端整理站点。 Then will capture the echoed output and insert those as option in to the second drop down.然后将捕获回显的输出并将它们作为选项插入到第二个下拉列表中。

<script>
    $(document).ready(function(){
        $('#klantenDropDown').on('change', function(){
            $.ajax({
                url: 'getSites.php',
                type: 'post', //You can use get or post
                data: {
                    'klantId':$(this).val()
                },
           dataType:'text',
           complete:function(data){
            if(data){
                    $('#SiteDropDown').html(data.responseText);
                }
            }
        }
    }
</script>

Hope this helps you or any other.希望这对您或任何其他人有帮助。

PS: I'm not a big mysqli_* user if there's any errors in my code please let know. PS:如果我的代码中有任何错误,请告诉我,我不是一个大的 mysqli_* 用户。 Also I used prepared statement where the second PHP binds the value which is much safer and prevents SQL injection.我还使用了准备好的语句,其中第二个 PHP 绑定了更安全并防止 SQL 注入的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM