简体   繁体   English

从PHP回显JavaScript无法正常工作?

[英]echo javascript from php not working?

So, in an html page I'm trying to have a php segment echo some javascript code, as seen here: 因此,在html页面中,我试图让php段回显一些javascript代码,如下所示:

<?php
    echo "This was legitimately hit";
    if(!empty($_POST['name']))
    {
        echo '<script type="text/javascript">alert("We got the name");</script>';
    }
    else
    {
        echo '<script type="text/javascript">alert("We DID NOT get the name");</script>';
    }
?>

and from what I've read online, this seems to be a legitimate way of doing things, but the page seems to be reading the first part up until the first closing chevron (seen just below here) as a comment. 从我在网上阅读的内容来看,这似乎是一种合法的处理方式,但是该页面似乎一直在阅读第一部分,直到第一个关闭V形字形(如下所示)为止。

<?php
    echo "This was legitimately hit";
    if(!empty($_POST['name']))
    {
        echo '<script type="text/javascript">

Then it reads the else and next echo as plain text, and puts it on the webpage. 然后,它将else和next echo作为纯文本读取,并将其放在网页上。 the next javascript code block then gets read as a regular javascript code block, so the page does a pop-up saying it did not get the name. 接下来,下一个javascript代码块将作为常规javascript代码块被读取,因此该页面会弹出一个对话框,提示它没有获取名称。 The closing bracket and closing chevron then just get output as more text. 然后,右括号和V形结尾将输出为更多文本。

So in the end the page just ends up having 所以最终页面只是

alert("We got the name")'; } else { echo ''; } ?>

printed on it as plain text, and has a pop-up that says we received no name. 以纯文本形式打印在其上,并弹出一个窗口,提示我们没有收到任何名字。

What is going wrong here? 这是怎么了?

Sounds like the file isn't being processed as PHP. 听起来好像文件没有作为PHP处理。 Does the file name end in .php? 文件名是否以.php结尾? Are you sure PHP is installed and hooked up correctly to the web server? 您确定PHP已安装并正确连接到Web服务器吗?

edit: To handle the Facebook requests in the same page: 编辑:要处理同一页面中的Facebook请求:

<?php

if (isset($_POST['facebook_request_field'])) {
  // handle the Facebook request, output any necessary response
  // then exit
  exit;
}

?>
<!-- display the web page normally here -->

So for your test page: 因此,对于您的测试页:

<?php

if (isset($_POST['name'])) {
  echo '<script type="text/javascript">alert("got a name!");</script>';
  exit;
}

?>
<script type="text/javascript">alert("No name.");</script>

(That's actually identical in function to what you already have, so maybe I'm misunderstanding the purpose.) (实际上,该功能与您已经拥有的功能相同,因此也许我误解了这个目的。)

Between We got the signed request and We got the name , I think you haven't given us the actual code that's causing the error. We got the signed requestWe got the name ,我想您还没有提供导致错误的实际代码。 Double check that, and make sure you don't have any stray single quotes before your alert call. 请仔细检查,并确保在发出alert之前没有任何单引号。

There are missing ; 有失踪; after the alert . alert Have you tried correcting this first? 您是否尝试过首先纠正此问题?

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

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