weixin_33743880 2016-09-08 11:40 采纳率: 0%
浏览 14

使用AJAX返回PHP数据

I am slowly learning how to build a fully custom CMS system, and currently building a Image Uploader. Ive got my PHP form to work but what i want is to send and return data to the upload.php file using AJAX. So far i have managed to successfully send the form data to PHP and the image is uploaded into the my uploads folder. The trouble I am having is that nothing is returned to me to tell me if it was successful or not. I would like the image to display if its successful and and error message if its not. I am not that experienced with using Javascript or AJAX so could someone please look at my code and tell me where i am going wrong?

HTML UPLOAD FORM

<div class="container-main">
  <form action="" method="post" id="myForm" enctype="multipart/form-data">
    <input type="file" name="file" id="file"><br>
    <input type="submit" id="upload-img" name="submit" class="btn btn-success" value="Upload Image">
  </form>

  <div class="progress progress-striped active">
    <div class="progress-bar"  role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
      <span class="sr-only">0% Complete</span> 
    </div>
  </div>
  <div class="image">
  <!-- uploaded image goes here -->   
  </div>
</div>

<script type="text/javascript" src="js/custom.js"></script>

JS

$('#upload-img').on('click', function() {
    var file_data = $('#file').prop('files')[0];   
    var form_data = new FormData();                  
    form_data.append('file', file_data);
    alert(form_data);                             
    $.ajax({
                url: 'upload.php', // point to server-side PHP script 
                dataType: 'text',  // what to expect back from the PHP script, if anything
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,                         
                type: 'post',
                success: function(){
                    $(".image").html("<img src='"+response.responseText+"' width='100%'/>");

                }
     });
});

upload.php

$allowedExts = array("gif", "jpeg", "jpg", "png","GIF","JPEG","JPG","PNG");

$temp = explode(".", $_FILES["file"]["name"]);

$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts)) {

  if ($_FILES["file"]["error"] > 0) {
    echo "Invalid File Type";
  } else {
    $target = "../upload/";
    move_uploaded_file($_FILES["file"]["tmp_name"], $target. $_FILES["file"]["name"]);
    echo  "../upload/" . $_FILES["file"]["name"];

  }
} else {
  echo "Error uploading image";
  die();
}

Many Thanks!

  • 写回答

1条回答 默认 最新

  • hurriedly% 2016-09-08 11:44
    关注

    You don't need to use the .responseText property (it doesn't exist anyway)

    and your function should be like so:

    success: function(response){
                        $(".image").html('<img src="'+response+'" width="100%"/>');
    
                    }
    

    Note I changed your single/double quotes around.

    评论

报告相同问题?

悬赏问题

  • ¥15 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么