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 として渡そうとしています。

POST次に、パラメーターの名前を使用して PHP で取得する必要があります。例えば:

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