javascript函数响应为空

时间:2013-09-20 05:44:14

标签: javascript jquery ajax response

以下是在数据库中添加数据的代码。但它会是其他部分。 回复是空的。我通过警报检查了回复。但它是空的。

    //add donation by ajaxs
function ccjkfoundation_adddonation()
{
    var dateobj  = jQuery('#donation_date');
    var nameobj  = jQuery('#donation_name');
    var desobj   = jQuery('#donation_des');
    var assetobj = jQuery('#donation_asset');
    var data = {
            action: 'ccjkfoundation_adddonation', 
            date:  dateobj.val(),
            name:  nameobj.val(),
            des:   desobj.val(),
            asset: assetobj.val()
        };
    jQuery.post(ajaxurl, data, function(response){
    alert(response);
        if(response)
        {
            var retstr = eval("("+response+")");
            jQuery('#donation-con').prepend(retstr['list']);
            jQuery('#other_donation_asset').text('¥'+retstr['ototalasset']).attr('title',retstr['ototalasset']);
            dateobj.val('');
            nameobj.val('');
            desobj.val('');
            assetobj.val('');
            ccjkfoundation_total_asset();
        }
        else
        {

            alert('Failure');
        }
    });
}

这是php函数。请检查一下,如果我需要更改数据库表可能是。

    function ccjkfoundation_adddonation_callback()
{
    ob_clean();
    $date  = trim($_POST['date']);
    $name  = trim($_POST['name']);
    $des   = trim($_POST['des']);
    $asset = trim($_POST['asset']);
    $ototalasset = get_option('ccjk_other_donation');
    if(empty($name) || empty($asset) || !is_numeric($asset))
        exit('');
    global $wpdb;
    $result = $wpdb->insert( DONATION_TABLE, array(
            'name' => stripslashes($name),
            'des' => stripslashes($des),
            'asset' => $asset,
            'date' => stripslashes($date)
            ));
    if(!$result)
        exit('');
    $donation_id = (int) $wpdb->insert_id;
    $ototalasset+=$asset;
    $return_json = array('ototalasset'=>number_format($ototalasset));
    update_option('ccjk_other_donation', $ototalasset);
    $liststr = '
        <tr id="donation_'.$donation_id.'">
            <td><input type="text" name="donation_date_'.$donation_id.'" id="donation_date_'.$donation_id.'"  class="form-required form-input-tip" placeholder="Date of Donation" aria-required="true" value="'.$date.'"></td>
            <td><input type="text" name="donation_name_'.$donation_id.'" id="donation_name_'.$donation_id.'"  class="form-required form-input-tip" placeholder="Donor Name" aria-required="true" value="'.$name.'"></td>
            <td><input type="text" name="donation_des_'.$donation_id.'" id="donation_des_'.$donation_id.'"   class="form-input-tip" placeholder="Donor Description" value="'.$des.'"></td>
            <td><input type="text" name="donation_asset_'.$donation_id.'" id="donation_asset_'.$donation_id.'" class="form-required form-input-tip" placeholder="0" aria-required="true" value="'.$asset.'"></td>
            <td><input type="button" class="button" value="Update" onclick="ccjkfoundation_updatedonation('.$donation_id.')"> <input type="button" class="button" value="Del" onclick="ccjkfoundation_deldonation('.$donation_id.');"></td>
        </tr>                       
        ';  
    $return_json['list'] = $liststr;
    echo json_encode($return_json);
    die(); // this is required to return a proper result 这里是必须的
}

1 个答案:

答案 0 :(得分:0)

我相信你使用的是WordPress,$ wpdb-&gt; insert()有问题。 “insert”方法中缺少“format”参数。这是它应该如何:

$result = $wpdb->insert( DONATION_TABLE,
                                array(
                                        'name' => stripslashes($name),
                                        'des' => stripslashes($des),
                                        'asset' => $asset,
                                        'date' => stripslashes($date)
                                ), 
                                array(
                                        '%s',
                                        '%s',
                                        '%s',
                                        '%s'
                                )
);

您应该可以根据需要调整“格式”标识符,例如%s表示字符串,%d表示整数等。请参阅http://codex.wordpress.org/Class_Reference/wpdb#INSERT_rows