简体   繁体   English

如何使用 JS/JQuery 显示数组中的单个项目

[英]How to display single items from array using JS/JQuery

Its my first Question here, so be kind ^^ Im training myself some JS/Jquery/Ajax.这是我的第一个问题,所以请善待 ^^ 我在训练自己一些 JS/Jquery/Ajax。 Short: Projekt to filter closest location of a specific company to our current postal zip. So, here is my "work" so far: On index.php im using a simple form like this:简短:Projekt 过滤特定公司最近的位置到我们当前的邮政 zip。所以,这是我到目前为止的“工作”:在 index.php 上,我使用这样的简单形式:

<form id="ajaxForm">
                <input type="number" class="form-control" id="plzInput" name="plzInput" placeholder="99999"
                       aria-label="Username" aria-describedby="basic-addon1" required>
                <button type="submit" id="plzSubmit" name="plzSubmit" class="btn btn-primary">Search</button>
            </form>

Im passing some data to ajax.php using ajax:我使用 ajax 将一些数据传递给 ajax.php:

 var request;
     $("#ajaxForm").submit(function (event) {
         event.preventDefault();
         if (request) {
             request.abort();
         }
         var $form = $(this);

         //Caching
         var $inputs = $form.find("input, select, button, textarea");

         var serializedData = $form.serialize();
         $inputs.prop("disabled", true);
         request = $.ajax({
             url: "ajax.php",
             type: "post",
             data: serializedData
         });
         request.done(function (response, textStatus, jqXHR) {
             console.log("in ajax.php geschrieben!");
             console.log(response);
             showResults(response);


         });
         request.fail(function (jqXHR, textStatus, errorThrown) {
             console.error(
                 "The following error occurred: " +
                 textStatus, errorThrown
             );
         });
         request.always(function () {
             $inputs.prop("disabled", false);
         });
     });

     function showResults(data){
         alert(data);

     }
 });

Here the data finally getting handled using php (db queries, calculations, result).此处数据最终使用 php 进行处理(数据库查询、计算、结果)。 Now im struggling on how to send my resuslts from ajax.php back to index.现在我正在努力研究如何将我的结果从 ajax.php 发送回索引。 All in all, i want to send back an array from SQLquery, and show those values into existing html inputs using js/jquery on index.php.总而言之,我想从 SQLquery 发回一个数组,并使用 index.php 上的 js/jquery 将这些值显示到现有的 html 输入中。 But i don't know why the response of ajax.php isnt readable by the JS/Jquery.但我不知道为什么 ajax.php 的响应不能被 JS/Jquery 读取。 I think there is something wrong with php arrays vs js arrays. Is there a way to send "multiple" responses or even an array of responses?我认为 php arrays 与 js arrays 有问题。有没有办法发送“多个”响应甚至一组响应?

im glad for every help!我很高兴得到每一个帮助!

A good way to do this is by using JSON.执行此操作的一个好方法是使用 JSON。

On the PHP side, in ajax.php , you use json_encode() to encode your array as a string.在 PHP 方面,在ajax.php中,您使用json_encode()将数组编码为字符串。

That JSON string is then received in your Javascript AJAX as the response .然后在您的 Javascript AJAX 中收到该 JSON 字符串作为response There you can turn it back into an array with JSON.parse() :在那里你可以用JSON.parse()把它变回一个数组:

let array = JSON.parse(response);

JQuery also has a shorthand for this, see: JQuery 也有这个的简写,见:

https://api.jquery.com/jQuery.getJSON/ https://api.jquery.com/jQuery.getJSON/

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

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