阿贾克斯在PHP中环路上的按钮,点击执行操作 [英] ajax in php loop perform action on button click

查看:104
本文介绍了阿贾克斯在PHP中环路上的按钮,点击执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个PHP的循环,我使用jQuery的幻灯片切换隐藏/显示一个表的SQL结果。目前该表只使用PHP加载,但有很多事情它造成部分负荷的问题,我需要在火中的幻灯片切换BTN阿贾克斯,所以只要求当前项目的详细信息按钮时,pressed 。我可以得到它调用从jQuery,但有困难传递值在每一个项目的IM PHP文件,因此它可以在数据库上执行的请求。这里是PHP的foreach循环的内容看起来像;

so i have a php loop, i am using jquery slide toggle to hide/show a table with sql results. currently the table is loaded using php only, but as there is a lot going on its causing some loading problems i need to fire the ajax with the slide toggle btn, so it only requests the current items details when the button is pressed. i can get it to call the php file from the jquery but im having difficulty passing the value for each item across, so it can perform the request on the database. here is what the php foreach loop content looks like;

<span class="searchitem">
  // some visible content here 
  <span value="item name" class="btn">button</span>
  <span class="slide_area">
    // hidden slide content ajax needs to populate with php result
  </span>
</span>

这个网站是重复使用jQuery的slideToggle隐藏slide_area,我需要做的是用来自PHP的结果slide_area,PHP文件需要的名称,返回的名称传递通过与URL得到的结果,所以我只需要追加URL与干训局的价​​值的实际名称,我确定这是不能忽视的是困难的,但我在这里。

this html is repeated i use jquery slidetoggle to hide the slide_area, what i need to do is populate the slide_area with results from php, the php file needs the name to return the results the name is passed via get with the url, so i only need append the url with the actual name from btn's value, im sure this cant be that difficult but here i am.

下面是jQuery的:

here's the jquery:

<script type="text/javascript">
//<![CDATA[  
    $(document).ready(function ()
              {
                    $('.searchitem').each(function () {
                        $(this).find('.slide_area').hide();
                        $(this).find('.btn').click(function ()
                        {
                            var ajax_load = "<img src='images/spinner.gif' style='width:50px;' alt='loading...' />";  
                            var loadUrl = "ajax/item.php?name=";    
                            var loadName = $(".btn");
                            var Name = URLEncode(loadName);
                            var loadString = loadUrl + Name;
         $(this).parent().find('.slide_area').slideToggle(1500).html(ajax_load).load(loadString);
                        });
                    });
              });
 //]]>
 </script>

我需要从BTN获得的价值和附加使用loadURL,即时通讯开放的,如果需要通过后期发送数据以不同的方式一样,更新了jQuery仍然没有工作我究竟做错了什么?

i need to get the value from btn and append the loadURL, im open to sending the data a different way like through post if needed, updated the jquery still not working what am i doing wrong here?

感谢。

推荐答案

最简单的办法就是使用Phery库 http://phery-php-ajax.net/ 这里的关键是数据phery遥控=切换,它会自动调用点击PHP函数,并且到处都可以重复使用

The easiest way would to use the Phery library http://phery-php-ajax.net/ the key here is the data-phery-remote="toggle" that will call the PHP function automatically on click, and can be reused everywhere

<span class="searchitem">
  // some visible content here 
  <span value="item name" data-phery-remote="toggle" class="btn">button</span>
  <span class="slide_area">
  // hidden slide content ajax needs to populate with php result
  </span>
</span>

的逻辑反向,负载将只有一个AJAX调用发生,而不是两个,

The logic is reversed, the load will happen with only one AJAX call, instead of two.

Phery::instance()->set(array(
  'toggle' => function($data){
    $r = new PheryResponse;
    /* do your code, fill $html_content */
    $r->this()->siblings('.slide_area')->html($html_content)->toggle();
    return $r;
  }
))->process();

这篇关于阿贾克斯在PHP中环路上的按钮,点击执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆