简体   繁体   中英

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.

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!

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

You can Try the Heredoc Notation:

<?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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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