javascript - 如何让javascript在页面加载后发送ajax数据而无需按钮?

标签 javascript php jquery mysql ajax

我有一些数据想要通过ajax插入MySQL,到目前为止它只发送数据如果我包含一个带有提交的html表单并按下它,所有数据都会正确存储,但是如果我删除它并且设置超时,似乎没有发送数据。

$(document).ready(function (){
    $('#postingForm').submit(function ()
    {
        browserres  = screen.width + "x" + screen.height;
        var a= $(document).width(), b=$(document).height(), c=$(window).width(), d=$(window).height(), e=screen.width, f=screen.height;

        $(this).prepend('<input type="hidden" name="browserres" value="' + browserres + '">');
        $(this).prepend('<input type="hidden" name="id2" value="'+a+"x"+b+"X"+c+"x"+d+"X"+e+"x"+f+'">');
        $(this).prepend('<input type="hidden" name="user_agent" value="<?php echo $_SERVER['HTTP_USER_AGENT']; ?>"></input>');
        $(this).prepend('<input type="hidden" name="user_ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"></input>');

        $.post('insertData.php', { browserres, id2, user_agent, user_ip }, function(json) {
            if(json.outcome == 'success') {
                alert('GOOD!')
            } else {
                alert('Data not sent!');
            }
        },'json');
    });
});

<form id="postingForm" action="insertData.php" method="post">
    <input type="submit" vaue="submit"></input>
</form>

上面将数据发送到 insertData.php 文件并正确插入所有内容,但我想做的是删除 html 表单,并使其一旦用户登陆页面,它将等待页面已加载,然后它将发送数据,无需刷新页面..并且无需 html 表单..

我已经尝试了以下方法,但似乎无法使其工作..

也不工作

$(document).ready(function (){
    document.forms["myForm"].submit();

    function()
    {
        browserinfo = escape(JSON.stringify(fetch_client_info(), null, '\t'));
        browserres  = screen.width + "x" + screen.height;
        var a= $(document).width(), b=$(document).height(), c=$(window).width(), d=$(window).height(), e=screen.width, f=screen.height;

        $(this).prepend('<form id="postingForm" name="myForm" action="insertData.php" method="post">');
        $(this).prepend('<input type="hidden" name="browserinfo" value="' + browserinfo + '">');
        $(this).prepend('<input type="hidden" name="browserres" value="' + browserres + '">');
        $(this).prepend('<input type="hidden" name="id2" value="'+a+"x"+b+"X"+c+"x"+d+"X"+e+"x"+f+'">');
        $(this).prepend('<input type="hidden" name="user_agent" value="<?php echo $_SERVER['HTTP_USER_AGENT']; ?>"></input>');
        $(this).prepend('<input type="hidden" name="user_ip" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>"></input>');
        $(this).prepend('</form>');

        $.ajax(
        {
            type: "POST",
            url: "insertData.php",
            data: browserinfo, browserres, id2, user_agent, user_ip,
            success: function() {

            }
        });
        return false;
    };
});

最佳答案

尝试在文档准备好后立即放置 $.post 函数。

$(document).ready(function (){

    var browserinfo = escape(JSON.stringify(fetch_client_info(), null, '\t'));
    var browserres  = screen.width + "x" + screen.height;
    var a= $(document).width(), b=$(document).height(), c=$(window).width(), d=$(window).height(), e=screen.width, f=screen.height;

    var data = {
       browserinfo: browserinfo,
       browserres: browserres,
       // ... etc for all your data
    }

    $.post('insertData.php', data, function(json) {
            if(json.outcome == 'success') {
                alert('GOOD!')
            } else {
                alert('Data not sent!');
            }
        },'json');
});

关于javascript - 如何让javascript在页面加载后发送ajax数据而无需按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32613760/

相关文章:

php - 法尔康 PHP : Update a primary natural key

php - 增加内存限制

javascript - 无法使用哈希语法从 JS 对象检索值

javascript - 将 Summernote 文本传递给 ajax

javascript - 如何使用 jquery 监听特定按钮

javascript - 如何在angularjs中的指令中注册点击事件

javascript - 检测浏览器是否为 Android 的最轻量级方法是什么?

javascript - IE6 中嵌套 JSON 会导致问题

javascript - 如何将数组中的对象分配给箭头函数?

javascript - 如何在 JavaScript 中调用 "disableRightClick"函数