【问题标题】:Send php value with jQuery ajax function to php function使用 jQuery ajax 函数将 php 值发送到 php 函数
【发布时间】:2018-04-10 01:13:17
【问题描述】:

我有一个 ajax 表单,可以根据类别过滤帖子。 设置:

  1. HTML 表单
  2. PHP 函数回显输出
  3. jQuery ajax 请求加载 php 功能

问题:如何从 jQuery ajax 函数中解析一个值到 php 函数($test,见下文)?

表单 - 输出 html 选择字段和按钮以过滤帖子

<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
    <?php
        if( $terms = get_terms( 'category', 'orderby=name' ) ) : // to make it simple I use default categories
            echo '<select name="categoryfilter"><option>Select category...</option>';
            foreach ( $terms as $term ) :
                echo '<option value="' . $term->term_id . '">' . $term->name . '</option>'; // ID of the category as the value of an option
            endforeach;
            echo '</select>';
        endif;
    ?>
        <button>Apply filter</button>
    <input type="hidden" name="action" value="myfilter">
</form>
<div id="response"></div>

PHP 函数 - 在 HTML 表单中单击按钮时输出结果

function misha_filter_function($test){

// Do something with $test, but how to parse it with jQuery into this php function?     

$args = array(
            'orderby' => 'date', // we will sort posts by date
            'order' => $_POST['date'] // ASC или DESC
        );

        // for taxonomies / categories
        if( isset( $_POST['categoryfilter'] ) )
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'id',
                    'terms' => $_POST['categoryfilter']
                )
            );

        $query = new WP_Query( $args );

        if( $query->have_posts() ) :
            while( $query->have_posts() ): $query->the_post();
                echo '<h2>' . $query->post->post_title . '</h2>';
            endwhile;
            wp_reset_postdata();
        else :
            echo 'No posts found';
        endif;

        die();
    }


    add_action('wp_ajax_myfilter', 'misha_filter_function');
    add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');

jQuery - 问题:如何将 php 值 $test 解析为上述 php 函数?

jQuery(function($){
    $('#filter').submit(function(){
        var filter = $('#filter');
        $.ajax({
            url:filter.attr('action'),
            data:filter.serialize(), // form data
            type:filter.attr('method'), // POST
            beforeSend:function(xhr){
                filter.find('button').text('Processing...'); // changing the button label
            },
            success:function(data){
                filter.find('button').text('Apply filter'); // changing the button label back
                $('#response').html(data); // insert data
            }
        });
        return false;
    });
});

【问题讨论】:

    标签: javascript php jquery ajax wordpress


    【解决方案1】:

    您序列化由以下对象访问的数据:

    parse_str($_POST['data'], $inputValues); //$inputValues will be array with your form fields

    【讨论】:

    • 谢谢。你能通过使用/修改我的代码来帮助我吗?
    • 只需重写代码以使用$inputValues:$inputValues = array(); parse_str($_POST['data'], $inputValues); if( isset( $inputValues['categoryfilter'] ) )
    • 好的,如何使用 jQuery 将值发送到 php 函数?那么如何改变它发送数据的jQuery函数呢?
    • @Bob 您已经将数据从 jQuery 发送到 php 脚本 - 使用 $.ajax()。现在您可以直接从 $_POST 获取 php 函数中的这些数据。
    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 2013-12-21
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多