down00112 2016-11-27 18:38
浏览 31
已采纳

Jquery和AJAX发布到php数据属性?

Hello I have the following AJAX code:

 var formData = new FormData($('form')[0]);
$.ajax({
    url: 'saveImage.php',  //Server script to process data
    type: 'POST',
    data: formData,
    processData: false,
    success: function(data){
        console.log(data);            
    }

});

It works great and it loads up the PHP page it the background like it should:

    <?php
        include_once "mysql_connect.php";
        $imageName = mysql_real_escape_string($_FILES["Image1"]["name"]);
        $imageData = '';
        $imageext = '';
        if($imageName != null){
            $imageData = mysql_real_escape_string(file_get_contents($_FILES["Image1"]["tmp_name"]));
            $imageType = mysql_real_escape_string($_FILES["Image1"]["type"]);
            $imageSize = getimagesize($_FILES["Image1"]["tmp_name"]);
            $imageType = mysql_real_escape_string($_FILES["Image1"]["type"]);
            $FileSize = FileSize($_FILES["Image1"]["tmp_name"]);
            $imageext = mysql_real_escape_string($imageSize['mime']);
        }
        $query=mysql_query("INSERT INTO pictures (`id`, `imagedata`, `imageext`) VALUES ('', '$imageData', '$imageext');"); 


        echo $imageext;


?>

The only problem is that the PHP page cant find the variable Image1 which is the name of the input in the form. Have I done something wrong. I was thinking that maybe in the data parameter in the Ajax it would be something like this but correct:

data: "Image1"=formData,

Is that a thing, if not why cant my PHP see that input field?

  • 写回答

1条回答 默认 最新

  • douhuireng4407 2016-11-27 19:12
    关注

    You forgot cache and contentType properties in your Ajax function. Try that it should work :

    var formData = new FormData($('form')[0]);
    $.ajax({
       type: "POST",                
       url: "saveImage.php",
       processData: false,
       contentType: false,
       cache:false,
       data: formData,
       success: function(data){
            console.log(data);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?