2016-10-18 42 views
0

我在做什么?我如何使用JavaScript代码与PHP?

我有一个dynamically created html division包含用户可以在社交媒体上分享的用户玩游戏的分数。

对于分享我有元标记例如Facebook的

<meta property="og:image" content= "<?php echo $img_url; ?>"/>

$img_urldynamically created html division截图的链接。

test.php的 - 在我上面提到的meta标签放置,我想如果$img_url未设置采取html div截图。

<?php 
 
    session_start(); 
 
    
 
    $img_url = $_POST['img_url']; 
 
    
 
    if(!isset($url)) { 
 

 
    /* <script> 
 
    function capture() {  
 

 
    html2canvas will give var img. 
 
    Which will be POSTED to testsave.php to save it in the server. 
 
     
 
    } 
 
    </script> */ 
 
} 
 
?> 
 
<html> 
 

 
<!--dynamically created html division --> 
 

 
</html>

testsave.php - 将从test.php采取张贴值和截屏图像保存到服务器,将发回$img_urltest.php

<?php 
 
session_start(); 
 
    
 
/*Get the base-64 string from data 
 

 
Decode the string 
 
    
 
Save the image 
 

 
function predefined for random string*/ 
 

 
$img_url = $random_str.".png"; 
 

 
file_put_contents($img_url, $unencodedData); 
 

 
$_SESSION['img_url'] = $img_url ; 
 

 
?>

我的问题是什么?

当facebook刮掉一个页面时,它不需要session values或来自其他页面的任何值。所以我不能发送img_val from testsave.php 到另一个页面,因为刮板不会读取POSTED值。

所以,我在做什么时刮板将刮test.php那么如果$img_val没有设置我希望我的代码,以截图,并张贴js可变var imgtestsave.php保存图像,然后将回发的值$img_valtest.php

我知道在上面的代码中可能会有很多错误,例如我不能在php中放置js。但我尝试了很多方法,但无法弄清楚。

您能否建议我能做些什么来使代码正常工作,或者您能建议其他方式来做到这一点?

+0

回声出HTML您是否尝试过使用AJAX? – Icewine

+2

PHP是服务器端,您需要将您的脚本“回显”为HTML。 –

+0

@Iwwine如果您要求使用ajax将img val发布在同一页面中。我做到了,但由于ajax稍后将值返回给同一页面,因此刮板无法刮取该值。 – rinja

回答

1

你必须从PHP

<?php 
    session_start(); 

    $img_url = $_POST['img_url']; 

    if(!isset($url)) { 

    echo "<script> 
    function capture() {  

    //your js code here 

    } 
    </script>"; 
} 
?> 
<html> 

<!--dynamically created html division --> 

</html>