9
$.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?

phuzi
  • 12,078
  • 3
  • 26
  • 50
Ben
  • 2,562
  • 8
  • 37
  • 62

2 Answers2

7

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

Artek Wisniewski
  • 797
  • 5
  • 13
0

Just submit the form!

$('form#form').submit();

For AJAX Upload read this answer

Community
  • 1
  • 1
Fabian Horlacher
  • 1,899
  • 1
  • 24
  • 31