4

reset.php file:

 <?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 Ajax:

<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>

Hi i trying to call the php reset function in ajax but when i googling i know that not possibilities of direct calling php function so I put that particular function in seperate php file and call that php file i am not sure how can i do this in ajax i tried this above code but nothing happen. Settings Resets Message appear. How Can I do this Any Help would be great. Before using the ajax concept i tried with isset function in php But it getting page load every time for that only i jump into the ajax.

1 Answer 1

7

There is a standard way of achieving AJAX in WordPress plugin by relying on the admin_ajax.php. The file name is misleading, as it can be used in the frontend too, by assigning functions to AJAX actions.

There is a good description on the WordPress Codex:

http://codex.wordpress.org/AJAX_in_Plugins

One thing to be aware of: Your AJAX handler functions will always have to terminate with a die() command, to avoid the extra '0' output from WordPress.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks beerwin i use the ajax for about 1 week only. I read out the corresponding link. I call the php function when i click reset button its get the particular function(before use ajax i use the isset function). I am Not sure how can do this in ajax any help. Sorry I bit confuse with the corresponding link
Did you try that solution?
You cant actually call a PHP function directly from an Ajax call. What yiou can do is send a variable with the Ajax and listen for that variable inside the PHP file, if the variable value is true then call the function
Ya true , I used your concept for the submit button. But For Reset We Didn't pass any value. In Above also i posted the code what i did exactly. I still confuse with reset option.
Send some dummy data with your ajax request, and check for it in your PHP script: $.ajax({type: 'POST', url: '<?php echo WP_PLUGIN_URL; ?>/fantasticpopuptemplate/inc/reset.php', data: {reset: true} success: function(response)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.