【问题标题】:Why zero after ajax call为什么ajax调用后为零
【发布时间】:2013-10-20 06:40:39
【问题描述】:

我是第一次使用 WordPress 学习 Ajax。我正在使用小部件实现 Ajax 功能,但在提交表单后我得到 0。

您能否指导我并解释为什么会发生这种情况,以便我更好地理解。

我的代码:

wp_enqueue_script('jquery');

/**
 * Add function to widgets_init that'll load our widget.
 * @since 0.1
 */
add_action( 'widgets_init', 'example_load_widgets' );

/**
 * Register our widget.
 * 'Example_Widget' is the widget class used below.
 *
 * @since 0.1
 */
function example_load_widgets() {
    register_widget( 'Example_Widget' );
}

/**
 * Example Widget class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 0.1
 */
class Example_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function Example_Widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'example', 'description' => __('An example widget that displays a person\'s name and sex.', 'example') );

        /* Widget control settings. */
        $control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );

        /* Create the widget. */
        $this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
    }

    /**
     * How to display the widget on the screen.
     */
    function widget() {
            echo $name = $_POST['name'];
            ?>
            <form type="post" action="" id="newCustomerForm">

                <label for="name">Name:</label>
                <input name="name" type="text" />

                <input type="hidden" name="action" value="example_load_widgets"/>
                <input type="submit" name="submit" value="Submit">
            </form>
            <br/><br/>
            <div id="feedback"></div>
            <br/><br/>

            <script type="text/javascript">
                jQuery('#newCustomerForm').submit(ajaxSubmit);

                function ajaxSubmit(){

                    var newCustomerForm = jQuery(this).serialize();

                    jQuery.ajax({
                        type:"POST",
                        url: "http://localhost/testing/wordpress/wp-admin/admin-ajax.php",
                        data: newCustomerForm,
                        success:function(data){
                            jQuery("#feedback").html(data);
                        },
                        error: function(errorThrown){
                            alert(errorThrown);
                        }  
                    });

                    return false;
                }
            </script>

            <?php
            die();
            add_action('wp_ajax_example_load_widgets', 'example_load_widgets');
            add_action('wp_ajax_nopriv_example_load_widgets', 'example_load_widgets');

        }
    }

【问题讨论】:

  • 您的代码有很多个问题,我建议重新开始。如果您在这里和WordPress Development 搜索我的答案,您会发现许多适用于 WP 的 Ajax 工作示例。然后,您必须将其中一个与有效的小部件代码集成。哦,是的,你应该做一个插件来实现这个。
  • 感谢您的重播和指导。我已经为 ajax 参考了本教程,并尝试了小部件。 http://www.makeuseof.com/tag/tutorial-ajax-wordpress
  • 我建议你想出一些工作代码。如果没有给出答案,我认为你完全改变问题是可以的。如果你这样做,请联系我,我会看一下并撤回接近投票。
  • 就像 brasofilo 说的那样 - 那里有很多问题 - 但作为一个快速说明:从最近的 wordpress 开始,ajax url 应该始终以 ajaxurl 的形式提供......另外,你能看看控制台日志吗和要分享的错误?

标签: php jquery ajax wordpress


【解决方案1】:

我已经创建了像你这样的 ajax 示例,可能会对你有所帮助。

试试这个:

首先创建插件并添加以下代码:

<?php
    function sampleHelloWorld() {
    ?>
        <form type="post" action="" id="newCustomerForm">
            <label for="name">Name:</label>
            <input name="name" type="text" /><br /><br />

            <input type="hidden" name="action" value="addCustomer"/>
            <input type="submit" name="submit" value="Submit">
        </form>

        <br/><br/>
        <div id="feedback"></div>
        <br/><br/>

        <script type="text/javascript">
            jQuery('#newCustomerForm').submit(ajaxSubmit);

            function ajaxSubmit(){

                var newCustomerForm = jQuery(this).serialize();

                jQuery.ajax({
                    type:"POST",
                    url: "http://localhost/testing/wordpress/wp-admin/admin-ajax.php",
                    data: newCustomerForm,
                    success:function(data){
                        jQuery("#feedback").html(data);
                    },
                    error: function(errorThrown){
                        alert(errorThrown);
                    }  
                });

                return false;
            }
        </script>
        <?php
    }

function widget_myHelloWorld($args) {
  extract($args);
  echo $before_widget;
  echo $before_title;?>Hello World Ajax<?php echo $after_title;
  sampleHelloWorld();
  echo $after_widget;
}

function myHelloWorld_init(){
  register_sidebar_widget(__('Hello World'), 'widget_myHelloWorld');     
}
add_action("plugins_loaded", "myHelloWorld_init");

wp_enqueue_script('jquery');

function addCustomer(){
    echo $name = $_POST['name'];
    die(); 
}

add_action('wp_ajax_addCustomer', 'addCustomer');
add_action('wp_ajax_nopriv_addCustomer', 'addCustomer');
?>

【讨论】:

  • 是什么导致了问题?你改变了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 2013-03-31
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多