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?

6
  • sorry, it's impossible - safety reasons
    – picios
    Commented Aug 8, 2013 at 13:30
  • 1
    @picios It's not impossible, anymore. But, is limited to modern browsers. Commented Aug 8, 2013 at 13:31
  • @Jonathan Lonowski yes, but if you're developer, you don't want to do such things
    – picios
    Commented Aug 8, 2013 at 13:33
  • i have email and password need to check and post back, hmm... this is going to be hard
    – Ben
    Commented Aug 8, 2013 at 13:33
  • @BenWong Don't be discouraged! We will help you! Commented Aug 8, 2013 at 13:34

2 Answers 2

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

3
  • if i use plug in, how to post message back from php?
    – Ben
    Commented Aug 8, 2013 at 13:39
  • 1
    It's treated as a normal AJAX call - I'll edit the answer to show you how Commented Aug 8, 2013 at 13:40
  • thx a lot! let me try it
    – Ben
    Commented Aug 8, 2013 at 13:45
0

Just submit the form!

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

For AJAX Upload read this answer

2
  • he's talking about ajax post
    – picios
    Commented Aug 8, 2013 at 13:37
  • Maybe after changing his question. Thanks for downvote. Commented Aug 8, 2013 at 13:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.