插件表单提交给admin-ajax.php而不是admin-post.php

时间:2015-03-06 作者:Ravioli87

我正在构建一个插件,在单击时将表单加载到表格单元格中。

以下是AJAX:

    public static function cp_libhours_ajax(){
?>
<script>
    jQuery(document).ready(function(){
        jQuery(\'.libhours-row a\').click(function(e){
            e.preventDefault(); //don\'t let the link click through

            //this is the cell in which the form will be placed
            var cellToReplace = jQuery(this).parent(\'td\').prop(\'className\');

            //extract the values of the parameters passed in the URL
            var querystring = jQuery(this).prop(\'href\').split("?")[1];
            var values = querystring.split("&"); 

            var count = 0;
            var param = [];
            jQuery(values).each(function(index, element) {
                param[count] = element.split("=")[1];
                count++;
            });

            //data object to be passed to the ajac function
            var data = {
                \'action\': \'cp_libhours_ajax\',
                \'semester\': param[0],
                \'day_of_week\': param[1]
            };
            // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
            jQuery.post(ajaxurl, data, function(response) {
                jQuery(\'.\' + cellToReplace).html(response);
            });
        });
    });
</script>
以下是回调函数生成的响应:

/**
 * CALLED VIA AJAX!
 *
 * within public static function cp_libhours_ajax()
 */
public static function cp_libhours_ajax_callback() {
    global $wpdb; // this is how you get access to the database

    $semester = $_POST[\'semester\'];
    $day_of_week = $_POST[\'day_of_week\'];

    $output = 
        \'  
        <form method="post" action="\'.admin_url(\'admin-post.php\').\'">
            \'.wp_nonce_field(\'cp_libhours_pest_control\',\'cp_libhours_pest_control_field\').\'
            <fieldset>
              <p class="clearfix">  
                <label for="regular_open">Open:</label>
                <input type="text" id="regular_open" name="regular_open" class="timepicker">
                <label for="regular_close" >Close:
                <input type="text" id="regular_close" name="regular_close" class="timepicker">
                </label>
              </p>
              <p>
                <input type="checkbox" id="regular_24" name="regular_24"> 24-HR
                <input type="checkbox" id="regular_closed" name="regular_closed"> Closed
              </p>
              <div>
                <button type="submit">Save</button>
                <button type="reset">Cancel</button>
                <input type="hidden" name="semester" value="\'.$semester.\'">
                <input type="hidden" name="day_of_week" value="\'.$day_of_week.\'">
                <input type="hidden" name="action_type" value="add_regular_hour">
              </div>
                </fieldset>
                <input type="hidden" name="action" value="hours_action">
                <input type="hidden" name="first_year" value="2014">
                <input type="hidden" name="area_id" value="1">
        </form> 
        \';
    echo $output;
    wp_die(); // this is required to terminate immediately and return a proper response
}
表单在页面上显示得很好。但是,当需要提交时,表单不会转到操作标记(admin post.php)中指定的链接,而是转到admin ajax。php。我得到一个带有“0”的白色页面。

2 个回复
SO网友:Mark Kaplun

这是因为您在ajax请求中指定的url与表单中使用的url不同。

Query.post(ajaxurl, data, function(response) {
您不会在代码段中显示它,但是ajaxurl 通常设置为{site url}\\wp-admin\\admin-ajax.php.

SO网友:Ituk

尝试添加return false; 在ajax调用之后。像这样:

  jQuery.post(ajaxurl, data, function(response) {
    jQuery(\'.\' + cellToReplace).html(response);
  });
return false;
});
这样ajax只调用一次。

结束