0

I ran into a problem with AJAX and FromData due to lack of experience. I try to get all values from the dinamic fields (users can add new input fileds as many as they want) put them in different arrays, then put it in the FormData. After all I plan to send them using AJAX to another file where to put everything from FormData to data base with php.

So, please, give me a hint how to send that FormData via AJAX (do I need to serialize it or put in a JSON string) and how to get the values from it as $_POST[''].

Currently I did that code

            var mySubTasks = [];
                $('.subTasky').each(function(){
                  mySubTasks.push({
                    id: $(this).attr('id'),
                    title: $(this).children('.currentInput').val(),
                    comment: $(this).children().children('.currentInputCom').val(),
                    img: $(this).children('.taskPhoto').prop("files")[0]
                  });
                });
                
            var myHeaders = [];
                $('.subHeader').each(function(){
                  myHeaders.push({
                      header: $(this).children('.listInput').val()
                  });
                });
                
            var mySubSub = [];
                $('.subsub').each(function(){
                  mySubSub.push({
                      id: $(this).attr('id'),
                      title: $(this).children('.currentInput').val(),
                      comment: $(this).children().children('.currentInputCom').val()
                  });
                });
                
            var myPrime = [];
                $('#mainTaskBox').each(function(){
                  mySubSub.push({
                      title: $(this).children('.listPrTitle').val(),
                      comment: $(this).children().children('.listPrCom').val(),
                      img: $(this).children().children('.taskPhoto').prop("files")[0]
                  });
                });
            
            var formData = new FormData();
            formData.append('mySubTasks', mySubTasks);
            formData.append('myHeaders', myHeaders);
            formData.append('mySubSub', mySubSub);
            formData.append('myPrime', myPrime);
            
            console.log(formData);

What should I do next.

Vlad Zaev
  • 23
  • 5
  • 1
    Why are you using JS FormData() when all the rest of your code uses jQuery and this library offers serialize and serializeArray methods? – Mister Jojo Aug 21 '20 at 21:15
  • Does this answer your question? [How to use FormData for AJAX file upload?](https://stackoverflow.com/questions/21044798/how-to-use-formdata-for-ajax-file-upload) – JTC Aug 21 '20 at 21:18
  • I'm not that good in programming. Still tring to figure out how to do it in a good way. – Vlad Zaev Aug 21 '20 at 21:19
  • @JTC not really. It doesn't show how to use arras in it and get info from. – Vlad Zaev Aug 21 '20 at 21:21
  • 1
    to inspect you formData, you need to do `formData.entries()` inside a loop – S.LT Aug 21 '20 at 21:24

0 Answers0