php - 奇怪的ajax提交问题

标签 php jquery mysql ajax

我正在使用 AJAX 提交我的表单。 ID 为#submit_btn 的按钮和ID 为#autosave 的复选框正在做同样的事情。按钮工作正常,但自动保存不工作。代码有什么问题?

我注意到,当我点击提交按钮时,日志显示了 2 个帖子并成功添加到数据库中

enter image description here

但是当我选中#autosave 复选框时,日志每 5 秒只显示 1 个帖子并且不更新数据库表(但显示成功消息,日志中没有错误) enter image description here

Js端

var int = null;
var counter = 0;
function call() {
    postViaAjax(true)
}


function postViaAjax(autosaveMode) {
dataString = $("#add_form").serialize();
        $.ajax({
            type: "POST",
            url: "processor/dbadd.php",
            data: dataString,
            dataType: "json",
            success: function (result, status, xResponse) {
                var message = result.msg;
                var err = result.err;
                var now = new Date();
                if (message != null) {
                    if(autosaveMode){
                    $('#submit_btn').attr({
                    'value': 'Yadda saxlanıldı '+ now.getHours()+':'+now.getMinutes()+':'+now.getSeconds()
                    });
                    }
                    else{
                    $.notifyBar({
                        cls: "success",
                        html: message+' '+ now.getHours()+':'+now.getMinutes()+':'+now.getSeconds()
                    });
                    }
                }
                if (err != null) {
                    $.notifyBar({
                        cls: "error",
                        html: err
                    });
                }
            }
        });
};




$(document).ready(function () {
    $('.autosave').hide();
    $("#add_form").submit(function (e) {
        postViaAjax(false)
        e.preventDefault();
    });


    $("#submit_btn").click(function () {
        if (counter === 0) {
            if (validate()) {
                $('.autosave').show();
                counter++;
            }
        }
        $('#add_form').submit();
    });

    $('#autosave').click(function () {
        if ($(this).is(':checked')) {
            clearInterval(int);
            int = window.setInterval(call, 5000);

        } else {
            $('#submit_btn').attr({
                'value': 'Yadda saxla'
            });
           clearInterval(int);
        }
    });

});

PHP 端

<?php
require '../../core/includes/common.php';

$name=filter($_POST['name'], $db);
$id=filter($_POST['id'], $db);
$title=filter($_POST['title'], $db);
$parentcheck=filter($_POST['parentcheck'],$db);
if(isset ($_POST['parent'])) $parent=filter($_POST['parent'],$db);
else $parent=$parentcheck;  
$menu=filter($_POST['menu'], $db);
$content = $db->escape_string($_POST['content']);


$result=$db->query("UPDATE pages AS p, menu AS m SET m.parent='$parent', m.name='$name', m.showinmenu='$menu', p.id='$id', p.title='$title', p.content='$content' WHERE m.id='$id' AND p.id=m.id") or die($db->error);


if ($result){  
echo "{";  
echo '"msg": "Qeydə alındı" ';
echo "}";  
}else{ 
echo "{"; 
echo
'"err": "error"';  
echo "}";  
}

?>

最佳答案

我不确定这是唯一的问题,但 int 是一个 reserved word in older specifications of javascript .选择一个真正的变量名而不是那个。防止使用垃圾参数调用 clearInterval() 也更安全。

var autoSaveInterval = null;

$('#autosave').click(function () {
    if ($(this).is(':checked')) {
        if (autoSaveInterval) {
            clearInterval(autoSaveInterval);
        }
        autoSaveInterval = window.setInterval(call, 5000);

    } else {
        $('#submit_btn').attr({
            'value': 'Yadda saxla'
        });
        if (autoSaveInterval) {
            clearInterval(autoSaveInterval);
            autoSaveInterval = null;
        }
    }
});

关于php - 奇怪的ajax提交问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7654782/

相关文章:

php - Joomla:是否可以在没有 iframe 和插件的情况下显示组件的 View ?

php - 我的 jQuery .ajax 脚本没有将 html 放在 div 中

javascript - 隐藏和替换 html 内容和数据属性值

javascript - Rails 通过 ajax 发出闪光通知

javascript - jQuery offset() 在 Safari/Chrome iOS 中不起作用

mysql - 如何更正已删除行的自动增量字段(1,2,3,4,5 - 现在是 1,3,5),但我希望它是 1,2,3

javascript - 如何将json数据发送到codeigniter View

java - 如何检查数据库事务中的数据?

php - 通过 AJAX 将数组从 php 传递到 javascript

php - 查找借用特定 CD 的所有客户的姓名和电话号码