Wordpress + Ajax更新记录无法正常工作

时间:2014-05-19 09:38:28

标签: javascript php jquery ajax wordpress

我在使用Ajax更新记录时遇到了麻烦。

HTML& JS

<div class="row">
    <form role="form" action="" type="post" id="abcdform">
        <div class="col-md-3">

            <div class="form-group">
                <label for="date">Date</label>
                <input type="date" class="form-control" id="date" name="date" placeholder="mm/dd/yyy" tabindex="1" value="<?php echo $dr->date; ?>">
            </div>
            <div class="form-group">
                <label for="chats_init_customer">Chat Initiated by Customer</label>
                <input type="number" class="form-control" id="chats_init_customer" name="chats_init_customer" placeholder="eg:5" tabindex="2" value="<?php echo $dr->chats_init_customer; ?>">
            </div>
            <div class="form-group">
                <label for="emails_in">Emails Incomming</label>
                <input type="number" class="form-control" id="emails_in" name="emails_in" placeholder="eg:5" tabindex="3" value="<?php echo $dr->emails_in; ?>">
            </div> 
        </div>

        <div class="col-md-3">
            <div class="form-group">
                <label for="emails_out">Emails Outgoing</label>
                <input type="number" class="form-control" id="emails_out" name="emails_out" placeholder="eg:5" tabindex="4">
            </div>
            <div class="form-group">
                <label for="tickets_open">Tickets at Opening</label>
                <input type="number" class="form-control" id="tickets_open" name="tickets_open" placeholder="eg:5" tabindex="5">
            </div>                    
            <div class="form-group">
                <label for="tickets_closed">Tickets Closed</label>
                <input type="number" class="form-control" id="tickets_closed" name="tickets_closed" placeholder="eg:5" tabindex="6">
            </div>                    
        </div>

        <div class="col-md-3">
            <div class="form-group">
                <label for="tickets_remain">Tickets Remaining Open</label>
                <input type="number" class="form-control" id="tickets_remain" name="tickets_remain" placeholder="eg:5" tabindex="7">
            </div>
            <div class="form-group">
                <label for="calls_taken">Calls Taken</label>
                <input type="number" class="form-control" id="calls_taken" name="calls_taken" placeholder="eg:5" tabindex="8">
            </div>
            <div class="form-group">
                <label for="calls_abandon">Calls Abandon</label>
                <input type="number" class="form-control" id="calls_abandon" name="calls_abandon" placeholder="eg:5" tabindex="9">
            </div>


        </div>

        <div class="col-md-3">

            <div class="form-group">
                <label for="calls_avg_length">Calls Average Length</label>
                <input type="number" class="form-control" id="calls_avg_length" name="calls_avg_length" placeholder="eg:5" tabindex="10">
            </div>
            <div class="form-group">
                <label for="chats_total">Total Chats</label>
                <input type="number" class="form-control" id="chats_total" name="chats_total" placeholder="eg:5" tabindex="11">
            </div>
            <div class="form-group">
                <label for="sales">Sales</label>
                <input type="number" class="form-control" id="sales" name="sales" placeholder="eg:5" tabindex="12">
            </div>

            <div class="feedback hidden-md hidden-lg"></div>                    

            <input type="hidden" name="action" value="edit_daily_report">
            <input type="submit" value="Update" id="abcdsubmit" class="btn btn-primary pull-right" tabindex="13">
            <div id="process" class="loader pull-right"><img src="<?php echo abcd_root_url('css/images/loader.gif');?>" alt="loader" /></div>

        </div>

    </form>

</div>



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

    function ajaxSubmit() {

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

        jQuery('#abcdsubmit').attr('disabled', true);
        jQuery('#process').show();
        jQuery.ajax({
            type: "POST",
            url: "<?php echo site_url(); ?>/wp-admin/admin-ajax.php",
            data: abcdform,
            success: function(data) {
                jQuery(".feedback").html('<span class="glyphicon glyphicon-saved"></span> ' + data).addClass('alert alert-success');
                jQuery('#process').hide();
                jQuery('#abcdsubmit').attr('disabled', false);
            }

        });

        return false;
    }
</script>

功能

function edit_daily_report()
{
    global $wpdb;
    $date = $_POST['date'];

    $conditionValue = array('id' => 19);

    if ($wpdb->update('hf_abcd_daily_report', array('date' => $date), array('id' => 19)) === FALSE){

        echo "Error";
    } else {
        echo "Record successfully added to the database. Record id is <strong>$wpdb->insert_id</strong>";
    }
    die();
}

当我点击Update按钮时,表单提交后它什么都不做。请忽略表单中的所有字段,并仅考虑日期字段。

1 个答案:

答案 0 :(得分:1)

你应该使用ajax动作来使用wordpress

add_action( 'wp_ajax_my_action', 'edit_daily_report' );
add_action( 'wp_ajax_nopriv_my_action', 'edit_daily_report' );