Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ajax脚本从表到html页面_Php_Mysql_Ajax - Fatal编程技术网

Php 使用ajax脚本从表到html页面

Php 使用ajax脚本从表到html页面,php,mysql,ajax,Php,Mysql,Ajax,下午好 基本上,我正在尝试改编Eliza Witkowska的伟大剧本,我正在努力,因此我今天在stackoverflow上发表了一篇文章 在我的html文件中,我有3个复选框(chk1、chk2和chk3)。我想要的是通过链接mysql表“tbl_prototype”的技术_name_html,从中检索它们的值,并更新3个复选框,如下所示: a) if the value is *100* -> check the tickbox b) if the value is *0* ->

下午好

基本上,我正在尝试改编Eliza Witkowska的伟大剧本,我正在努力,因此我今天在stackoverflow上发表了一篇文章

在我的html文件中,我有3个复选框(chk1、chk2和chk3)。我想要的是通过链接mysql表“tbl_prototype”的技术_name_html,从中检索它们的值,并更新3个复选框,如下所示:

a) if the value is *100* -> check the tickbox
b) if the value is *0* -> uncheck the tickbox
我正在尝试使用ajax查询来实现这一点,但我不太确定从哪里开始

你有什么建议给我,让我可以继续这个美好的项目吗

我可以回答任何问题

非常感谢您的帮助,祝您度过愉快的一天。 洛朗

我的文件“index.html”的内容

我的PHP文件“checker.PHP”的内容:


乍一看,我可以看到jquery代码必须在documentready函数中编写,如下所示

/*对检查器的AJAX请求*/

  $(document).ready(function(){
        $.ajax({
            type: 'POST',
            url: 'checker.php',
            dataType: 'json',
            data: {
                counter:$('#message-list').data('counter')
            }
        }).done(function( response ) {
            /* update counter */
            $('#message-list').data('counter',response.current);
            /* check if with response we got a new update */
            if(response.update==true){
                $('#message-list').html(response.news);
            }
        });
    }
});

到目前为止,基本脚本是否有效?你有什么错误吗?基本概念似乎很简单:1。使用AJAX查找是否进行了新的更改。2.如果是这样,则以JSON格式响应这些更改并更新计数器。听起来你需要3方面的帮助。解码JSON数据并使用jQuery根据JSON响应中的数据更新复选框。发布的基本脚本正在运行。不幸的是,它只显示了一些数据,并没有像我希望的那样选中复选框。更改checker.php以从表中查找真正需要的数据并对其进行回显。2.更改index.php以从checker.php获取响应,并使用javascript相应地更新您的复选框。查看此处以获取有关php、AJAX和mySQL的帮助:查看此处以获取有关javascript的帮助:如果这个问题听起来可能很傻,那么很抱歉,但是如何将checker.php的响应与复选框链接?谢谢你的帮助。非常感谢,非常感谢。你认为我需要在某个地方做一些特殊的事情来更新我的复选框吗?AJAX代码不必在document ready函数中,但是将它放在那里可以确保在AJAX调用之前加载页面。在这种情况下,每500毫秒通过行
setInterval(检查,500)进行一次。删除这一行将删除Laurent试图调整的脚本的全部功能。此外,您将看到此脚本按编写的方式工作,但Laurent尚未调整它以实现其目标。
CREATE TABLE `tbl_prototype` (
  `id_component` int(11) NOT NULL,
  `technical_name_html` varchar(10) NOT NULL,
  `component_name` varchar(20) NOT NULL,
  `description` varchar(50) NOT NULL,
  `value` tinyint(3) UNSIGNED NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  INSERT INTO `tbl_prototype` (`id_component`, `technical_name_html`, `component_name`, `description`, `value`) VALUES
  (1, 'chk1', 'light_living_room', 'The light of the living room', 0),
  (2, 'chk2', 'light_entrance', 'The light of the entrance', 100),
  (3, 'chk3', 'light_kitchen', 'The light of the kitchen', 0);

ALTER TABLE `tbl_prototype`
ADD PRIMARY KEY (`id_component`);
ALTER TABLE `tbl_prototype`
MODIFY `id_component` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?php require('common.php');
//get current counter
$data['current'] = (int)$db->check_changes();
//set initial value of update to false
$data['update'] = false;
//check if it's ajax call with POST containing current (for user) counter;
//and check if that counter is diffrent from the one in database
if(isset($_POST) && !empty($_POST['counter']) && (int)$_POST['counter']!    =$data['current']){
//the counters are diffrent so get new message list
$data['news'] = '<h1>OMG! It\'s alive!!! NEW UPDATE !!!</h1>';
$data['news'] .= $db->get_news();
$data['update'] = true;
}
//just echo as JSON
echo json_encode($data);

/* End of file checker.php */
<?php
require_once ('db.php'); //get our database class
$db = new db();
/* end of file common.php */
<?php
/**
 * Class db for Ajax Auto Refresh - Volume II - demo
 *
 * @author Eliza Witkowska <kokers@codebusters.pl>
 * @link http://blog.codebusters.pl/en/entry/ajax-auto-refresh-volume-ii
 */
class db{

/**
 * db
 *
 * @var $   public $db;
 */
public $db;


/**
 * __construct
 *
 * @return void
 */
function __construct(){
    $this->db_connect('192.168.0.XY',user','1234','database');
}


/**
 * db_connect
 *
 * Connect with database
 *
 * @param mixed $host
 * @param mixed $user
 * @param mixed $pass
 * @param mixed $database
 * @return void
 */
function db_connect($host,$user,$pass,$database){
    $this->db = new mysqli($host, $user, $pass, $database);

    if($this->db->connect_errno > 0){
        die('Unable to connect to database [' . $this->db->connect_error . ']');
    }
}


/**
 * check_changes
 *
 * Get counter value from database
 *
 * @return void
 */
function check_changes(){
    $result = $this->db->query('SELECT counting FROM news WHERE id=1');
    if($result = $result->fetch_object()){
        return $result->counting;
    }
    return 0;
}


/**
 * register_changes
 *
 * Increase value of counter in database. Should be called everytime when
 * something change (add,edit or delete)
 *
 * @return void
 */
function register_changes(){
    $this->db->query('UPDATE news SET counting = counting + 1 WHERE id=1');
}


/**
 * get_news
 *
 * Get list of news
 *
 * @return void
 */
function get_news(){
    //if($result = $this->db->query('SELECT * FROM news WHERE id<>1 ORDER BY add_date DESC LIMIT 50')){
    if($result = $this->db->query('SELECT * FROM tbl_prototype')){
        $return = '';
        while($r = $result->fetch_object()){
            $return .= '<p>id: '.$r->id_component.' | '.htmlspecialchars($r->description).' | '. $r->value . ' | ' .  $r->technical_name_html . '</p>';
            $return .= '<hr/>';
        }
        return $return;
    }
}


/**
 * add_news
 *
 * Add new message
 *
 * @param mixed $title
 * @return void
 */
function add_news($title){
    $title = $this->db->real_escape_string($title);
    if($this->db->query('INSERT into news (title) VALUES ("'.$title.'")')){
        $this->register_changes();
        return TRUE;
    }
    return FALSE;
}
}
/* End of file db.php */
  $(document).ready(function(){
        $.ajax({
            type: 'POST',
            url: 'checker.php',
            dataType: 'json',
            data: {
                counter:$('#message-list').data('counter')
            }
        }).done(function( response ) {
            /* update counter */
            $('#message-list').data('counter',response.current);
            /* check if with response we got a new update */
            if(response.update==true){
                $('#message-list').html(response.news);
            }
        });
    }
});