Home > Software engineering >  sending multiple data ajax
sending multiple data ajax

Time:11-27

I want to send the data which is in the variable "blog_id" to the page "blog_img_upload.php" along with the "form_data" Please help. The codes are in below -


var blog_id = "<?php echo"$blog_id"?>";
let form_data = new FormData();
let img = $("#myImage")[0].files;
 
          if(img.length > 0){
          form_data.append('my_image', img[0]);
          $.ajax({
            url: 'blog_img_upload.php',
            type: 'post',
            data: form_data,
            //data:{ form_data, blog_id: blog_id }, // can I do like this ?
            contentType: false,
                processData: false,
                success: function(res){
                  const data = JSON.parse(res);
                  if (data.error != 1) {
                       let path = "../ui/images/blog_images/" data.src;
                       $("#preImg").attr("src", path);
                       $("#preImg").fadeOut(1).fadeIn(1000);
                       $("#myImage").val('');
                  }else {
                    $("#errorMs").text(data.em);
                  }
                  $('#img_main_show_div').load(location.href   " #img_main_show_div");
                }
          });
         
        }


CodePudding user response:

form_data.append('my_image', img[0]);
form_data.append('blog_id', '<?php echo blog_id; ?>');
.
.
.
//ajax 
data: form_data
  • Related