我正在尝试用ajax做一个简单的表单提交(这样页面就不会刷新)
functions.php
add_action(\'wp_ajax_send_projectmessage\', \'send_projectmessage\');
function send_projectmessage($projectid, $userid, $message) {
global $wpdb;
$wpdb->insert(\'tbl_messages\', array(
\'project_id\' => $projectid,
\'user_id\' => $userid,
\'message_body\' => $message
));
echo \'success?\'; //this shows up in console.log but 3 parameters are empty
wp_die();
}
js:
$(\'#form-pm\').on(\'submit\',function(e) {
e.preventDefault();
//hardcode stuff for testing
var testdata = {
\'action\': \'send_projectmessage\',
\'projectid\': \'71\',
\'userid\': \'1\',
\'message\': \'voila\'
};
var ajaxRequest =
$.ajax({
url: admin_ajax.ajax_url,
type: \'post\',
data: testdata
});
ajaxRequest.done(function(data) { console.log(data); });
ajaxRequest.fail(function(jqXHR) { alert(\'You are fail. \' + jqXHR); });
});
管理ajax。php连接正确。我的错误。日志显示
send_projectmessage
正在调用函数,但参数为空。我错过什么了吗?从昨天起我就一直在拉扯头发。
error.log
PHP Warning: Missing argument 2 for send_projectmessage(), called in /var/www/html/wp-includes/class-wp-hook.php on line 286 and defined in /var/www/html/wp-content/themes/mytheme_child/functions.php on line 178, referer: https://xxxxx
PHP Warning: Missing argument 3 for send_projectmessage(), called in /var/www/html/wp-includes/class-wp-hook.php on line 286 and defined in /var/www/html/wp-content/themes/mytheme_child/functions.php on line 178, referer: https://xxxxx
WordPress database error Column \'user_id\' cannot be null for query INSERT INTO `tbl_messages` (`project_id`, `user_id`, `message_body`) VALUES (\'\', NULL, NULL) made by do_action(\'wp_ajax_send_projectmessage\'), WP_Hook->do_action, WP_Hook->apply_filters, send_projectmessage, referer: https://xxxxx/