Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jquery'将文本输入与图像文件输入一起添加;用PHP实现ajax函数?_Php_Jquery_Ajax - Fatal编程技术网

如何使用jquery'将文本输入与图像文件输入一起添加;用PHP实现ajax函数?

如何使用jquery'将文本输入与图像文件输入一起添加;用PHP实现ajax函数?,php,jquery,ajax,Php,Jquery,Ajax,我有一个在DB中插入图像文件和文本输入的问题,我一直得到这个错误 **Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'. at HTMLButtonElement.<anonymous> (product-add.js:5) at HTMLButtonElement.dispatch (jquery.min.js:2)

我有一个在DB中插入图像文件和文本输入的问题,我一直得到这个错误

 **Uncaught TypeError: Failed to construct 'FormData': parameter 1 is not of type 'HTMLFormElement'.
     at HTMLButtonElement.<anonymous> (product-add.js:5)
     at HTMLButtonElement.dispatch (jquery.min.js:2)
     at HTMLButtonElement.v.handle (jquery.min.js:2)**
这是表格,评级数据和类别数据来自我数据库中的不同表格,此表格仅捕获产品名称、价格、产品图像和描述

         $(document).ready(function(){
         $("#submit-btn").on('click', function(e){
            e.preventDefault();

           var formData = new FormData(this);
           formData.append("file", $("#image")[0].files[0]);
           formData.append("name", $("#product").val());
           formData.append("price", $("#price").val());
           formData.append("category", $("#category").val());
           formData.append("rating", $("#rating").val());
           formData.append("description", $("#description").val());

           $.ajax({
            url: "controller/Product-add.php",
            type: "POST",
            data: formData,
            cache: false,           
            contentType: false,
            processdata: false,
            success: function(responce){
               if(responce.trim() == "image-file-error"){
                  alert('image file error');
                   }
                    else if(responce.trim() == "image-upload-failed"){
                    alert('image upload error');
                    }
                    else{
                    $("#add-new-product").modal("hdie");
                    location.reload();
                    alert('image uploaded');
                }
             },

          });

        });

      });
<form method="POST" enctype="multipart/form-data" id="add-product-form">                 
                <div class="form-group row">                  
                    <input type="text" name="name"id="product">          
                </div>
                <div class="form-group row">                                         
                    <input type="text" name="price" id="price" >
                </div>
                </div>
                <div class="form-group row">                  
                    <select type="text"name="category" id="category" >
                      <?php if(isset($allCategory)) {
                        foreach($allCategory as $key => $value){
                            ?>                             
                          <option value="<?php echo $allCategory[$key] 
              ["id"] ?>"><?php echo $allCategory[$key]["name"] ?></option>                              
                        <?php
                        }
                      }?>
                    </select>
                  </div>
                </div>
                <div class="form-group row">
                    <input type="file" name="image" id="image">
                    <label class="custom-file-label">Choose file</label>
                  </div>
                </div>
                <div class="form-group row">                      
                        <select type="text" name="rating" id="rating">
                          <?php if(isset($allRating)) {
                            foreach($allRating as $key => $value){
                                ?>                             
                              <option value="<?php echo $allRating[$key] 
        ["id"] ?>"><?php echo $allRating[$key]["actual_value"] ?></option>                              
                            <?php
                            }
                          }?>
                        </select>
                    </div>
                </div>
                <div class="form-group row">
                    <textarea class="form-control" rows="3" 
   id="description" name="description"></textarea>
                  </div>
                </div> 
              </div>
                <button class="btn btn-danger float-right" style="color:  
        type="submit" id="submit-btn">Submit</button>
            </form>

var formData=new formData();//当前为空应该可以正常工作。您的“this”范围不是表单元素。