php - 提交按钮后的ajax后台进程

标签 php jquery ajax submit

我发布的脚本工作正常,它只是在页面加载时在后台执行我的 php 代码。我不能做的是在按下提交按钮后在后台启动 php。我试图添加一个在正文中形成,但我不知道如何将它与 ajax 链接。怎么办? 谢谢!

<html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    $.ajax({
        url: 'trial.php',
        beforeSend: function() {
            // you could also put a spinner img or whatever here too..
            $("#myStatusDiv").html("started..");
        },
        success: function(result) {
            $("#myStatusDiv").html(result);
        }
    });
  });
</script>

</head>
<body>

</body>                                                                                                             

</body>                                     
</html>

最佳答案

当文档准备好加载时,您当前正在执行代码。您应该将此代码添加到按钮单击事件中。

所以不是这个:

$(document).ready(function() {
    //your code
});

这样做:

$(document).ready(function() {
    $('#buttonid').click(function() {
        //your code
    });
});

关于php - 提交按钮后的ajax后台进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10166155/

相关文章:

php - 将 CSS 存储在数据库中

php显示所有不重复的图像

jquery - 循环作为背景图像不起作用

ajax - 完全 Ajax 应用程序的基于内容的广告

javascript - 我有一个 php 文件,它提供 JSON 输出,并希望在 D3 中使用该 JSON

php - 从不同的网络获取网络数据

javascript - 使文本与滚动窗口同时加载

php - 使用来自 jquery 数据表的 ajax 调用发送参数

javascript - 必须按 2 次提交按钮才能实际提交 - AJAX

IE9 上的 jQuery ajax 问题