【问题标题】:Pass parameter to PHP function through AJAX通过 AJAX 将参数传递给 PHP 函数
【发布时间】:2017-04-14 23:58:37
【问题描述】:

我在 WordPress 网站上有一个 ajax 调用,如下所示:

$('.stm-ajax-checkbox-button .button, .stm-ajax-checkbox-instant .stm-option-label input').click(function(e){

        if($(this)[0].className == 'button') {
            e.preventDefault();
        }

        var sold = '';

         if( $('.soldlistings').length ) {
             sold = '(true)';
         }  

        $.ajax({
            url: ajaxurl,
            dataType: 'json',
            context: this,
            data: $(this).closest('form').serialize() + '&action=stm_ajax_filter',
            beforeSend: function () {
                $(this).closest('.stm-accordion-content-wrapper .stm-accordion-inner').addClass('loading');
                $('.stm-ajax-row').addClass('stm-loading');
                $('.classic-filter-row .filter-sidebar .select2-container--default .select2-selection--single .select2-selection__arrow b').addClass('stm-preloader');
            },
            success: function (data) {
                $(this).closest('.stm-accordion-content-wrapper .stm-accordion-inner').removeClass('loading');
                $('.stm-ajax-row').removeClass('stm-loading');
                $('.classic-filter-row .filter-sidebar .select2-container--default .select2-selection--single .select2-selection__arrow b').removeClass('stm-preloader');
                $('.classic-filter-row .filter-sidebar select').prop("disabled", false);

                if(typeof data.html != 'undefined') {
                    $('.stm-ajax-row .stm-isotope-sorting:not(.stm-isotope-sorting-featured-top)').html(data.html);

                    var sortVal = $('.stm-select-sorting select').select2('val');

                    $('.stm-ajax-row').removeClass('stm-loading');
                }

                stm_after_ajax_same_actions(data, sortVal);
            }
        })
    });

然后链接到一个 PHP 函数(我不会粘贴整个代码),如下所示:

function stm_ajax_filter() {
}

我想为这个函数添加一个参数,然后我将使用它来帮助确定使用 Ajax 调用的位置。为此,我在 PHP 函数中添加了一个参数。如何在 Ajax 调用中设置这个参数?

【问题讨论】:

  • 使用位置是指客户端的 IP 地址,还是从应用程序中的哪个页面调用它?
  • 可以发ajaxurl的内容吗
  • 它在哪个页面上。我打算在一个页面上放一个div,做一个if语句看是否可以找到,如果可以,然后将AJAX调用中的参数设置为true,如果不能,则不要设置它。
  • ajaxurl的上下文是http://{website}/wp-admin/admin-ajax.php

标签: php jquery ajax wordpress function


【解决方案1】:

为什么不向 AJAX 数据属性添加另一个参数?

data: $(this).closest('form').serialize() + '&origin=mainPage&action=stm_ajax_filter',

然后在 PHP 中,您可以检索 'origin' 键。

$ajaxOrigin = $_GET["origin"];

// and then pass that variable to your function as an argument
stm_ajax_filter($ajaxOrigin);

【讨论】:

    【解决方案2】:

    也许在这一行数据:$(this).closest('form').serialize() + '&action=stm_ajax_filter',

    我想你可以试试这个: 数据:$(this).closest('form').serialize() + '&action=stm_ajax_filter&your_variable='+参数,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2014-05-02
      相关资源
      最近更新 更多