P
P
Petro Boyko2021-07-21 22:09:54
AJAX
Petro Boyko, 2021-07-21 22:09:54

Why is the form not being submitted via ajax, the image is not being submitted?

The essence of the question is that the image is not transmitted through the form, the text is transmitted

<form id="myform"  action='' method='post' enctype="multipart/form-data">	
      <input type="hidden" name="action" value="do">
      
    <input type="text"  name="text"/>
    
        <input type="file" name="file"  />

    <input type="submit" id="btncontcall" value=" Ok "/>		

      </form>

$(document).ready(function() {
         $('#btncontcall').click(function(){ 
              $.ajax({
                 type: "POST",
                 url:"bot_send.php", // Адрес обработчика
                 data:$('#myform').serialize(),
                 error:function(){$("#erconts").html("<span class='erconts'>Ошибка!</span>");},
         		beforeSend: function() {
                     $("#erconts").html("<span class='erconts'>Отправляем...</span>");
                 },
         		success: function(result){
          $('#myform')[0].reset();
         			$('#erconts').html(result);
         			checkThis();
         		}
             });
             return false;
         });
    });

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
inkShio, 2021-07-21
@inkShio

The .serialize() method returns a string suitable for passing through a URL string. Data can be collected from many jQuery objects, including input, textarea, and select. Google FormData

N
Nadim Zakirov, 2021-07-22
@zkrvndm

Here is this construct in the code: Replace with: And then everything will work.
$('#myform').serialize()
new FormData($('#myform')[0])

P
Petro Boyko, 2021-07-22
@boykopetro

That's how it worked

(function ($) {
$("#myform").submit(function (event) {
  event.preventDefault();
  $.ajax({
    url: "/bot_send.php",
    type: "POST",
    data: new FormData($('#' + $(this).attr('id'))[0]),
    processData: false,
    contentType: false,
                 error:function(){$("#erconts").html("<b style='color: red;'>Сталася помилка!</b>");},
         		beforeSend: function() {
                     $("#erconts").html("<b>Відправляєм дані...</b>");
                 },	
         		success: function(result){
          $('#myform')[0].reset();
         			$('#erconts').html(result);
         			checkThis();
         		},
      });

});
  
}(jQuery));

       $("#answer").keyup(function(event){
    if(event.keyCode == 13){
        $("#btncontcall").click();
       } });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question