PHP脚本回显未执行

时间:2021-04-24 01:23:40

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:

我正在使用PHP打印一个非常简单的JavaScript,它不会被执行。如果我打印数据,我会在HTML文件的末尾看到以下脚本(完全符合要求):

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

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

我也尝试过使用innerHTML =“已经存在电子邮件”;

This is printed in PHP as such:

这是用PHP打印的:

    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:

在我的HTML中,我有一个具有ID:消息的元素。它看起来像这样:

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

我期待的是在h3中获得“电子邮件已存在”,但这不会发生。 JavaScript未执行。如果我使用完全相同的JavaScript代码但是将其置于前面或“onclick”请求上,则代码可以正常工作。

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

有一点可能是相关的:我注意到在关闭HTML标记之后打印了JavaScript。

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.

如何在回显到HTML后执行JavaScript代码?我已经阅读了几个说它应该简单运行的线程,但是我没有。我尝试过大约50种不同的修复方法,但都没有。

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

代码:http://ideone.com/dmR42O

2 个解决方案

#1


1  

You mentioned this:

你提到了这个:

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

有一点可能是相关的。我注意到在关闭html标签之后打印了javascript()。

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.

这非常重要。任何Javascript都必须包含在元素中(在 之前)。但是,请确保在

之后出现Javascript。正如Marc在上面的评论中所说,Javascript会在遇到它时运行。

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

如果Javascript必须在之前,那么执行以下操作:

window.onload=function(){
    document.getElementById("message").innerText="Email already exists";
};

#2


0  

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']; ?>"
  • action =“ ”应该是action =“ ”

  • <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
  • 没有标签

#1


1  

You mentioned this:

你提到了这个:

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

有一点可能是相关的。我注意到在关闭html标签之后打印了javascript()。

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.

这非常重要。任何Javascript都必须包含在元素中(在 之前)。但是,请确保在

之后出现Javascript。正如Marc在上面的评论中所说,Javascript会在遇到它时运行。

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

如果Javascript必须在之前,那么执行以下操作:

window.onload=function(){
    document.getElementById("message").innerText="Email already exists";
};

#2


0  

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']; ?>"
  • action =“ ”应该是action =“ ”

  • <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
  • 没有标签