0

我在提交表单时使用了 ajaxfileupload。这也用于上传图像文件。图片上传成功,但是没有包含其他要保存到数据库的数据?因为它使我的表字段空白,所以我认为这些值没有传递给 category_save.php。我还观察到,当我使用$.ajax({})而不是$.ajaxFileUpload,数据都成功传递并保存在数据库中,包括图像文件名,但实际文件根本没有上传。但是当我使用$.ajaxFileUpload而不是时$.ajax({}),它只是反向工作,文件已上传,但值并未保存在数据库中。这有什么问题?这是我的代码:

product_form.php

<form method="post" name="new_category" id="product_category" enctype="multipart/form-data"> 
 <ul class="add_prod">
 <li>Category Title:<input type="text" name="category[title]" id="cat_title" value="" placeholder="Title" /> </li>
 <li>Category Description:<textarea rows="4" cols="40" name="category[description]"></textarea></li>
 <li>Category Image:<input type="file" name="image_file" id="image_file" /></li>
 </ul>  
 </form>

product1.js

$("#product_category").submit( function(){

  event.preventDefault();  
   var data_category = $(this).serialize();
   var image = $("#image_file").val();

$.ajaxFileUpload
    (
        {
            type:"post",
            url:"../wp-content/plugins/product_form/category_save.php",
            secureuri:false,
            fileElementId:'image_file',
            dataType: "json",
            data:data_category + "&image_file=" +image,                
            success: function (data)
            {
               if(data.notify == "Success"){
               console.log(data.notify);
              }
             else{
                return false;
              }
            }  
        }
    ); 
 });

product2.js

$("#product_category").submit( function(){
  event.preventDefault();  
   var data_category = $(this).serialize();
   var image = $("#image_file").val();

     $.ajax({
       type: "post",
       url: "../wp-content/plugins/product_form/category_save.php",
       dataType: "json",
       data:data_category + "&image_file=" +image,
       success: function(data){
           if(data.notify == "Success"){
               console.log(data.notify);
           }
           else{
               console.log(data.notify);
           }
       } 

   });

});

category_save.php

<?php 
 //establish connection
 $con = mysqli_connect("localhost","root","","ion2_emagi"); 
 //on connection failure, throw an error
 if(!$con) {  
  die('Could not connect: '.mysql_error()); 
  } 

 $output_dir = "C:/Users/Employees/Dropbox/emagi/wp-content/plugins/product_form/img/";
 $file_name = "image_file"; 

 if(isset($_FILES[$file_name]))
  {
  //Filter the file types , if you want.
  if ($_FILES[$file_name]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
 else
 {  
    //move the uploaded file to uploads folder;
   move_uploaded_file($_FILES["image_file"]["tmp_name"],$output_dir.     $_FILES["image_file"]["name"]);
 }

}

//get the form elements and store them in variables
$category_values = $_POST["category"]; 
$image_url = basename($_POST["image_file"]);
$image_field = "image_url";
$data = array();

//unset($view_all_info['Password2']);
  foreach($category_values as $field => $val){
 $data[] = "`".$field."` = '".$val."'";
 }

 array_push($data,"`".$image_field."` = '".$image_url."'");

 $sql = "INSERT INTO wp_product_category SET ".implode(',', $data);
 $ret = mysqli_query($con,$sql); 

if($ret){
$notification="Success";
}
 else{
 $notification="Failed";
 }

echo json_encode(array('notify'=>$notification));

mysqli_close($con);
4

1 回答 1

2

data字段应具有以下格式:

data: { data_category: data_category,  image_file: image_file }

您正在尝试将其作为带有参数的 URL 传递。

然后你必须在 PHP 中POST通过参数的名称来检索它。例如:

$category_values = $_POST["data_category"]; 
于 2013-09-26T09:39:46.287 回答