Monday, 2 May 2016

How to use JQuery post method




$.post('image.php', {
image:form.image.value
}







PHP->isset($_FILES['file'])



How to use $.post() to post $_FILES inside of form tag?
Do I still need include enctype
or do I need use AJAX, and how can I do that?


Answer



Use jQuery form plugin to AJAX submit the form with files.
http://malsup.com/jquery/form/



In JS




$('#form').ajaxSubmit({
success: function(response) {
console.log(response);
}
});


in PHP




// after doing upload work etc

header('Content-type: text/json');
echo json_encode(['message' => 'Some message or param whatever']);
die();


Full docs and examples: http://malsup.com/jquery/form/#ajaxSubmit


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...