0

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

Noah Skull Weijian
  • 129
  • 1
  • 2
  • 8
  • Have you checked the permissions on the directory to which you're trying to upload the file? As a test, chmod 777 the directory and run your script. Bear in mind, 777 permits read, write, execute for all users on that directory... – Craig Oct 06 '16 at 18:24
  • @Craig just checked the permission and it is 777. So i guess is not that problem – Noah Skull Weijian Oct 06 '16 at 18:26
  • 1
    Don't edit the question with information from answers as it clouds the issue and may not allow folks to give you a proper answer. – Jay Blanchard Oct 06 '16 at 18:56

2 Answers2

0

You have to fix your form with enctype:

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

I suggest you this read

sensorario
  • 20,262
  • 30
  • 97
  • 159
0
<form id="data" enctype="multipart/form-data"> 

i think your missing enctype="multipart/form-data" in from,
hope this may help you

jQuery AJAX file upload PHP

use above url to find solution

Community
  • 1
  • 1
Umakant Mane
  • 1,001
  • 1
  • 8
  • 9