javascript - 使用 Post 方法和 Ajax 从一个表单接收数据到另一个表单
问题描述
我试图在处理文件中使用 post 方法获取数据,但我做不到。我需要通过 POST 方法接收数据并在 process.php 中显示我的数据 请记住,我的输入不在表单标签中。我只是想用 window.location.href 代码移动到相关页面。我认为当用户被JavaScript转移到处理页面时。帖子数据丢失
$(document).ready(function(){
$("#smp").click(function(event){
var name = $('#name').val();
$.ajax({
url: "process.php",
method: "POST",
data:name,
success: function (data) {
window.location.href = "process.php";
},
error: function (jqXHR, textStatus, errorThrown) { // the error will accure if process.php did not return a valid response (return)
alert(data);
alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
$('#result').html('<p>status code: ' + jqXHR.status + '</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>' + jqXHR.responseText + '</div>');
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
}
});
});
});
<div class="form"><label id="icon" for="name"><i class="fas fa-user"></i></label>
<input type="text" name="name" id="name" />
<div class="btn-block">
<button id="smp" type="submit" href="/">Submit</button>
</div></div>
我的 php 文件 http://sandbox.onlinephpfunctions.com/code/f379f5e2662300270c64ad867316ca268dd91640
解决方案
我在评论中解释了代码中发生的事情。您没有理由发送 JSON 请求,您在键中发送相同的值,而它的值毫无意义。
$(document).ready(function(){
$("#smp").click(function(event){
var name = $('#name').val();
$.ajax({
url: "process.php",
method: "POST",
dataType: 'text',
data:{"name" : name}, // here you are sending process.php name too. if it returns something then it should go th the success function and redirect.
success: function (data) {
window.location.href = "process.php?name=" + name; // if the function returned something then it will redirect it to "process.php?name=" + name
},
error: function (jqXHR, textStatus, errorThrown) { // the error will accure if process.php did not return a valid response (return)
alert(data);
alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
$('#result').html('<p>status code: ' + jqXHR.status + '</p><p>errorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>' + jqXHR.responseText + '</div>');
console.log('jqXHR:');
console.log(jqXHR);
console.log('textStatus:');
console.log(textStatus);
console.log('errorThrown:');
console.log(errorThrown);
}
});
});
});
现在,您的 HTML 代码不应该包含form
没有理由发送它!您只是发送 2 个具有相同数据的请求,因此我将其替换为以下代码:
<label id="icon" for="name"><i class="fas fa-user"></i></label>
<input type="text" name="name" id="name" />
<div class="btn-block">
<button id="smp" type="submit" href="/">Submit</button>
</div>
重要补充:如果您坚持离开表单请求而不重定向,您可以使用 .preventDefault();
推荐阅读
- jquery - JQuery 选项卡在 Jquery DataTables 的 Page2 上不起作用
- docker - Docker Django GPIO Rasbian buster
- pandas - 制作多索引数据框的 matplotlib 线图,每行具有不同的线,其索引用作 X 轴
- javascript - 如何使用从exports.default 函数传递的变量
- python - 将两列组合在一起作为列表数据类型,并从抑制 json 文件中过滤掉单词
- sql-server - 重新登录无法通过 t-sql xp_commandshell 工作,但相同的命令在命令提示符下工作
- eclipse - Create new instance of a project in intellij
- javascript - TypeError: _fieldRef$current.blur 不是函数
- firebase - Flutter/Firebase - type 'Future
' is not a subtype of type 'int' - c - Generate integer random number using available binaryrandom (which return 0 or 1) function