javascript - 处理没有重定向的 PHP post

标签 javascript php jquery

我希望将 html 表单发布到 php 脚本,但我不想实际将用户重定向到此脚本。我所需要的只是让脚本获取发布数据,用它做一些事情,然后以某种方式返回该数据,以便调用脚本可以使用它。这有可能吗?

我能想到的唯一方法是使用一个可以向自身发布的 php 脚本,但它非常困惑,因为我在调用脚本中使用 jQuery 和 JavaScript。

提前致谢

最佳答案

使用 JQuery Ajax。

假设您有这样的表单

<form id="Form" action="response.php" method="post">...</form>

JQuery:

$('#Form').submit(function (event){
        $.ajax({
            type:"POST", // define method post or get
            url:$('#Form').attr('action'), // gets the post url from your action attribute
            data:$('#Form').serialize(), // binds your form data
            dataType:'json', // server response data type, use 'html' 'json' etc
            encode:true,
            success:function(data){
            },
            error:function( xhr, status, errorThrown ) {
            alert( "Sorry, there was a problem!" );
            console.log( "Error: " + errorThrown );
            console.log( "Status: " + status );
            console.dir( xhr );

            },
        });
         event.preventDefault(); // prevents from going to the posting url
    });

关于javascript - 处理没有重定向的 PHP post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30219987/

相关文章:

php - MySQL SELECT where IN (关联数组[key]) AND SUM(Col5) IN >= (数组[key2])

带有 URL 重写的页面上的 PHP 函数?

javascript - 创建广告小部件

jquery - 如何使用预选复选框监视复选框状态

javascript - 防止保存html内容的多个副本

javascript - Tumblr API 获取 'Uncaught TypeError: Cannot read property ' 类型“未定义”

javascript - 在 url 中包含数据的方法

javascript - 如何仅使用 javascript 使 div 随页面向下滚动

php - 如何使用 discord API 从用户名获取 discord 用户 ID

javascript - 如何验证表单然后发出 post http 请求 jQuery