Memor.の 2018-05-04 15:21 采纳率: 100%
浏览 19

WordPress的-与ajax的形式

I want to make filter with checkboxes in Wordpress. What I firstly want to achieve is to test it and send variable with ajax on checkbox click in the form.

Here is the form with check boxes:

<form  method="post" id="filter">
<input type="checkbox" name="f" value="1" onchange="this.form.submit()" <?php if  ($face =='1') {echo 'checked';}?>
<input type="checkbox" name="t" value="1" onchange="this.form.submit()" <?php if ($twitter=='1') {echo 'checked';}?>    
<input type="hidden" value="myfilter"> 
</form>

Here is javascript:

jQuery(document).ready( function(){  
    jQuery('#content').on('submit', '#filter', function(e) {
        e.preventDefault();     
        var test = jQuery('#test-btn').data( 'id' );

        jQuery.ajax({           
            url : rml_obj.ajax_url,
            type : 'post',
            data : {
                action : 'test_function',
                security : rml_obj.check_nonce,
                test_data : test
            },
            success : function( response ) {
                alert (test);               
                jQuery('#result').html(response);

            }
        }); 
    });
});

Here is the function in functions.php:

function test_function() {  
    check_ajax_referer( 'rml-nonce', 'security' );  
    $test_data = $_POST['test_data'];
    echo $test_data; 
    die();
}
add_action('wp_ajax_test_function', 'test_function'); 
add_action('wp_ajax_nopriv_test_function', 'test_function');

For now I'm just trying to test it. So when user clicks on checkbox in form that ajax is called. But I'm stuck. If I add this line to form action action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" , then on submit I'm stuck on ajax url: admin-ajax.php. If I remove action line from form then page simple reloads.

When I'm using click on element, instead of form submit I'm succeeding. But I need to make it work on form submit so I can implement filtering by clicking on various checkboxes.

If anybody can tell me what I'm doing wrong, I will build on this and actually send checkbox values from the form and execute query in function for filtering the data.

  • 写回答

1条回答 默认 最新

  • weixin_33694620 2018-05-08 12:50
    关注

    The problem was in form onchange="this.form.submit()". After removing that part I was able to proceed.

    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作