PHP + Ajax / Javascript + Mysql

I trying control a "status" of 3 buttons using HTML/PHP + Ajax/javascript + Mysql.

I can click on button and:

  1. Button on 'div' tag and go to script java that manager the get to load;
  2. Update bd;
  3. Change the image that is my button (after query on altered bd);

There is my codes:

index.php: on head

<script>
    $(function () {
        $('#button div').click(function () {
            var nameDiv = $(this).attr('id');
            var last = nameDiv.charAt(nameDiv.length - 1)
            $("#" + nameDiv).load("change.php?num=" + last);
        });
    });
</script>

on body:

<div id="button">
    <div id="button1"><?php QueryStateButton(1); ?><br /></div>
    <div id="button2"><?php QueryStateButton(2); ?><br /></div>
    <div id="button3"><?php QueryStateButton(3); ?><br /></div>
</div>

my function:

    function QueryStateButton($num) {
        include 'conection.php';
        $query = 'SELECT * FROM button WHERE id =' . $num;
        $result = mysqli_query($link, $query);
        while ($row = mysqli_fetch_array($result)) {
            $place = $row['place'];
            $status = $row['status'];
        }
        if ($status == 1) {
            echo '<img src="image/on.png"><br />' . $place;
        } else {
            echo '<img src="image/off.png"><br />' . $place;
        }
        mysqli_close($link);
    }

another:

    function changeStatusButton($num) {
        $status = queryStatus($num);
        include 'conection.php';
        if ($status == 0) {
            $query = 'UPDATE button SET status = 1 WHERE id =' . $num;
        } else {
            $query = 'UPDATE button SET status = 0 WHERE id =' . $num;
        }
        $result = mysqli_query($link, $query);
        mysqli_close($link);

        QueryStateButton($num);
    }

Just scope:

    //take the status for button on bd
    function queryStatus($num) {
        ...
    }

change.php

    <?php
        $num = $_GET['num'];
        include 'functions.php';
        $resul = changeStatusButton($num);
    ?>

My problem is this:

I need be able to click image inside div and change the state of this button on bd. Same time I need the return this state via ajax/javascript and change the image on browser "alive".

For example: I click the button on my tablet and my son on PC see the change ON to OFF (or OFF to ON) without need to refresh the page.

How is this possible?

I don't want to use Node.js (because I think a very complex and cannot understood how to do this).

I saw for long_polling and comet strategies, but this is possible using php/html + ajax/javascript + mysql?

duanqiang6501
duanqiang6501 bd或txt中的计时器或监视事件(更改)?
5 年多之前 回复
dra8603
dra8603 (mtgcg.com.br/botoes.rar)上的文件如果想看...
5 年多之前 回复
doujiong2533
doujiong2533 所以在页面上然后你需要一个javascript计时器来触发定期ajax请求以加载新状态...
5 年多之前 回复
dskld5423
dskld5423 基本上bdok上的更改状态。当我点击图像bdok上的设置时,更改de值和ON和OFF确定。但是,如果没有页面刷新,其他设备中的其他人将无法看到...
5 年多之前 回复
dongqun1656
dongqun1656 我猜你已经从函数QueryStateButton内部打印了(你应该修复打开和关闭数据库两次,但是,这是性能问题,你只需要为每个请求打开一次)...所以也许你需要添加到这个质疑实际发生的事情......你得到了什么?
5 年多之前 回复
doumiebiao6827
doumiebiao6827 在change.php上打印/回显?可能的.load()示例以及如何使用$.ajax()?
5 年多之前 回复
dongyongyu0789
dongyongyu0789 首先,代替.load()使用带有cache:false和POST的$.ajax()或者你会遇到缓存问题,因为.load()正在使用GET(默认情况下会缓存)。其次,Ajax需要您在正在调用的页面中打印/回显某些内容,否则它无法更新前端。
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐