在我的wordpress网站上,在提交表单之前,我正在保存数据库数据。之后,我需要提交页面表单。 的 JS:
$('#submit').bind('click',function(e){
var cm_id = $('#campaign_id').val();
var cm_name = $('#campaign_name').val();
var cm_description = tinymce.activeEditor.getContent();
var cm_background = $('#upload_image').val();
e.preventDefault();
var form = $('#campaign_form');
if (cm_id == ''){
data = {
action: 'cm_save',
aj_name: cm_name,
aj_description: cm_description,
aj_background: cm_background,
};
}
else{
data = {
action: 'cm_update',
aj_id: cm_id,
aj_name: cm_name,
aj_description: cm_description,
aj_background: cm_background,
};
}
$.post(ajaxurl, data).done(function(response){
console.log(response);
$(form).submit();
}).error(function(response){
console.log(response);
});
});
HTML:
<form id="campaign_form" action="options.php" method="post" enctype="multipart/form-data">
<?php settings_fields('campaign_options'); ?>
<?php //wp_nonce_field('update-options');?>
<table class="form-table">
<tr valign="top">
<th scope="row">Enable Campaign Mode:</th>
<td><input type="checkbox" id="campaign_enable" name="campaign_enable"
value="1" <?php checked('1', get_option('campaign_enable')); ?> /></td>
</tr>
<tr valign="top">
<th scope="row">Campaign Type:</th>
<td><label><input type="radio" id="campaign_type" name="campaign_type" <?php if(get_option('campaign_type') == 'block') echo 'checked="checked"'; ?> value="block" />Layer with block</label>
<label><input type="radio" id="campaign_type" name="campaign_type" <?php if(get_option('campaign_type') == 'layer') echo 'checked="checked"'; ?> value="layer" />Only layer</label></td>
</tr>
<tr valign="top" id="campaign_days_row" <?php if(get_option('campaign_type') != 'layer') echo 'style="display: none;"'; ?> >
<th scope="row">Show layer again after(d.):</th>
<td><input type="text" size="5" id="campaign_days" name="campaign_days" value="<?php echo get_option('campaign_days'); ?>"" /> days</td>
</tr>
<tr valign="top">
<th scope="row">Success message:</th>
<td><input type="text" id="campaign_success_message" name="campaign_success_message" size="50"
value="<?php echo get_option('campaign_success_message'); ?>"/></td>
</tr>
<tr valign="top" style="border: 1px solid #D1CCCC">
<th style="padding: 0"></th><td style="padding: 0"></td>
</tr>
<tr valign="top">
<th scope="row">Select Campaign:</th>
<td>
<select id="campaign_list" name="campaign_list" value="<?php esc_attr_e( get_option('campaign_id') ); ?>" style="width: 50%;">
<option value="new" style="background: #ECECEC" <?php selected( '' == get_option('campaign_id') ); ?>>New Campaign</option>
<?php
if ( $campaign_list ):
foreach ($campaign_list as $_item): ?>
<option value="<?php echo $_item['id'] ?>" <?php selected( $_item['id'] == get_option('campaign_id') ); ?>><?php echo $_item['name'] ?></option>
<?php endforeach;
endif; ?>
</select>
<img src="<?php echo admin_url('/images/wpspin_light.gif'); ?>" class="waiting" id="cm_loading" style="display: none;" />
<p><input type="submit" class="button-secondary" value="Remove from list" id="cm_remove" style="display: none" /></p>
</td>
</tr>
<input type="hidden" id="campaign_id" name="campaign_id" value="<?php echo get_option('campaign_id') ?>">
<tr valign="top" class="campaign-row">
<th scope="row">Campaign Name:</th>
<td><input type="text" id="campaign_name" name="campaign_name"
value="<?php echo get_option('campaign_name'); ?>"/></td>
</tr>
<tr valign="top" class="campaign-row">
<th scope="row">Background:</th>
<td><label for="upload_image">
<input id="upload_image" type="text" size="36" name="campaign_background"
value="<?php echo get_option('campaign_background'); ?>"/>
<input id="upload_image_button" class="button" type="button" value="Upload Image"/>
<br/>Enter an URL or upload an image for the campaign background.
</label><br>
<img id="upload_image_path" src="<?php echo get_option('campaign_background'); ?>" height="75">
</td>
</tr>
<?php
$settings = array(
'teeny' => true,
'textarea_rows' => 15,
'tabindex' => 1
); ?>
<tr valign="top" class="campaign-row">
<th scope="row">Campaign Description:</th>
<td><?php wp_editor((get_option('campaign_description')) , 'campaign_description', $settings); ?></td>
</tr>
</table>
<input type="hidden" name="action" value="update"/>
<input type="hidden" name="page_options" value="new_option_name,some_other_option,option_etc"/>
<?php submit_button(); ?>
</form>
PHP:
function cm_save_ajax(){
global $wpdb;
if (isset($_POST['aj_name']) && !empty($_POST['aj_name'])){
$name = $_POST['aj_name'];
$description = $_POST['aj_description'];
$background = $_POST['aj_background'];
$wpdb->insert('wp_campaigns',
array(
'name' => $name,
'description' => $description,
'background' => $background
),
array(
'%s', '%s', '%s'
)
);
$lastid = $wpdb->insert_id;
update_option('campaign_id', $lastid );
echo $lastid;
//echo $wpdb->show_errors();
}
//else echo "Not all data is completed";
die();
} add_action('wp_ajax_cm_save','cm_save_ajax');
我的Ajax请求成功,但表示没有提交。可能是什么情况?