1

I have three type of inputs those are file input, text input and variable. I want to upload those inputs by sending data to PHP file by using Ajax JSON. Also I want to know how to capture these data in PHP file.

I am using HTML code without form syntax. variable data name as a val1 in JQuery code.

HTML Code:

<div class="container" id="post">
    <textarea id="posttext" autocomplete="off"></textarea>
    <input type="file" name="file" id="file" multiple accept=".mp4, .mov, .m4v, .MPEG-4, .gif, .jpg, .png"/>
    <button type="button" id="submitpost">Submit</button>
</div>

JQuery Code:

$(document).on("click", "#submitpost", function(){
    var val1 = "Some Datas";

        $.ajax({
        url: post.php,
        type: 'POST',
        data: VALUES,
        async: false,
        success: function (data) {
            alert(data)
        },
        cache: false,
        contentType: false,
        processData: false
        });
});

PHP Code:

<?php
if (!isset($_POST['VALUES']) && !empty($_POST['VALUES'])) {
        $params = $_POST['VALUES'];

}
?>

How to get each values in PHP to Upload files and insert text and variable data to database.

1 Answer 1

1

Use this code:

Html:

<div class="container" id="post">
  <form enctype="multipart/form-data" method="POST" id="myform">
    <textarea id="posttext" name="posttext" autocomplete="off"></textarea>
    <input type="file" name="file" id="file" multiple accept=".mp4, .mov, .m4v, .MPEG-4, .gif, .jpg, .png"/>
    <button type="submit" name="submitpost" id="submitpost">Submit</button>
   </form>
</div>

Jquery:

$(document).on("click", "#submitpost", function(e){
    $("form#myform").submit();
});
$("form#myform").on('submit', function(e){
    e.preventDefault();
    var val1 = "Some Data";
    var file = this.files[0];
    var form = new FormData();
    form.append('file', file);
    form.append('val1', val1);
    form.append('posttext', $('#posttext').val());
    $.ajax({
        url : "post.php",
        type: "POST",
        cache: false,
        async: false,
        contentType: false,
        processData: false,
        data : form,
        success: function(response){
            alert(response);
        }
    });
});

PHP Code:

<?php
if (isset($_POST) && !empty($_POST)) {
    print_r($_POST);
    print_r($_FILES);
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

where is the variable data (val1)\
ajax is working i put each "Text" code in php file and I get Text response but not values
I got response like this Array ( [file] => undefined [val1] => Some Data [posttext] => jjj ) Array ( ) but file is undefined
I use var file = this.files; like this. I got error for var file = this.files[0];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.