从ajax调用获取数据

编程入门 行业动态 更新时间:2024-10-26 10:32:28
本文介绍了从ajax调用获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个选择框,我可以选择我的客户,每个客户可以有1个或多个网站,因此,当您更改客户选择时,网站的选择框也应更改,但是主要问题是我无法填写这些网站带有选项的选择框.

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.

我认为这应该是返回数组之类的最佳方法.

I thought it should be the best way to return an array or something.

推荐答案

我花了一些时间,但写了一些可行且简单的内容.另外,如果您使用的是AJAX,则实际上不需要表单标签.您可以通过潜水创建表单,并使用JQuery函数触发AJAX将数据发送到数据库.

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. You can create form with dives and use a JQuery function to fire AJAX to send data to the database.

主要用于填充您的第一个下拉列表的PHP.

Main PHP to populate your first drop down.

<?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="ajax.googleapis/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>

此PHP将接收AJAX post变量,并回显将填充第二个下拉列表的选项.

This PHP will receive the AJAX post variable and echo out the options which will populate the second drop down.

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>: }

此代码适用于您的AJAX,它将把您设置的kalntId发送到您的第一个下拉列表的第二个PHP onChange中. Ajax会将选定的客户端ID发送到PHP,以便您可以仅对该客户端进行站点排序.然后将捕获回显的输出并将其作为选项插入第二个下拉列表中.

This code is for your AJAX which will send the your set kalntId to the above second PHP onChange of your first drop down. The ajax will send the selected clients ID to the PHP so you can sort out the sites only for that client. 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.

P.S:如果我的代码中有任何错误,我不是mysqli_ *用户,请告诉我.另外,我使用了准备好的语句,其中第二个PHP绑定了该值,该值更加安全并可以防止SQL注入.

P.S: I'm not a big mysqli_* user if there's any errors in my code please let know. Also I used prepared statement where the second PHP binds the value which is much safer and prevents SQL injection.

更多推荐

从ajax调用获取数据

本文发布于:2023-10-29 13:00:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!