dongtiao1817 2014-03-22 23:13 采纳率: 100%
浏览 59

从Javascript调用PHP函数不起作用

I am working on a posting system in PHP. The posting system goes through a SQL database and then prints out data from the database. I am trying to use javascript to see if the system has been updated. Basically, I want to use javascript to check whether any new posts have been added. I have some code that goes into the database

function minicheck (){
    $query= "SELECT * FROM posts";
    $result= mysql_query($query);
    return mysql_num_rows($result);
}

The function returns the number of rows in the database every 10 seconds.I use javascript to call this function.

<?php
     echo(" <script>
        killPage();
        function killPage () {
      setInterval(function(){
    x++;
    alert(".minicheck().");
    }, 10000);
     }
   </script> ");
?>

THe issue i am facing is that the function returns the exact same value no matter whether or not a row has been added into the sql databse. Even if I add a row into the sql database, the value that is returned by this function is the exact same every time.

How can I fix this?

Thank You,.

  • 写回答

1条回答 默认 最新

  • douhuo1738 2014-03-22 23:31
    关注

    You cannot call PHP functions from JavaScript like that.

    What you are doing is calling the function once when building your JavaScript, which is then being sent to the browser.

    The browser only ever gets the static value you've put in the string, so naturally it can never update.

    If you want to call something in PHP from JavaScript, you'll probably want to use XMLHttpRequest (or any of the readily available libraries like jQuery that can help w/AJAX) to query a PHP file that runs the function and returns the value.

    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏