简体   繁体   English

在PHP循环中运行jQuery AJAX从PHP脚本获取HTML

[英]Running jQuery AJAX in a PHP loop get HTML from a PHP script

I had to ask this one again, sorry. 对不起,我不得不再问一次。 So I am running this jQuery to slide toggle a span in my PHP loops output. 因此,我正在运行此jQuery,以在我的PHP循环输出中滑动切换范围。 I have some PHP that generates some calls to the db to get more detailed information back for each item. 我有一些PHP生成对数据库的一些调用,以获取有关每个项目的更详细的信息。

I know so far I have got it making the call to the PHP script. 我知道到目前为止,我已经调用了PHP脚本。 I can see the packets returning with the HTML echoed from the PHP file. 我可以看到从PHP文件回显的HTML返回的数据包。 but I'm having difficulty getting jQuery to insert the returned data into the specified span. 但是我很难让jQuery将返回的数据插入指定的范围。

This is a simplified version of what the loop items look like: 这是循环项的简化版本:

<span class="searchitem">

 <span style="visibility:hidden;" class="name">some name</span>

 <span class="btn">Details</span>

 <span class="itemdetails" style="display: none;">
   //hidden area to populate with returned ajax html from php script 
 </span>

</span>


<span class="searchitem">

 <span style="visibility:hidden;" class="name">another name</span>

 <span class="btn">Details</span>

 <span class="itemdetails">
   <div>
     <p>
     this is the area i need the html to populate
     </p>
   </div>
</span>

</span>.................

This is the jQuery that I'm trying to run. 这是我要运行的jQuery。 I know everything works up to success. 我知道一切都会成功。 I can't get the data from there. 我无法从那里获取数据。

<script type="text/javascript">   
      var ajax_load = "<img src='images/loading.gif' style='width:50px;' alt='loading...' />"; 
      var loadUrl = "ajax/item_details.php";
      $(document).ready(function ()
              {
                    $('.searchitem').each(function () {

                    var itemname = $(this).find('.name').text();
                    var ename = encodeURI(itemname);
                    var requestUrl = loadUrl + ename;
                        $(this).find('.itemdetails').hide();
                        $(this).find('.btn').click(function ()
                        {
                            var returned = "";
                                $.ajax({
                                type: "GET",
                                url: loadUrl,
                                data: "name=" + ename,
                                dataType: "text",
                                error: function () { alert("error") },
                                success: function (data) { $(".ptavail").append(data); alert("successful") }
                                }); 
                    $(this).parent().find('.ptavail').html(ajax_load).slideToggle(1500).html(returned);
                        });
                    });
              });
</script>

As you can see from the code I'm trying to get the click function the set everything off. 从代码中可以看到,我试图将click函数设置为关闭。 I know the PHP file is receiving the call and returning the data but I'm stuck trying to get the jQuery to fill the .itemdetails with the returned data. 我知道PHP文件正在接收调用并返回数据,但是我一直试图让jQuery用返回的数据填充.itemdetails。

What is wrong with this? 这有什么问题? Would I need to put the AJAX into a separate function for it to behave like I need, or do I need to make it synchronous for it to work? 我是否需要将AJAX放入一个单独的函数中以使其表现出所需的性能,或者是否需要使其同步才能工作? I'm trying to basically replace everything between .itemdetails spans with first a loading symbol and then the data returned with AJAX.... As it is now I get error alert so there's something wrong with the ajax call I know it does the request properly and the PHP returns the results but getting AJAX to read them is proving problematic. 我正在尝试基本上先用加载符号替换.itemdetails跨度之间的所有内容,然后用AJAX返回的数据...。由于现在是错误提示,所以ajax调用有问题,我知道它确实在处理请求正确,PHP返回了结果,但事实证明让AJAX读取它们是有问题的。

I can see that the content type in the headers is text, so how do I get the AJAX to do the call properly? 我可以看到标题中的内容类型是文本,那么如何获取AJAX才能正确进行调用?

Put $(this).parent().find('.itemdetails').html(ajax_load).slideToggle(1500).html(data); $(this).parent().find('.itemdetails').html(ajax_load).slideToggle(1500).html(data);

inside the "success" part of the Ajax request. 在Ajax请求的“成功”部分中。

success: function(data) {
  $(this).parent().find('.itemdetails').html(ajax_load).slideToggle(1500).html(data);
}

The success function is there to say "when the Ajax has worked, do this" 成功功能是这样说的:“当Ajax工作时,请执行此操作”

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

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