How to use jQuery post method

$.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 send $_FILES inside a form tag?
Should I still include enctype or do I need to use AJAX, and how can I do this?

+6
source share
2 answers

Use the jQuery form plugin for AJAX to 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 documents and examples: http://malsup.com/jquery/form/#ajaxSubmit

+4
source

Just submit a form!

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

To download AJAX read this answer

0
source

Source: https://habr.com/ru/post/951317/


All Articles