0

I'm trying to send a file to a php script through ajax, however I see that it's not being passed properly as I tried to echo that file and got a null response. How can I solve this?

HTML JS code

<script type="text/javascript">
$(function(){
    $("#first").submit(function(event){
        event.preventDefault();

        $(this).find(".error").remove();
        file = $(this).find("input[name=file_save]").val();
        form = $(this);
        url = $(this).attr("action");
        $.post(url,{file_save:file}, function(data){    
            console.log(data);
        },"json");
        return false;
    });
});

</script>

Php script (addImages.php)

<?php
$username = $_POST['files'];
$tableau["error"] = $username;
echo json_encode($tableau);
?>
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Bobby
  • 496
  • 5
  • 18

1 Answers1

0

$tableau is undefined.
Your PHP srcipt should be like this:

<?php
$username = $_POST['files'];
$tableau = [];
$tableau["error"] = $username;
echo json_encode($tableau);
Ad5001
  • 748
  • 5
  • 19
  • Thanks man, but I get the same issue with the changes you've requested – Bobby Feb 16 '17 at 13:35
  • @Bobby Then try looking into the the [$_FILES](http://php.net/manual/en/reserved.variables.files.php) variable. – Ad5001 Feb 16 '17 at 13:38