$ .ajaxFileUpload成功将文件上传到文件夹但无法传递要保存在数据库中的值

时间:2013-09-26 09:37:09

标签: javascript php jquery ajax

我在表单提交表单时使用了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);

1 个答案:

答案 0 :(得分:2)

data字段应具有以下格式:

data: { data_category: data_category,  image_file: image_file }

您尝试将其作为带参数的网址传递。

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

$category_values = $_POST["data_category"];