Search code examples
javascriptgetjson

Can't call a JavaScript function on a php script


When I called the javascript function within the php script, it just called run() but not the function of the run(). Is there something wrong on my JavaScript or php script?

<script type= "text/javascript">

    function run(){
        $.getJSON("http://smartfypwindow.000webhostapp.com/api/led/update.php?id=1&status=Close", function(data) {
            console.log(data);
        });
    }
</script>

<?php

    $dbusername = "id3137057_windowdatabase"; 

    $dbpassword = "root123";  

    $server = "localhost"; 

    $My_db = "id3137057_window";

    // Connect to your database

    $dbconnect = mysqli_connect($server, $dbusername, $dbpassword);

    $dbselect = mysqli_select_db($dbconnect,"id3137057_window");

    $result = mysqli_query($dbconnect, "SELECT temp, hum, lum FROM 
    weather");

    while($row = mysqli_fetch_assoc($result)){

        if($row["temp"] < 27) {
            echo" run()";
        }
    }

    mysqli_close($dbconnect);

?>  


Solution

  • If you really want to go this approach...

    while($row = mysqli_fetch_assoc($result)){
    
        if($row["temp"] < 27) {
            echo" <script>run()</script>";
        }
    }
    

    This should work.