2

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 comment.

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

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 Commented Jun 1, 2012 at 19:23

3 Answers 3

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.)

Sign up to request clarification or add additional context in comments.

15 Comments

Well this is all in an html page.
php will not execute in an html page. It needs to "be" a php page (file extension of php, not html) for the php code to be processed.
Servers can be set up to process .html pages as php. @Zahel: see if any php at all runs correctly before troubleshooting the javascript.
@ScottSaunders, it runs a regular php just fine.
By "regular php," do you mean a file ending in .php?
|
1

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.

2 Comments

How do you explain the different strings?
Ah, I see what you mean, the "signed request" was bit was not supposed to be there, it was supposed to be "the name". All the code here is referring to the same code, I just overwrote something by accident.
0

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

3 Comments

just tried this, changed nothing. Will change the initial post to reflect this.
that should not stop php from printing the string
it also wouldn't stop the javascript from running

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.