用php处理表格不刷新
问题描述:
我有这个表单(它是一个网络民意调查),有收音机答案,我需要它时从动作文件(voting.php)提交的信息打印在同一页上,无需刷新!请帮助我我一直努力工作了几个小时,因为我刚接触Ajax jQuery和Java以及所有棘手的动态内容。用php处理表格不刷新
<form name="myform" id="myform" action="voting.php" method="post">
<input id="radio1" type="radio" name="answer" value="answer1"><?php echo $answer1?><br>
<input id="radio2" type="radio" name="answer" value="answer2"><?php echo $answer2?><br>
<input id="radio3" type="radio" name="answer" value="answer3"><?php echo $answer3?><br>
<input id="radio4" type="radio" name="answer" value="answer4"><?php echo $answer4?><br>
<input id="radio5" type="radio" name="answer" value="answer5"><?php echo $answer5?><br>
<input id="questionId" type="hidden" name="questionId" value="<?php echo $questionId?>">
<center><input id="button" type="submit" value="Vote"></center>
</form>
答
如果你能够使用jQuery的,你可以这样做
$('#myform').submit(function(e){
e.preventDefault();
$.post('voting.php', {$(this).serialize()}, function(data){
$('#form').html(data);
})
});
答
首先做任何你打算在voting.php数据做。数据库无论如何,或者只是将其设置为以任何您想要的格式回显帖子。回声$ _ POST(“答案”)
那么,你的jQuery的可能看起来像这样
$(function() {
$('#myform').on('submit', function(){
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value){
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
});
$.ajax({
url: url,
type: type,
data: data,
success: function(responce){
$('#display').html(responce).delay(6000).fadeOut(1000);
}
});
return false;
});
});
的#display是div#显示你会显示从voting.php你的结果,不管你回音在voting.php将显示在#display
摆脱回声$回答的东西
+0
谢谢你的合作,我会稍后再试这个代码! 我不能摆脱echo $ answer1..5 cuz这些变量在表单之前定义,我从我的数据库中选择它们,所以这不是我想要的问题 – user2375910 2013-05-13 07:10:41
哪里是你已经厌倦了jqury Ajax代码? – 2013-05-12 22:58:33
http://www.w3schools.com/ajax/如果你是新人,这是一个非常好的开始,你不会regrat,你可以/应该补充它与一个惊人的网站称为*;)cumpz – konnection 2013-05-13 00:30:07
另一个地方学习jquery是Jeffery Ways 30天来学习Jquery视频。 https://tutsplus.com/course/30-days-to-learn-jquery/ – Brad 2013-05-13 01:00:35