开发者

Strange ajax submission issue

I'm using AJAX to submit my forms. Button with id #submit_btn and checkbox with id #autosave are doing the same things. Button works properly but autosave doesn't. 开发者_StackOverflow社区What's wrong with the code?

I noticed that, when i click the submit button the log shows me 2 post's and succesfully adds into db

Strange ajax submission issue

But when i check #autosave checkbox, log shows only 1 post in every 5 seconds and doesn't update db table (BUT shows success message, and no error in log)

Strange ajax submission issue

Js side

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 side

<?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 "}";  
}

?>


I don't know for sure that this is the only issue, but int is a reserved word in older specifications of javascript. Pick a real variable name instead of that. It's also safer to protect against calling clearInterval() with junk for a parameter.

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;
        }
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜