Help me please. The code has an Ajax request that sends the data from the form to the server. The server receives data, the file changes, but echo does not send a response, but the script page opens and echo displays the data there.

Form Code:

<form enctype='multipart/form-data' id='change_form' method='post' action='ajax-for-saving.php'> <textarea id='filetext' name='filetext'>$content</textarea> <input type='hidden' name='filename' value='$pathToFile'> <input id='save_changes' type='submit' value='Сохранить' /> </form> 

Request Code:

 $('#change_form').on('submit', function(e) { e.stopPropagation(); e.preventDefault(); var changeForm = new FormData($('#change_form').get()[0]); $.ajax({ type: $(this).attr('method'), url: $(this).attr('action'), data: changeForm, contentType: false, processData: false, success: function(data) { alert(data); } }); }); 

Script code:

 <?php echo file_put_contents($_POST['filename'], $_POST['filetext']); 

Such a thing when you press a button: enter image description here

  • Not an expert, but try adding return false; at the end of the JS function return false; It should get the type of this: $('#change_form').on('submit', function(e) { ... return false; }); - Manitikyl
  • Your form sends a post to the server earlier than the script will work. Rewrite the script so that there is no post. Post implement in the script JS. The server did what you asked. He has nothing to do with (PHP). - MrMEX
  • @MrMEX You are mistaken, the listener goes first, then the form is sent. - Manitikyl
  • @MrMex, thank you very much. Wrote a direct link in the url, and removed from the form. Earned! - Love
  • The documentation site file_put_contents reads This function returns the number of bytes that were written to the file, or FALSE on failure. So it seems like everything is correct, and the file is recorded. Check the $pathToFile in the folder where ajax-for-saving.php is located. - Kosta B.

0