使用ajax触发php脚本-如何以及在哪里编程

Trigger a php script using ajax - how and where to program this?

本文关键字:在哪里 编程 ajax 触发 php 脚本 使用      更新时间:2023-09-26

你好,

我有一个php文件(db.php),其中包含以下函数

function edit_record($id, $value){
    if($this->db->query('UPDATE tbl_prototype SET value = ' . $value .' WHERE id_component = '.$id)){
        $this->register_changes();
        return TRUE;
    }
    return FALSE;
}

此外,我在我的html页面中有一些复选框,如下所示:

<input id="chk01" type="checkbox" data-onstyle="success" data-toggle="toggle">
<input id="chk02" type="checkbox" data-onstyle="success" data-toggle="toggle">

html页面还包含以下脚本。

<script>
        /* AJAX request to checker */       
        function check(){
            $.ajax({
            type: 'POST',
            url: 'checker.php',
            dataType: 'json',
            data: {
            counter:$('#message-list').data('counter')
        }
        }).done(function( response ) {
        /* check if with response we got a new update */
        if(response.update==true){
            var j = response.news;
            $('#message-list').html(response.news);
            sayHello(j);
        }           
        });
        };
        //Every 1/2 sec check if there is new update
        setInterval(check,500);
    </script>
    <script>
        function sayHello(j){
        var json=$.parseJSON(j);
        var techname = "";
        var techname1 = "";
        var c;
        var w;
        $(json).each(function(i,val){
        $.each(val,function(k,v){
        if (k=="tech_name")
        {
            techname = "#" + v;
            techname1 = v;
        }
        else
        {
            console.log("Mon nom est " + techname + " et ma valeur est " + v);
            c=document.getElementById(techname1);
            if (c.checked)
            {
                w = 1;
            }
            else
            {
                w = 0;
            }
            console.log(w);
            console.log("techname : " + techname1);
            if (v != w)
            {
                console.log ("Pas identique");
                if (v==0)
                {
                    // false
                    uncheckBox(techname);
                }
                else
                {
                    // true
                    checkBox(techname);
                }
            }
            else
            {
                console.log ("Identique");
            }
        }
        });
        });
        }
        function checkBox(pCtrl)
        {
            toggleOn(pCtrl);
        }
        function uncheckBox(pCtrl)
        {
            toggleOff(pCtrl);
        }
    </script>

现在我的问题是:我应该在哪里以及如何指定运行存储在带有两个参数($id和$value)的"db.php"文件中的函数"edit_record"。

"checker.php"的内容:

<?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']){
if(isset($_POST)){
    $data['news'] = $db->get_news2();
    $data['update'] = true;
}
//just echo as JSON
echo json_encode($data);
/* End of file checker.php */

非常感谢您的宝贵意见。如果这个问题听起来很傻,很抱歉(我是php/ajax/jquery编程的新手)。

在具有丰富界面的现代web应用程序中,您应该选择REST API并创建控制器,该控制器应在checker.php中的You case中。示例(checker.php):

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//update code
edit_record($_POST['id'],$_POST['counter]);
}
if ($_SERVER['REQUEST_METHOD'] == 'GET'){
//get code
}

ps。我看不到在ajax中传递id,您只发送计数器,所以您应该添加id,如:

...
data: {
        id:yourId //here your id
        counter:$('#message-list').data('counter')
    }

从js中删除的下一件事:

setInterval(check,500);

并创建绑定:

$("yourcheckboxselector").on("click",function(e){
 check($(this).prop("checked") ) //here you have it was checked or not as boolean
});