我想在我的網站中使用ajaxFIleUpload。我沒有收到JavaScript發送給PHP的JSON數據。我被困在這一點上。文件上傳是好的,但我無法接收任何後期值。我jQuery的功能是PHP是不是從jQuery接收JSON數據
$(function() {
$(document).on("submit", "#upload_file", function(e) {
e.preventDefault();
$.ajaxFileUpload({
url :base_url + 'payments/uploadPaymentSlip/',
secureuri :false,
fileElementId :'userfile',
type : 'POST',
data: { paymentFormInputAmount: 'asdasd' },
success : function (data, status)
{
if(data.status != 'error')
{
$('#files').html('<p>Reloading files...</p>');
//refresh_files();
$('#files').val('');
}
alert(data.msg);
},
dataType: 'json'
});
});
});
我的PHP函數是
function uploadPaymentSlip() {
$status = "";
$msg = "";
$file_element_name = 'userfile';
$status = "error";
// checking whether json value shows in php or not
// $_POST['paymentFormInputAmount'] is also not working
$msg = $_POST['paymentFormInputAmount'];
if ($status != "error") {
$config['upload_path'] = realpath(APPPATH . '../uploads/paymentSlip');
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload($file_element_name)) {
$status = 'error';
$msg = $this->upload->display_errors('', '');
}
else {
$data = $this->upload->data();
// $file_id = $this->files_model->insert_file($data['file_name'], $_POST['title']);
if($file_id) {
$status = "success";
$msg = "File successfully uploaded";
}
else {
unlink($data['full_path']);
$status = "error";
$msg = "Something went wrong when saving the file, please try again.";
}
}
@unlink($_FILES[$file_element_name]);
}
echo json_encode(array('status' => $status, 'msg' => $msg));
}
你是如何調用你的PHP函數'uploadPaymentSlip'? – putvande
url:base_url +'payments/uploadPaymentSlip /',這是部分調用PHP函數。我認爲調用PHP函數是正確的,因爲除了獲取json值之外,上傳是成功的。 – atomaprchya