1

I'm trying to upload a file and get some processed data from it by PHP,but I came across some problems. Here are my simplified codes.

HTML CODE

<form action="ajax.php" method="post" enctype="multipart/form-data">
<input type="file" name="mypic"> 
<input type="submit" value="upload" id="submit"> 
</form>

JS CODE

$(function(){
    var button = $('#submit');
    button.click(function(){
        alert("You clicked!");
        getdata();
    });

})
function getdata(){
    function onDataReceived(data) {
        alert("Get the data!");
    }
    $.ajax({
        url : "ajax.php",
        method : 'GET',
        cache : false,
        dataType : 'json',
        success : onDataReceived
    });
}

ajax.php

<?php
    $filepath = $_FILES["mypic"]["tmp_name"];   
    $filename = $_FILES["mypic"]["name"];

    SOME CODE HRER;

   $mech_para = "Hello";
   $json = json_encode(array("mech_para"=>$mech_para));

   echo $json;
?>

Once I clicked the submit button, the page will jump to ajax.php, and it shows {"mech_para":"Hello"}, but without the alert:Get the data!.

But if I delete the first two lines in the ajax.php as below

<?php
        SOME CODE HRER;

       $mech_para = "Hello";
       $json = json_encode(array("mech_para"=>$mech_para));

       echo $json;
?>

The result turns to be totally fine, the page jump to ajax.php and shows the right data, and the alert(Get the data!) also shows.

I don't know what causes this result and how to solve it, I wander if there is any conflicts between ajax and the GLOBAL VARIABLE _FILE, OR if there is any other way that I can both process the file and return the data I want to JS. Very thanks!

12
  • You're not sending anything at all from the client, yet you seem to be expecting files on the server ? Commented Oct 19, 2016 at 16:26
  • I send a file stored in my computer within the form to ajax.php. Commented Oct 19, 2016 at 16:31
  • You need to use POST in ajax to make $_FILES work. Commented Oct 19, 2016 at 16:31
  • Nope, the ajax request isn't sending anything, but as you haven't prevented the form from submitting, it could be sending the file and redirecting Commented Oct 19, 2016 at 16:33
  • @Yuki, Can you make it more explicit? Commented Oct 19, 2016 at 16:34

2 Answers 2

3

Because you're simply ASSUMING success. You told jQuery to expect JSON, and what you're sending out is almost certainly NOT json.

You're doing a GET request, which means it's IMPOSSIBLE for $_FILES to contain anything. That means you're generating PHP warnings for undefined indexes

You have no error handler on your jquery call, so jquery can't even tell you that it failed to decode this (corrupt) json it received.

Never EVER assume success when dealing with external resources. Always assume failure, check for failure, and treat success as a pleasant surprise. That means you need to have

$.ajax(
   ....
   error : function(jqerr, msg) {
      alert('ajax request failed: ' + msg);
   }),
   c....
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your advice, I'll keep this in mind! But I did't get any error meg neither after adding those codes.
1

You have to prevent the form from submitting, and actually send the file.

<form id="myForm" action="ajax.php" method="post" enctype="multipart/form-data">
    <input type="file" name="mypic"> 
    <input type="submit" value="upload" id="submit_button"> 
</form>

and then

$(function(){
    $('#myForm').on('submit', getdata);
});

function getdata(e){
    e.preventDefault();

    function onDataReceived(data) {
        alert("Get the data!");
    }

    $.ajax({
        url         : "ajax.php",
        method      : 'POST',
        contentType : false,
        processData : false,
        data        : new FormData(this),
        dataType    : 'json',
        success     : onDataReceived
    });
}

10 Comments

I still can't the json data back from ajax.php, I don't know what on earth is it....
So you're not seeing the alert, and no errors in the console (F12), and you changed the ID for the button as in the form above
I've changed the ID, and still no alert.
And you added Marc's advice, with the error handler, and there's no errors there either
Yes, I add a alert function there, no alert neither.
|

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.