简体   繁体   English

如何在 PHP 中使用 document.getElementbyId

[英]how to use document.getElementbyId in PHP

i want to run javascript in PHP but this code isn't not working at all for me.我想在 PHP 中运行 javascript 但这段代码对我来说根本不起作用。

if(!mysqli_fetch_assoc($result))
    {
      echo '<script type="text/javascript">',
     'document.getElementById("console1").innerHTML += "Query did not work";',
     '</script>'  ;

    }

Replace those , with .替换那些,. to chain a string.链接一个字符串。

if(!mysqli_fetch_assoc($result))
    {
      echo '<script type="text/javascript">' . 
      'document.getElementById("console1").innerHTML += "Query did not work";' .
      '</script>';
    }

You can't use " in php, this worked for me!你不能在 php 中使用 ",这对我有用!

echo "<script> 
       document.getElementById('console1').innerHTML += 'Query did not work'; 
      </script>";

You can Try the Heredoc Notation:您可以尝试使用 Heredoc 表示法:

<?php
    echo<<<HTML
    .
    .
    <script type="text/javascript">
      document.getElementById("console1").innerHTML += "Query did not work";
    </script>
    HTML; 

//[*Note that echo<<<HTML and HTML; has to be on the very left side without blank spaces]

since it is done with ajax try something similar to this:因为它是用 ajax 完成的,所以尝试类似的东西:

<?php
    if(!mysqli_fetch_assoc($result)) {
        http_response_code(500);
        die(['error' => 'Query did not work!']);
    }
?>

and in your frontend code something like:并在您的前端代码中类似:

<script>
    $.get('your/query/path?query=...', function() {
        console.log('executed');
    }).fail(function(result) {
        $('#console1').append(result.error);
    });
</script>

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

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