简体   繁体   English

未执行PHP脚本回显

[英]PHP script echo isn't executed

I am printing a very simple JavaScript using PHP, which doesn't get executed. 我正在使用PHP打印一个非常简单的JavaScript,但未执行。 If I print the data, I see the following script (exactly as needed) at the end of the HTML file: 如果打印数据,则在HTML文件末尾会看到以下脚本(完全根据需要):

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

I have also tried using innerHTML="Email already exists"; 我也尝试过使用innerHTML="Email already exists"; .

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. 在我的HTML中,我有一个具有ID:消息的元素。 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. 我期望的是在h3中获取“电子邮件已经存在”,但是这不会发生。 The JavaScript is not executed. JavaScript未执行。 If I use the exact same JavaScript code but place it ahead or on an "onclick" request, the code works. 如果我使用完全相同的JavaScript代码,但是将其放在前面或“ onclick”请求上,则该代码有效。

One thing which could be relevant: I noticed that the JavaScript is printed after the closing HTML tag. 可能相关的一件事:我注意到JavaScript是结束HTML标记之后打印的。

How do i get the JavaScript code to execute after being echo'ed into the HTML? 在回显到HTML后,如何使JavaScript代码执行? 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. 我尝试了大约50种不同的修复程序,但均无效果。

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

You mentioned this: 您提到了这一点:

One thing which could be relevant. 一件事可能是相关的。 I noticed that the javascript is printed AFTER the closing html tag (the ). 我注意到javascript是在html标记()之后打印的。

That is very relevant. 那很相关。 Any Javascript must be contained within the <html> element (before </html> ). 任何Javascript必须包含在<html>元素中( </html>之前)。 But, be sure that the Javascript appears after the <h3> . 但是,请确保Javascript出现在<h3> Javascript will run when it's encountered, as Marc said in a comment above. 正如Marc在上面的评论中所说的那样,JavaScript将在遇到时运行。

If the Javascript must be before the , then do this: 如果Javascript必须位于之前,请执行以下操作:

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

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="<?php echo $PHP_SELF;?>"应该是action="<?php echo $_SERVER['PHP_SELF']; ?>"
  • <button id="logIn_submit" should be <input id="logIn_submit" type="submit" name="logIn_submit"> <button id="logIn_submit"应该为<input id="logIn_submit" type="submit" name="logIn_submit">
  • <? php <? php had extra space should be <?php <? php有多余的空间应该是<?php
  • If statement was missing closing brace } If statement缺少右括号}
  • No <body> tags 没有<body>标签

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

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