无法从php检索变量

时间:2014-08-08 09:59:27

标签: php jquery html ajax smarty

PHP文件:

<?php

#Communication with the API
require_once 'api.php';
$cloudkey = new CloudKey($user_id, $api_key);


if(isset($_FILES["FileInput"]) && $_FILES["FileInput"]["error"]== UPLOAD_ERR_OK)
{
    //Deleted Code: putting the uploaded files in my server.

    if(move_uploaded_file($_FILES['FileInput']['tmp_name'], $UploadDirectory.$NewFileName ))
       {

 $video_file = "/home/george/public_html/q/"; 
 $video_file = $video_file . $NewFileName; 

#Sending the video from my server to the cloud server
$res = $cloudkey->file->upload_file($video_file);

#The cloud server will create a link
$source_url = $res->url;

#The cloud server will convert the video very fastly
while(1) {
    #Deleted Code: Conversion
    if ($asset->status == 'ready') {
        # A lot of code. It will convert the videos here and if it succeeds it will stop.
    break;
    # If the conversion fails, display error
    } else if ($asset->status == 'error') {
        echo "Error while transcoding the media\n";
    }
    sleep(1);
}


# Getting the URL of a thumbnail
echo $thumb = $cloudkey->media->get_stream_url(array('id' => $media_id, 'asset_name' => 'jpeg_thumbnail_source'));

    }else{
        die('error uploading File!');
    }

}
else
{
    die('Something went wrong!');
}
?>

HTML文件:

  {literal}
<script type="text/javascript">


$(document).ready(function() { 
    var options = { 
            target:   '#output',   // target element(s) to be updated with server response 
            beforeSubmit:  beforeSubmit,  // pre-submit callback 
            success:       afterSuccess,  // post-submit callback 
            uploadProgress: OnProgress, //upload progress callback 
            resetForm: true        // reset the form after successful submit 
        }; 


//function after succesful file upload (when server response)
function afterSuccess()
{
    $('#loading-img').hide(); //hide submit button
    $('#progressbox').fadeOut(); //hide progress bar
    //I WANT TO INSERT SOMETHING HERE.
}



//progress bar function
function OnProgress(event, position, total, percentComplete)
{
   //DOES NOT INTEREST YOU
}); 

</script>


{/literal} 

<div id="output"></div>

我正在使用smarty模板引擎。我有一个html上传表单,将与php文件 progressupload.php 进行通信,其中php文件将转换视频(使用API​​服务)并在其中带回响应饰面。

当用户上传视频文件时,ajax将接管显示百分比(以html为单位),并将文件发送到 progressupload.php ,当 progressupload.php 完成转换后,只需将此内容放入html文件即可输出 php 中回显的所有内容(我不会这样做)知道原因):<div id="output"></div>

我想要什么:

progressupload.php 完成转换时,它会生成缩略图(屏幕截图)并将其链接存储在$thumb中。我想检索$thumb并使用jquery将其显示给用户。

现在,如果您查看HTML代码,afterSuccess()是我想向用户显示缩略图的功能:

function afterSuccess()
{
    $('#loading-img').hide(); //hide submit button
    $('#progressbox').fadeOut(); //hide progress bar
    //I WANT TO SHOW THE THUMBNAIL HERE.
}

我删除了很多不必要的代码,这些代码可能会分散您的注意力。请记住,我使用的是smarty模板引擎,我的html文件不以 .php 结尾,我无法直接回显变量。如何在创建变量后检索php变量并将其放在jquery方法中。

我怀疑如果你直接在页面加载时检索变量,它将无法成功获取$thumb,因为链接需要时间来创建(上传,转换)。如何检索$thumb

1 个答案:

答案 0 :(得分:1)

假设您使用的是jQuery.ajax();
$ .ajax&#39;成功的第一个参数&#39;回调是从服务器端返回的。

function afterSuccess( response /** this is what returned from your server **/ )
{
    $('#loading-img').hide(); //hide submit button
    $('#progressbox').fadeOut(); //hide progress bar
    //I WANT TO INSERT SOMETHING HERE.

    console.log(response); // here you can access your $thumbnail
    // below code is untested but it should work
    // $("#output").html("<img src='"+response+"' class='thumbnail' />");
}