最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

echo javascript from php not working? - Stack Overflow

programmeradmin5浏览0评论

So, in an html page I'm trying to have a php segment echo some javascript code, as seen here:

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

<?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. 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. The closing bracket and closing chevron then just get output as more text.

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?

So, in an html page I'm trying to have a php segment echo some javascript code, as seen here:

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

<?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. 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. The closing bracket and closing chevron then just get output as more text.

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?

Share Improve this question edited Jun 1, 2012 at 19:22 ZachLHelms asked Jun 1, 2012 at 19:05 ZachLHelmsZachLHelms 3172 gold badges4 silver badges10 bronze badges 1
  • I don't think this is the real code except you are writing the code in an environment where html tags are not allowed - like in Content Management System html editor – codingbiz Commented Jun 1, 2012 at 19:23
Add a ment  | 

3 Answers 3

Reset to default 4

Sounds like the file isn't being processed as PHP. Does the file name end in .php? Are you sure PHP is installed and hooked up correctly to the web server?

edit: To handle the Facebook requests in the same page:

<?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. Double check that, and make sure you don't have any stray single quotes before your alert call.

There are missing ; after the alert. Have you tried correcting this first?

发布评论

评论列表(0)

  1. 暂无评论