简体   繁体   English

如何在 php 中使用 javascript 代码?

[英]How can I use javascript code with php?

What am I doing ?我在做什么?

I have a dynamically created html division containing the score of a game played by user which user can share on social media.我有一个dynamically created html division其中包含用户可以在社交媒体上分享的用户玩游戏的分数。

For sharing I have meta tag eg facebook为了分享,我有元标签,例如 facebook

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

Where $img_url is the link of the screenshot of dynamically created html division .其中$img_urldynamically created html division截图的链接。

test.php -- where my above mentioned meta tag is placed and I want to take screenshot of html div if $img_url is not set. test.php——我上面提到的元标记所在的位置,如果未设置$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 -- which will take the posted value from test.php and save the screenshot image to server and will send back the $img_url to test.php testsave.php -- 它将从test.php获取发布的值并将屏幕截图图像保存到服务器,并将$img_url发送回test.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 ; ?>

What my problem is ?我的问题是什么?

When facebook scrapes a paricular page , it doesn't take session values or any values from other page.当 facebook 抓取特定页面时,它不会从其他页面获取session values或任何值。 So I can't send img_val from testsave.php to another page because scraper won't read POSTED values.所以我不能将img_val从 testsave.php 发送到另一个页面,因为刮板不会读取POSTED值。

So, what am I doing is when scraper will scrape test.php then if $img_val is not set I want my code to take the screenshot and post js variable var img to testsave.php to save the image, which will then post back the value of $img_val to test.php .所以,我在做什么是当刮板刮test.php然后如果$img_val没有设置我希望我的代码截取屏幕截图并将js变量var imgtestsave.php以保存图像,然后将回发$img_valtest.php值。

I know there may be many mistakes in above code like I can't put js inside php .我知道上面的代码可能有很多错误,比如我不能把js放在php But I tried it many way but couldn't figure it out.但是我尝试了很多方法,但无法弄清楚。

Can you please suggest what can I do to make the code work or can you suggest any other way to do it ?您能否建议我该怎么做才能使代码正常工作,或者您可以建议任何其他方法吗?

You have to echo out HTML from PHP你必须从 PHP 中回显 HTML

<?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>

One of the solutions is to use here-document to assign the whole HTML page to PHP variable and later echo/print it where it matches your needs, and you can include the Javascript codes there as well解决方案之一是使用 here-document 将整个 HTML 页面分配给 PHP 变量,然后在符合您需要的地方回显/打印它,您也可以在那里包含 Javascript 代码

Example "this example doesn't include the image thing you mentioned but you can use any Javascript codes"示例“此示例不包含您提到的图像内容,但您可以使用任何 Javascript 代码”

<?php

$variable=<<<_END
<html>
<p>Some HTML line...</p> 
<script>
//you can write the Javascript codes you need here
 console.log('Find this sentence at console !')
</script>
</html>
_END;

echo $variable;

?>

In order to deliver it in browser, we have to construct JS code as a string inside the PHP code为了在浏览器中传递它,我们必须将 JS 代码构造为 PHP 代码中的字符串

eg.例如。 Test.phtml - where i am calling a js function under some condition. Test.phtml - 我在某些条件下调用 js 函数。

<body>
 <?php if($this->emptyData){ ?>
    <div id="someTestDialog">
        <!-- call js function below -->
        <?php
            echo '<script type="text/javascript">testDialog();</script>';
        ?>
    </div>
<?php } ?>

</body>
<script>
function testDialog() {
    return $("<div class='dialog' title='Alert'><p>Hello World</p></div>")
        .dialog({
            resizable: false,
            height:90,
            width:90,
            modal: true,
            dialogClass: 'no-close success-dialog',
            buttons: {
                "OK": function() {
                $( this ).dialog( "close" );
            }
         }
    });}
$(document).ready(function() {
    var text = "some code here" });    </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM