Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JQuery post method [duplicate]

Tags:

jquery

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


<form id="form" enctype="multipart/form-data">
<input type="file" id="image" name="image"/>

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?

like image 882
Ben Avatar asked Aug 08 '13 13:08

Ben


1 Answers

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

like image 108
Artek Wisniewski Avatar answered Nov 15 '22 01:11

Artek Wisniewski