Php 使用Ajax和jQuery发布表单
开始学习Ajax(和jQuery)时,我遇到了post表单数据的问题,这是我自己无法解决的 首先,这里是一个简单的示例,我收集数据,发布数据,并将其显示在新页面上:Php 使用Ajax和jQuery发布表单,php,jquery,ajax,forms,post,Php,Jquery,Ajax,Forms,Post,开始学习Ajax(和jQuery)时,我遇到了post表单数据的问题,这是我自己无法解决的 首先,这里是一个简单的示例,我收集数据,发布数据,并将其显示在新页面上: <!-- index.html --> <form id="myForm" action="test.php" method="post"> <input type="checkbox" id="ch1" name="Ch1" value="1">Check Box 1 <br />
<!-- index.html -->
<form id="myForm" action="test.php" method="post">
<input type="checkbox" id="ch1" name="Ch1" value="1">Check Box 1 <br />
<input type="checkbox" id="ch2" name="Ch2" value="2">Check Box 2 <br />
<input type="submit" id="myButton" />
</form>
<?php
// test.php
print_r($_POST);
?>
我的问题是,如何重新实现jQuery函数以在test.php
选项卡中回显结果?我搜索了很多资源,但没有找到任何解决方案
提前感谢您的指点
最好的,Andrej我相信您正在
.ajax
选项参数中寻找成功
$.ajax({
...
success: function(d){
alert(d);
}
});
我相信您正在
.ajax
选项参数中寻找成功
$.ajax({
...
success: function(d){
alert(d);
}
});
您将需要一个
success
回调,我假设它将是一些HTML
$(document).ready(function(){
$("#myButton").click(function() {
$.ajax({
cache: false,
type: 'POST',
url: 'test.php',
data: $("#myForm").serialize(),
success: function(response) {
$("#someElement").html(response);
}
});
});
});
您将需要一个
success
回调,我假设它将是一些HTML
$(document).ready(function(){
$("#myButton").click(function() {
$.ajax({
cache: false,
type: 'POST',
url: 'test.php',
data: $("#myForm").serialize(),
success: function(response) {
$("#someElement").html(response);
}
});
});
});
试试这个
$(document).ready(function(){
$("#myButton").click(function() {
$.ajax({
cache: false,
type: 'POST',
url: 'test.php',
data: $("#myForm").serialize(),
success: function(data){
alert(data);
}
});
});
});
试试这个
$(document).ready(function(){
$("#myButton").click(function() {
$.ajax({
cache: false,
type: 'POST',
url: 'test.php',
data: $("#myForm").serialize(),
success: function(data){
alert(data);
}
});
});
});
什么“标签”?php是您的目标脚本。什么“选项卡”?test.php是您的目标脚本。但这将在my index.html中显示$u帖子。我想要的是打开test.php并在那里呈现结果。我想要的是打开test.php并在那里呈现结果,但这只会显示一条警告消息。我想要的是打开test.php并在那里呈现结果。我认为您没有理解AJAX的要点。如果您正在寻找要打开的窗口,那么AJAX就不是您的选择。AJAX用于“无缝集成”,在不离开页面的情况下执行任务。对于您尝试执行的操作,我建议使用默认的表单操作(并可能添加一个
target=“\u blank”
,以便它在一个新窗口中提交。好的,很好。您对它的措辞感到困惑。现在,只需将它绑定到一个元素(如
),并使用类似$('.\myElement').html(d);
(代替警报)。但这将只显示一条警报消息。我想要的是打开test.php并在那里呈现结果。我认为您没有理解AJAX的要点。如果您正在寻找要打开的窗口,那么AJAX就不是您的赌注。AJAX用于“无缝集成”,在不离开页面的情况下执行任务。对于您尝试执行的操作,我建议使用默认的表单操作(并可能添加一个target=“\u blank”
,以便它在新窗口中提交。好的,很好。您对它的措辞感到困惑。现在,只需将其绑定到元素(如
)并使用类似于$('#myElement').html(d);
(代替警报)的内容将结果放在那里。