2013-05-12 31 views
0

我有這個表單(它是一個網絡民意調查),有收音機答案,我需要它時從動作文件(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> 
+1

哪裏是你已經厭倦了jqury Ajax代碼? – 2013-05-12 22:58:33

+1

http://www.w3schools.com/ajax/如果你是新人,這是一個非常好的開始,你不會regrat,你可以/應該補充它與一個驚人的網站稱爲stackoverflow;)cumpz – konnection 2013-05-13 00:30:07

+0

另一個地方學習jquery是Jeffery Ways 30天來學習Jquery視頻。 https://tutsplus.com/course/30-days-to-learn-jquery/ – Brad 2013-05-13 01:00:35

回答

0

如果你能夠使用jQuery的,你可以這樣做

$('#myform').submit(function(e){ 
    e.preventDefault(); 
    $.post('voting.php', {$(this).serialize()}, function(data){ 
     $('#form').html(data); 
    }) 
}); 
0

首先做任何你打算在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