普通网友 2016-05-03 12:32
浏览 72
已采纳

使用jquery ajax将图像上传到文件夹

home.php

<form role="form" name="form1"  id="myform" enctype="multipart/form-data">
                              <div class="form-group">
                                  <label >Title</label>
                                  <input type="text" class="form-control" name="title_name" id="title" placeholder="Enter Title" required>
                              </div>

                              <div class="form-group">
                                  <label >Upload Image</label>
                                  <input type="file" id="imagefile" name="image_file" required>

                              </div>

                              <button type="submit" class="btn btn-primary" id="submit1" name="submit">Submit</button>
                          </form>
        <script>


                $("#myform").submit(function(e){
                    e.preventDefault();
                });

                $("#submit1").click(function(){
                    var title = $("#title").val();
                    var imagefile = $("#imagefile").val();

                    var mydata = {'title_name':title,'image_file':imagefile}; 

                  $.ajax({
                      url:"home-insert.php",
                      type:"POST",
          cache: false,
                      data: mydata,
                      success: function(data){
                          $("#title").val('');
                          $("#imagefile").val('');
                      }
                  });        
                });
                </script>

my php file is

home-insert.php

$title_name = $_POST['title_name'];
               $image_file = $_POST['image_file'];

               $name = $_FILES['image_file']['name'];
               $tmp_name = $_FILES['image_file']['tmp_name'];

      move_uploaded_file($tmp_name,"uploads/".$name);
                   mysql_query("INSERT INTO home (title,image) VALUES ('".$title_name."','".$image_file."')") or die(mysql_error());

I am unable to upload file to to the folder.please any one help me....

  • 写回答

1条回答 默认 最新

  • douyan2821 2016-05-03 12:47
    关注

    You can use new FormData($(this)[0]); for image post. Change your codes with this

    home.php


    <form role="form" name="form1"  id="myform" enctype="multipart/form-data">
        <div class="form-group">
            <label >Title</label>
            <input type="text" class="form-control" name="title_name" id="title" placeholder="Enter Title" required>
        </div>
    
        <div class="form-group">
            <label >Upload Image</label>
            <input type="file" id="imagefile" name="image_file" required>
        </div>
        <button type="submit" class="btn btn-primary" id="submit1" name="submit">Submit</button>
    </form>
    
    <script src="js/jquery-1.12.3.min.js" type="text/javascript"></script>
    <script>
            $("#myform").on("submit", function(e){
                e.preventDefault();
    
                var mydata = new FormData($(this)[0]);
    
                $.ajax({
                    url:"home-insert.php",
                    type:"POST",    
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: mydata,
                    success: function(data){
                        $("#title").val("");
                        $("#imagefile").val("");
                    }
                });        
            });
    </script>
    

    home-insert.php


    <?php
    $title_name = $_POST['title_name'];
    $name = $_FILES['image_file']['name'];
    $tmp_name = $_FILES['image_file']['tmp_name'];
    
    move_uploaded_file($tmp_name,"./uploads/".$name);
    mysql_query("INSERT INTO home (title,image) VALUES ('".$title_name."','".$name."')") or die(mysql_error());
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据