How to send PHP variables to Ajax(Jquery)?

I have sample file from the internet:
index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="https://rawgithub.com/hayageek/jquery-upload-file/master/css/uploadfile.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="https://rawgithub.com/hayageek/jquery-upload-file/master/js/jquery.uploadfile.min.js"></script>
</head>
<body>

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

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

$(document).ready(function()
{

var settings = {
    url: "upload.php",
    method: "POST",
    allowedTypes:"jpg,png,gif,doc,pdf,zip",
    fileName: "myfile",
    multiple: true,
    onSuccess:function(files,data,xhr)
    {
        $("#status").html("<font color='green'>Upload is success</font>");
        
    },
    onError: function(files,status,errMsg)
    {        
        $("#status").html("<font color='red'>Upload is Failed</font>");
    }
}
$("#mulitplefileuploader").uploadFile(settings);

});
</script>
</body>

+++PHP+++ upload.php

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

if(isset($_FILES["myfile"]))
{
    $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;
        }
        else
        {
                $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.