I am trying to set up an AJAX file upload script with jQuery and JSON.
I have some experience with AJAX and jQuery, but have never used JSON in my life.
While going through this tutorial, I found that their method of retrieving uploaded files was to get all the files from the database.
This does not suit my current needs. This will be part of a form for writing articles to the database, so the script needs to work only with attachments associated with the article being created.
I basically had to create a DB table for attachment sessions which I use to determine the attachments that are currently being worked with.
This is fine, however, I now need a way to pass an extra variable for the session id to the jQuery script.
I have encoded the JSON data in my PHP script, but now how do I access the certain variable in the jQuery script? In the code below, I will indicate where I encoded the JSON data, and also where I would like to add that data in the jQuery script.
PS: This is in CodeIgniter.
This is the PHP script
function upload_file()
{
$status = '';
$msg = '';
$config = array(
'upload_path' => FCPATH.'attachments',
'allowed_types' => 'jpg|gif|png',
'min_height' => ($this->session->flashdata('img_min_height')) ? $this->session->flashdata('img_min_height') : 0,
'min_width' => ($this->session->flashdata('img_min_width')) ? $this->session->flashdata('img_min_width') : 0,
'max_height' => ($this->session->flashdata('img_max_height')) ? $this->session->flashdata('img_max_height') : 0,
'max_width' => ($this->session->flashdata('img_max_width')) ? $this->session->flashdata('img_max_width') : 0,
'max_size' => 300,
'encrypt_name' => TRUE
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('user_files')){
$status = 'error';
$msg = $this->upload->display_errors('', '');
} else {
$data = $this->upload->data();
$this->attachment->create($data);
if($this->attachment->error == NULL){
$status = "success";
$msg = "File uploaded.";
$session_id = $this->attachment->_session_id;
} else {
unlink($data['full_path']);
$status = "error";
$msg = $this->attachment->error;
$session_id = '';
}
}
@unlink($_FILES['user_files']);
echo json_encode(array('status' => $status, 'msg' => $msg, 'session_id' => $session_id )); **//I added the session id variable here.**
}
This is the jQuery script that gets the uploaded files:
function refresh_files()
{
$.get('./upload/files/') //The session ID should go at the end of this URL...
.success(function (data){
$('#files').html(data);
});
}
You simply add an extra parameter next to the url.
Reference: http://api.jquery.com/jquery.get/
$.get('./upload/files/', required_data_here)
.success(function (data){
$('#files').html(data);
});
Then in your server-side script:
<?php
var sess_id = $_GET['your_variable_here'];
?>
Here's an example from the reference:
$.get( "test.php", { name: "John", time: "2pm" } );
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.