简体   繁体   English

如何将php变量从链接传递到jquery ajax函数

[英]how to pass php variable from a link to a jquery ajax function

i am trying to pass two variable from a link to my jquery ajax function. 我试图将两个变量从链接传递给我的jquery ajax函数。 But i don't have any idea how to do these ajax stuffs. 但是我不知道如何做这些ajax的东西。

i have a link with two ids. 我有两个ID的链接。 1. rowid & 2nd. 1. rowid和2nd。 bookid. 书本。 I have to pass these two id to my jquery function. 我必须将这两个id传递给我的jquery函数。

Please help me how can i do this. 请帮我怎么做。

`//cart item displaying in a for each loop , each row have a 'remove' link with two id 
//say i have $id='4dsf2323' & $bid='43' now i have to pass these two ids to my jquery function on click of a link     


<a id="removeid" >Remove item from cart link</a>`

My jquery function 我的jQuery函数

    <script>
        $('removeid').click(function(e) {
      // prevent the default action when a nav button link is clicked
      e.preventDefault();

              //HOW TO GET HERE THOSE TWO IDS FROM MY ABOVE LINK ON CLICK EVENT

      // ajax query to retrieve the HTML view without refreshing the page.
      $.ajax({
        type: 'get',
        url: '/path/to/your/controller/method',
        dataType: 'html',
        success: function (html) {
          // success callback -- replace the div's innerHTML with
          // the response from the server.
          $('#yourDiv').html(html);
        }
      });
    });

    </script>

***********Update************** Output looks like this ***********更新**************输出看起来像这样 在此处输入图片说明

How about making use of jQuery's .data() functionality ? 如何利用jQuery的.data()功能 Add HTML like this: 像这样添加HTML:

<a id="removeid" data-id="<?php echo $id;?>" data-bid="<?php echo $bid;?>">Remove item from cart link</a>

And then retrieve with .data() like this: 然后使用.data()进行检索,如下所示:

$('removeid').click(function(e){
    e.preventDefault();

    var $aid = $('#removeid'),
        id = $aid.data('id'),
        bid = $aid.data('bid');

    // rest of the ajax stuff that uses the data
});

Should work just fine, as long as you don't use camelCase . 只要您不使用camelCase应该就可以正常工作。

EDIT, because apparently remove links are in a loop and they have the same ID? 编辑,因为显然删除链接处于循环中并且它们具有相同的ID?

Set your item with a class: 为课程设置项目:

<a class="RemoveClass" data-id="<?php echo $id;?>" data-bid="<?php echo $bid;?>">Remove item from cart link</a>

And then use $(this) inside of a click function to capture the data values only of the item clicked: 然后在click函数中使用$(this)仅捕获被单击项的数据值:

$('.RemoveClass').on('click',function(e){
    e.preventDefault();

    var $this = $(this),
        id = $this.data('id'),
        bid = $this.data('bid');

    // rest of the ajax stuff that uses the data
});

If you do this correctly, it will be localized to the link that has been clicked. 如果正确执行此操作,它将被本地化为已单击的链接。

As a side note, I have changed the syntax to be the more versatile .on() event handler. 附带说明一下,我已将语法更改为更通用的.on()事件处理程序。 If you want to do something else with the item in the future, like a hover event or something, you can include it with the same .on() bind rather than create a separate bind for the different event. 如果您想在将来对项目进行其他操作(例如,悬停事件或其他操作),则可以使用相同的.on()绑定将其包括在内,而不是为不同的事件创建单独的绑定。

Pass the data to the link with data attributes: 将数据传递到具有数据属性的链接:

         <a id="removeid" data-bid="<?=$bid?>" data-id="<?=$id?>" >Remove item from cart link</a>

And your jQuery javascript should look like this: 而且您的jQuery javascript应该如下所示:

  $('removeid').click(function(e) {
      // prevent the default action when a nav button link is clicked
      e.preventDefault();
      var id = $.data(this,'id'); // or $(this).attr('data-id');
      var bid = $.data(this,'bid'); // or $(this).attr('data-bid');

      // ajax query to retrieve the HTML view without refreshing the page.
      $.ajax({
           type: 'get',
           url: '/path/to/your/controller/method',
           data: {bid:bid,id:id}
           dataType: 'html',
           success: function (html) {
           // success callback -- replace the div's innerHTML with
           // the response from the server.
           $('#yourDiv').html(html);
       }
  });
});

or you can specify the whole url to href attribute of the link, and just: 或者,您可以指定链接的整个URL到href属性,只需:

  <a id="removeid" href="/path/to/your/controller/method?bid=<?=$bid?>&id=<?=$id?>" >Remove item from cart link</a>

And js become: js变成:

   $('removeid').click(function(e) {
      // prevent the default action when a nav button link is clicked
      e.preventDefault();

      // ajax query to retrieve the HTML view without refreshing the page.
      $.ajax({
           type: 'get',
           url: $(this).attr('href'),
           dataType: 'html',
           success: function (html) {
           // success callback -- replace the div's innerHTML with
           // the response from the server.
           $('#yourDiv').html(html);
       }
  });

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

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