du8864 2016-10-06 18:19
浏览 37

使用jquery Ajax和PHP上传文件

I am trying to upload a file to a folder using jQuery Ajax and PHP.

This is the form I use to upload a file.

<form id="data" method="post" enctype="multipart/form-data">
                <textarea name="txtmessage" placeholder="Write your post here"></textarea>
                </br>
                        <label>Subject:</label>
                        <input type="text" id="subcode" name="subcode" placeholder="Enter Subject Code e.g.: BIT304">
                        <br/>
                         <input name="img" type="file" /><br/>
                        <button type="submit" class="btn btn-default" id="btnAddAction" name="submit" value="Submit">Share</button>
                </form>
                <script>
                 $("form#data").submit(function(event){   
                      var formData = new FormData($(this)[0]);
                      $.ajax({
                        url: 'formprocessing.php',
                        type: 'POST',
                        data: formData,
                        async: false,
                        cache: false,
                        contentType: false,
                        processData: false,
                        success: function (returndata) {
                            $("#comment-list-box").append(returndata);

                        $("form#data")[0].reset();
                        $("#loaderIcon").hide();   
                        },
                        error:function (){}                 
                      });
                    });   
                </script>

and this is formprocessing.php to process the form.

<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
$id = $_SESSION['id'];

$subcode = $_POST['subcode'];
$txtmessage = $_POST['txtmessage'];

$file = $_FILES['img']['name'];

if($file != "") {
    $subcode = "General";
    $target = "uploads/";
    $fileTarget = $target.$file;
    $tempFileName = $FILES["img"]["tmp_name"];

    $infile = move_uploaded_file($tempFileName,$fileTarget);

    if($infile){
        $result= mysql_query("INSERT INTO fyp_comment(message, subject, userid, timeDate, image) VALUES('$txtmessage', '$subcode', '$id', now(), '$fileTarget')");
        if($result){
            $insert_id = mysql_insert_id();
        }
    }   
}
else{
    $result = mysql_query("INSERT INTO fyp_comment(message, subject, userid, timeDate) VALUES('$txtmessage', '$subcode', '$id', now())");
                if($result){
                  $insert_id = mysql_insert_id();
}
}

?>

However, I could not upload the files to the directory after i submit the form and the other part are not stored in the database. On the other hand, if i submit the form without a file the text are all stored into the database. What did I do wrong in this case? been thinking about this for an hour now . Thanks

  • 写回答

2条回答 默认 最新

  • 普通网友 2016-10-06 18:22
    关注

    You have to fix your form with enctype:

    <form name="data" method="post" enctype="multipart/form-data">
        ...
    </form>
    

    I suggest you this read

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line