dongwuxie7976 2015-02-26 19:07
浏览 37

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?

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP