在ajax中从wordpress调用PHP函数?

在ajax中从wordpress调用PHP函数?,php,ajax,wordpress,jquery,Php,Ajax,Wordpress,Jquery,reset.php文件: <?php add_action('admin_init','popup_template_reset_options'); function popup_template_reset_options() { delete_option('popup_template_on'); delete_option('popup_template_close'); delete_opt

reset.php文件:

 <?php 
    add_action('admin_init','popup_template_reset_options');

    function popup_template_reset_options() 
        {
        delete_option('popup_template_on');
        delete_option('popup_template_close');
        delete_option('popup_template_escape');
        delete_option('popup_template_external');
        delete_option('popup_template_home_page');
        delete_option('popup_template_all_pages');
        delete_option('popup_template_template');
        delete_option('popup_cookies_display_after_like');
        add_option('popup_cookies_display_after_like','365');
        //add_option('popup_template_on','1');
        add_option('popup_template_close','1');
        add_option('popup_template_escape','1');
        add_option('popup_template_external','1');
        add_option('popup_template_force_timer','2');
        add_option('popup_template_home_page','1');
        add_option('popup_template_all_pages','1');
        add_option('popup_template_template','2');

    }
    ?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#reset_general').click(function()
        {
            $('#result1').css("display", "block");
            jQuery('#result1').animate({'opacity': '1'});

        });
    });
    function resetgeneral() {
        $.ajax({type: 'POST', url: '<?php echo WP_PLUGIN_URL; ?>/fantasticpopuptemplate/inc/reset.php', success: function(response) {

                //$('#fff').find('.form_result').html(response);
                $('#result1').css("display", "none");
                $('#resets1').css("display", "block");
                $('#resets1').html("Settings Resets");
                $('#resets1').fadeOut(2500, "linear");
            }});

        return false;
    }

</script>
<form onsubmit="return resetgeneral();" id="form_general_reset" class="form-1">
    <input type="submit" value="Reset" id="reset_general" name="reset" class="button-secondary"/>
    <input name="action" type="hidden" value="reset" />

</form>

脚本Ajax:

 <?php 
    add_action('admin_init','popup_template_reset_options');

    function popup_template_reset_options() 
        {
        delete_option('popup_template_on');
        delete_option('popup_template_close');
        delete_option('popup_template_escape');
        delete_option('popup_template_external');
        delete_option('popup_template_home_page');
        delete_option('popup_template_all_pages');
        delete_option('popup_template_template');
        delete_option('popup_cookies_display_after_like');
        add_option('popup_cookies_display_after_like','365');
        //add_option('popup_template_on','1');
        add_option('popup_template_close','1');
        add_option('popup_template_escape','1');
        add_option('popup_template_external','1');
        add_option('popup_template_force_timer','2');
        add_option('popup_template_home_page','1');
        add_option('popup_template_all_pages','1');
        add_option('popup_template_template','2');

    }
    ?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#reset_general').click(function()
        {
            $('#result1').css("display", "block");
            jQuery('#result1').animate({'opacity': '1'});

        });
    });
    function resetgeneral() {
        $.ajax({type: 'POST', url: '<?php echo WP_PLUGIN_URL; ?>/fantasticpopuptemplate/inc/reset.php', success: function(response) {

                //$('#fff').find('.form_result').html(response);
                $('#result1').css("display", "none");
                $('#resets1').css("display", "block");
                $('#resets1').html("Settings Resets");
                $('#resets1').fadeOut(2500, "linear");
            }});

        return false;
    }

</script>
<form onsubmit="return resetgeneral();" id="form_general_reset" class="form-1">
    <input type="submit" value="Reset" id="reset_general" name="reset" class="button-secondary"/>
    <input name="action" type="hidden" value="reset" />

</form>

$(文档).ready(函数(){
$(“#重置_常规”)。单击(函数()
{
$('#result1').css(“显示”、“块”);
jQuery(“#result1”).animate({'opacity':'1');
});
});
函数resetgeneral(){
$.ajax({type:'POST',url:'/fantasticpopuptemplate/inc/reset.php',success:function(response){
//$('#fff').find('.form#result').html(响应);
$('#result1').css(“显示”、“无”);
$('#resets1').css(“显示”、“块”);
$('#resets1').html(“设置重置”);
$('#resets1')。淡出(2500,“线性”);
}});
返回false;
}

嗨,我试图在ajax中调用php重置函数,但当我在谷歌上搜索时,我知道不可能直接调用php函数,所以我将该特定函数放在单独的php文件中,并调用该php文件。我不确定如何在ajax中做到这一点。我尝试了上面的代码,但什么都没有发生。设置重置消息出现。我怎样才能做到这一点任何帮助都将是巨大的。在使用ajax概念之前,我曾尝试在php中使用
isset
函数,但每次都会有页面加载,因此只有我跳入ajax。

在WordPress插件中,有一种标准的方法可以通过依赖admin_ajax.php来实现ajax。文件名有误导性,因为它也可以通过将函数分配给AJAX操作在前端使用

WordPress Codex上有一个很好的描述:


需要注意的一点是:您的AJAX处理程序函数必须始终使用
die()
命令终止,以避免WordPress额外的“0”输出。

谢谢beerwin,我只使用了大约一周的AJAX。我读出了相应的链接。我在单击reset按钮时调用php函数,以获取特定的函数(在使用ajax之前,我使用isset函数)。我不知道如何在ajax中做到这一点有任何帮助。抱歉,我有点混淆了相应的链接。您尝试过该解决方案吗?您不能直接从Ajax调用调用PHP函数。YOU可以做的是用Ajax发送一个变量,并在PHP文件中侦听该变量,如果变量值为true,则调用函数a true,我将您的概念用于submit按钮。但对于重置,我们没有传递任何值。在上面,我也发布了我所做的代码。我仍然混淆了重置选项。在ajax请求中发送一些虚拟数据,并在PHP脚本中检查它:
$.ajax({type:'POST',url:'/fantasticpopuptemplate/inc/reset.PHP',数据:{reset:true}成功:函数(响应).