0

I have a form, in that, I have various fields including an image. Basically, I am trying to submit the product image including other fields and I used AJAX to submit the form. But I am facing an issue doing that. I do not know where I am doing wrong. Please if any suggestion, would be appreciated.

<form id="addproductform" name="addproductform" enctype="multipart/form-data">
<input type="button" class="btn btn-outline-danger mt-4" value="Submit" onclick="addproduct()">
    
    function addproduct(){
            var productname = document.getElementById('productname').value;
            var category = document.getElementById('categorydd').value;
            var subcategory = document.getElementById('subcategorydd').value;
            var tag = document.getElementById('tagsdropdown').value;
            var skucode = document.getElementById('skucode').value;
            var productimg = document.getElementById('imagepath').value;
            var url = "../api/products";
            console.log(url);
            $.post(url,{
                productname : productname,
                category : category,
                subcategory : subcategory,
                tag : tag,
                skucode : skucode,
                productimg : productimg,
            },function(data, status) {
                if (data.status == "OK") {
                    if (data.statusCode == 1) { 
                        $('#addproductform').submit();
                    }else {
                        var error = data.responseMessage;
                        swal(error, "", "error");
                    }
                } else {
                    var error = data.responseMessage;
                    swal(error, "", "error");
                }
            });
        }
Rakhi Sharma
  • 111
  • 1
  • 2
  • 18
  • So, what issue are you facing exactly? edit: nevermind. Instead of getting all that manually, try `const data = new FormData($('#addproductform')[0]);`. Now send that as shown here: https://stackoverflow.com/a/5976031/5734311 –  Apr 22 '21 at 06:28
  • When I was submitting excluding the image field, then it was working. But after passing the image field, the form did not submit. – Rakhi Sharma Apr 22 '21 at 06:30

0 Answers0