我似乎有一個奇怪的問題與jQuery和新創建的AJAX窗體/函數。我對AJAX很新,所以請耐心等待。這裏是jQuery的:jQuery Ajax表單不能使用選擇元素
jQuery('#wpuf_new_post_form').submit(function(e) {
e.preventDefault();
var data2 = jQuery('#wpuf_new_post_form').serialize();
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: data2 + '&action=savedata',
success: function(resp) {
if(!resp){ alert ('Failed!')}else{ alert(data2) }
}
});
});
上面的代碼需要提交我#wpuf_new_post_form
形式,序列化的數據,然後將它通過我的PHP函數:
add_action('wp_ajax_nopriv_savedata', 'my_ajax_save_form');
add_action('wp_ajax_savedata', 'my_ajax_save_form');
function my_ajax_save_form(){
$my_post = array(
'post_title' => $_POST['wpuf_post_title'],
'post_content' => $_POST['wpuf_post_content'],
'post_type' => $_POST['wpuf_post_type'],
'post_status' => 'publish',
);
wp_insert_post($my_post);
$this_post_ID = wp_insert_post($my_post);
add_post_meta($this_post_ID, 'cf_user-timestamp', $_POST['cf_user-timestamp']);
add_post_meta($this_post_ID, 'cf_project-id', $_POST['cf_project-id']);
add_post_meta($this_post_ID, 'cf_bin-id', $_POST['cf_bin-id']);
add_post_meta($this_post_ID, 'cf_status', $_POST['cf_status']);
add_post_meta($this_post_ID, 'cf_schedule-date', $_POST['cf_schedule-date']);
add_post_meta($this_post_ID, 'cf_reply-url', $_POST['cf_reply-url']);
add_post_meta($this_post_ID, 'cf_from-user', $_POST['cf_from-user']);
}
創建後和元如圖所示以上,通過AJAX提交,工作得很好。唯一拒絕工作的三個字段恰好是select
表單元素。
它們是:
- cf_project-ID
- cf_bin-ID
- cf_status
同樣,所有如上描述的其他職位元意見書,訪問數據庫就好了。只有select
表單元素值沒有。
是否有更好的方法來處理select
表單元素的序列化值?
PS:如果它有任何價值,下面是一個作爲jQuery變量data2
傳遞的序列化字符串的示例。正如你所看到的,格式不從任何其他提交的表單元素的不同:
_wpnonce=537e4539cc&_wp_http_referer=%2Fajax%2F&wpuf_post_title=1395254005136&wpuf_post_content=Testing&cf_user-timestamp=March+19th+2014%2C+11%3A33%3A25+am&cf_project-id=3797&cf_bin-id=3325&cf_status=-status-today&cf_schedule-date=03%2F21%2F2014&cf_reply-url=&cf_from-user=&wpuf_post_type=post&wpuf_post_new_submit=yes
你可以顯示HTML嗎? – Bene
你明白'return false'後的所有內容都不會被處理? – adeneo
@adeneo由於wp_insert_post()函數正常工作,似乎工作得很好,有或沒有返回false,但爲了簡單起見,我已將其刪除。問題依然存在。 –