如何在PHP中使用document.getElementbyId

时间:2017-07-19 19:02:34

标签: javascript php

我想在PHP中运行javascript,但这段代码对我来说根本不起作用。

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

    }

4 个答案:

答案 0 :(得分:2)

,替换为.以链接字符串。

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

答案 1 :(得分:1)

您可以尝试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]

答案 2 :(得分:0)

你不能使用&#34;在PHP中,这对我有用!

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

答案 3 :(得分:0)

因为它是用ajax完成的,所以尝试类似的东西:

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

并在你的前端代码中:

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