我是jquery/ajax的新手,經過兩天的搜索,我終於尋求幫助。顯然有一些基本的東西我不理解。.ajax報告成功,但數據庫插入失敗
下面是.ajax的代碼:
$.ajax({
url: "../ajax/create_employee.php",
type: 'json',
async: false,
data: $serialFormData,
// callback handler that will be called on success
success: function(response, textStatus, jqXHR){
這裏是PHP文件(create_employee.php)被調用:
<?php
require ("../models/m_employee.php");
$myemployee = new m_employee();
$response = $myemployee->create_new_employee($_POST);
//echo " php file response = " . $response;
return $response;
?>
這裏是目標代碼:
}
$q = "insert into employee(employee_num,employee_fname,employee_lname,employee_position,employee_start_time,employee_stop_time)";
$q .= "values ('$num','$fname','$lname','$position','$start_time','$stop_time')";
if(!$r = mysqli_query($dbc,$q)) {
$data['error'] = "true";
$data['message'] = "insert failed";
$data['success'] = "false";
return json_encode($data);
}
返回到調用文件的數據是正確的,但它似乎沒有進入'成功'功能。返回到.ajax調用的數據應該來自我的create_employee.php文件還是僅僅是服務器報告文件執行?
使用'echo'而不是返回將數據返回給您的ajax成功函數 – peacemaker