0

I am printing a very simple JavaScript using PHP, which doesn't get executed. If I print the data, I see the following script (exactly as needed) at the end of the HTML file:

    <script type="text/javascript">
    document.getElementById("message").innerText="Email already exists";
    </script>

I have also tried using innerHTML="Email already exists";.

This is printed in PHP as such:

    echo "<script type=\"text/javascript\">
        document.getElementById(\"message\").innerText=\"Email already exists\";
        </script> ";

In my HTML I have an element which has the ID: message. It looks like this:

    <h3 id="message"> </h3>

What I am expecting is to get "Email already exists" in the h3, however this doesn't happen. The JavaScript is not executed. If I use the exact same JavaScript code but place it ahead or on an "onclick" request, the code works.

One thing which could be relevant: I noticed that the JavaScript is printed after the closing HTML tag.

How do i get the JavaScript code to execute after being echo'ed into the HTML? I've read several threads which said its supposed to simply run, however mine doesn't. I have tried about 50 different fixes, none of which worked.

The code: http://ideone.com/dmR42O

8
  • 1
    It all depends on where you're echoing the html relative to the <div> you're trying to modify. Remember that <script> tags are executed as they're encountered. If the <script> appears BEFORE the <div>, the div won't exist in the dom yet and getElementById will fail. Commented Mar 22, 2014 at 21:51
  • I used a PHP file which has HTML code inside. The HTML code lies BEFORE the PHP code, so the page is displayed and ID is identified by the browser. Only when the document is ready, the PHP code will run (which also requires a form submit) Commented Mar 22, 2014 at 21:58
  • Replace document.getElementById(\"message\").innerText=\"Email already exists\"; with alert("Code Executed "); and let us know the results Commented Mar 22, 2014 at 21:58
  • php runs on the server. the js can NOT possibly run until after the page has been built and sent/received by the client. Commented Mar 22, 2014 at 22:00
  • hi James. The alert gets printed on the HTML document but is not executed (i don't get an alert, but i see it on the console). i'll do a jsfiddle version of the code. Commented Mar 22, 2014 at 22:05

2 Answers 2

2

You mentioned this:

One thing which could be relevant. I noticed that the javascript is printed AFTER the closing html tag (the ).

That is very relevant. Any Javascript must be contained within the <html> element (before </html>). But, be sure that the Javascript appears after the <h3>. Javascript will run when it's encountered, as Marc said in a comment above.

If the Javascript must be before the , then do this:

window.onload=function(){
    document.getElementById("message").innerText="Email already exists";
};
Sign up to request clarification or add additional context in comments.

9 Comments

Most browsers will actually run the JavaScript even if it's placed outside of the </html> tag. DON'T DO IT.. But it will run.
Hi and thank you for your reply. i enclosed the entire code in <html> tags as suggested, however the problem remains. Code is printed (now before the closing html tag), yes still doesnt get executed.
Thanks, James. My other statement about how the JS should appear after the <h3> is still possibly the issue.
From the console, what happens if you execute document.getElementById("message").innerText="Email already exists";
Can you create your example at jsfiddle.net, copy/pasting in all your code, saving it and then giving us the link to it here?
|
1

Try it like this:

echo '<h3 id="message"> Email already exists!</h3>';

<!DOCTYPE html>
<html>
<body>
<form id="submitform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">       
    <input id="logIn_email_input" name="email" type="text" placeholder="Enter e-mail address" autocomplete="off">       
    <input id="logIn_password_input" name="password" type="password" placeholder="Enter password" autocomplete="off">       
    <input id="logIn_submit" type="submit" name="logIn_submit">SIGN UP</button>
</form>

<?php 
$query = mysql_query("SELECT userid FROM users WHERE email = '". $email ."'"); 

if (mysql_num_rows($query) > 0) {
    echo '<h3 id="message"> Email already exists!</h3>';
}
?>
<body>
</html>

You had a lot of issue here (maybe typos ?)

  • action="<?php echo $PHP_SELF;?>" should be action="<?php echo $_SERVER['PHP_SELF']; ?>"
  • <button id="logIn_submit" should be <input id="logIn_submit" type="submit" name="logIn_submit">
  • <? php had extra space should be <?php
  • If statement was missing closing brace }
  • No <body> tags

3 Comments

I have tried it and i see the code printed in the console.log(data), however the new <h3> element is missing from the elements page. In other words, its gets printed but is being ignored by the html page.
@Bdevelle can you post more code, what you showed look alright. Show where are you running this in your HTML
hi meda. the server echo and type=submit were indeed incorrectly written. I copied the code and placed on jsfiddle. it was impossible to split, but it should be easy enough to see. jsfiddle.net/hpSHa

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.