How to send PHP variables to Ajax(Jquery)?

I have sample file from the internet:

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<link href="" rel="stylesheet">
<script src=""></script>
<script src=""></script>

<div id="mulitplefileuploader">Upload</div>

<div id="status"></div>


var settings = {
    url: "upload.php",
    method: "POST",
    fileName: "myfile",
    multiple: true,
        $("#status").html("<font color='green'>Upload is success</font>");
    onError: function(files,status,errMsg)
        $("#status").html("<font color='red'>Upload is Failed</font>");


+++PHP+++ upload.php

//If directory doesnot exists create it.
$output_dir = "uploads/";

    $ret = array();

    $error =$_FILES["myfile"]["error"];
        if(!is_array($_FILES["myfile"]['name'])) //single file
                $fileName = $_FILES["myfile"]["name"];
                move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $_FILES["myfile"]["name"]);
                 //echo "<br> Error: ".$_FILES["myfile"]["error"];
                     echo $ret[$fileName]= $output_dir.$fileName;
                $fileCount = count($_FILES["myfile"]['name']);
              for($i=0; $i < $fileCount; $i++)
                  $fileName = $_FILES["myfile"]["name"][$i];
                     $ret[$fileName]= $output_dir.$fileName;
                move_uploaded_file($_FILES["myfile"]["tmp_name"][$i],$output_dir.$fileName );
    echo $_FILES["myfile"]["name"];
    echo json_encode($ret);


How to display display value from PHP?

by using the onSuccess handler of the plugin.

Thanks for reply. I’d need to display $_FILES[“myfile”][“name”], but it didn’t show anything. I’m very new with Ajax.
If you can show me how to get each variable from PHP to display in Ajax is great for me.

Your code example is a bit confusing.

It looks like the “echo” lines are there for developing / debugging purposes.

I’m guessing that the variable “$ret” is so named because it is meant to hold what is “returned”.

If you are done testing the code, you can comment out the “echo” lines.

Then after // echo json_encode($ret); put

return json_encode($ret);

Everything that the PHP code has assigned to the variable will now be available in the “success” portion of your AJAX code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.