Good day! There is some form that conveys a message. She transmitted to server.php with the following code:

function sendAjaxForm(infoBox, addUser, url) { jQuery.ajax({ url: 'server.php', type: "POST", dataType: "html", data: jQuery("#"+addUser).serialize(), success: function(response) { result = jQuery.parseJSON(response); document.getElementById(infoBox).innerHTML += "<tr><td><b>"+result.name+"</b></td></tr><tr><td>"+result.msg+"</td></tr>";// ИСПРАВИТЬ }, error: function(response) { // Данные не отправлены document.getElementById(infoBox).innerHTML = "Ошибка. Данные не отправленны."; } }); } 

Now I wanted to attach files to the message via

 <input type='file' name='msgFile'> 

Question: how to change the script (1) so that the file will also fly to server.php and then be downloaded via FTP?

    2 answers 2

     <form action="action" method="post" class="cl_form" enctype="multipart/form-data"> jQuery('.cl_form').submit(function(e) { //e.preventDefault(); var formData = new FormData($(this).get(0)); jQuery.ajax({ [...] data:formData, cache:false, processData: false, contentType: false, [...] }); return false; }); 
    • Do I understand correctly that this script will reload the page anyway? - AtCliff_
    • add e.preventDefault(); before var formData = ... - programmer403
    • Thanks, it works, but unfortunately it still opens the URL from the url: 'server.php' - AtCliff_
    • at the end of .submit(function(e){......}) (in the inner function, of course) add return false; - Vladimir Novopashin

    Can so

    It is possible also so , but it through XMLHttpRequest.