Upload an image with AJAX
To begin, little reminder about sending a form (without upload) AJAX with jQuery.
Nothing less than a form for sending in AJAX.
Here in this example will be about an upload image, but can be adapted to any other type of file. Here is the code that supports the upload of an image, in addition to the rest of the form.
enctype added = "multipart / form-data" to the form is mandatory if you want to do a file upload. We also add our field that allows the user to select a file on his computer. It states that only the images are accepted.
The upload AJAX is related in fact to support the browser FormData API. This is supported by the major browsers
finaly PHP :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="my_form" method="post" action="process_form.php"> | |
<input type="text" name="title"> | |
<textarea name="content"></textarea> | |
<button type="submit">OK</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
$('#my_form').on('submit', function (e) { | |
// prevent browser from submitting the form | |
e.preventDefault(); | |
var $form = $(this); | |
$.ajax({ | |
url: $form.attr('action'), | |
type: $form.attr('method'), | |
data: $form.serialize(), | |
success: function (response) { | |
//server responce | |
} | |
}); | |
}); | |
}); |
Here in this example will be about an upload image, but can be adapted to any other type of file. Here is the code that supports the upload of an image, in addition to the rest of the form.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="my_form" method="post" action="upload.php" enctype="multipart/form-data"> | |
<input type="text" name="title"> | |
<textarea name="content"></textarea> | |
<input type="file" name="image" accept="image/*"> | |
<button type="submit">OK</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
$('#my_form').on('submit', function (e) { | |
// prevent browser from submitting the form | |
e.preventDefault(); | |
var $form = $(this); | |
var formdata = (window.FormData) ? new FormData($form[0]) : null; | |
var data = (formdata !== null) ? formdata : $form.serialize(); | |
$.ajax({ | |
url: $form.attr('action'), | |
type: $form.attr('method'), | |
contentType: false, // mandatory for the upload | |
processData: false, // mandatory for the upload | |
dataType: 'json', | |
data: data, | |
success: function (response) { | |
// server responce | |
} | |
}); | |
}); | |
}); |
The upload AJAX is related in fact to support the browser FormData API. This is supported by the major browsers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data['file'] = $_FILES; | |
$data['text'] = $_POST; | |
echo json_encode($data); | |
?> |
You can use my other tutoriel to handle this php file .
Comments
Post a Comment