-2

I want to upload PDF file through Ajax in php but I could not complete the code. Please help

My Form is looks like this

<form action="javascript:void(0)" method="post" id="ajax-form" enctype="multipart/form-data>
                            <input type="" name="user_id" value="<?= $_SESSION['id']?>" hidden ><!---user id from tb_user table-->
                            <input type="" name="user_name" value="<?= $_SESSION['reader_name']?>" hidden > <!--user name from tb_user--->
                            <input type="" name="aid" value="<?= $aid?>" hidden > <!---assignment id from tb_assignment----->
                            <input type="" name="aname" value="<?= $q['assignment']?>" hidden> <!------assignment name from tb_assignment-------->
                            <input type="" name="a_answerid" value="<?= $row['id']?>" hidden><!---assignment answer ID from assignment answer table--->
                            <textarea class="form-control" id="textAreaAdjust" name="reply" style="overflow:auto" placeholder="Write your comment here"></textarea>
                            <label for="formFile" class="form-label">Default file input example</label>
                            <input class="form-control" type="file" id="formFile">
                            <div class="col-lg-3 col-sm-3">
                            <input type="submit" class="btn btn-outline-dark mt-2 text-left mb-4" name="submit" value="Reply">
                        </div>
                        </form>

And my Ajax code is looks like this. I am confused how to write code for ajax to submit the PDF file.

<script type="text/javascript">
        $(document).ready(function($){
     
        // hide messages 
        $("#error").hide();
        $("#show_message").hide();
     
        // on submit...
        $('#ajax-form').submit(function(e){
     
            e.preventDefault();
     
            $("#error").hide();
     
            //First name required
            var reply = $("input#reply").val();
           if(reply == ""){
                $("#error").fadeIn().text("Please Write Something");
                $("input#reply").focus();
                return false;
            }

            var userid = $("input#user_id").val();
           if(userid == ""){
                $("#error").fadeIn().text("Please Write Something");
                $("input#user_id").focus();
                return false;
            }

            var uname = $("input#user_name").val();
           if(uname == ""){
                $("#error").fadeIn().text("Please Write Something");
                $("input#user_name").focus();
                return false;
            }

            var aid = $("input#aid").val();
           if(aid == ""){
                $("#error").fadeIn().text("Please Write Something");
                $("input#aid").focus();
                return false;
            }

            var aname = $("input#aname").val();
           if(aname == ""){
                $("#error").fadeIn().text("Please Write Something");
                $("input#aname").focus();
                return false;
            }
            var aname = $("input#a_answerid").val();
           if(a_answerid == ""){
                $("#error").fadeIn().text("Please Write Something");
                $("input#a_answerid").focus();
                return false;
            }
         
            // ajax
            $.ajax({
                type:"POST",
                url: "comment.php",
                data: $(this).serialize(), // get all form field value in serialize form
                success: function(){
                    location.reload();
                }
            });
        });  
     
        return false;
    });
    </script>

And my php code for action is as below

extract($_POST);

if(mysqli_query($con, "INSERT INTO tb_comment(comment, user_id, user_name, a_id, a_name, a_answerid) VALUES('" . $reply . "', '" . $user_id . "', '" . $user_name . "', '" . $aid . "', '" . $aname . "' , '" . $a_answerid . "')")) {
 echo '1';
 exit;
} else {
   echo "Error: " . $sql . "" . mysqli_error($con);
}

mysqli_close($con);

Please help me to submit a file through this ajax.

  • you have no name for the input[type=file] .. maybe that's why it cant be retrieved for the POST – skytorner May 20 '22 at 08:27
  • Does this answer your question? [How can I upload files asynchronously with jQuery?](https://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery) – CBroe May 20 '22 at 09:26
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman May 20 '22 at 11:16

0 Answers0