2013-06-03 46 views
0

你好,我想問我怎麼可以從Javascript後發佈HTML打印$ POST值?

這裏是我的HTML提交形式:

<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script type="text/javascript" src="./comments.js"></script> 
</head> 
<div id="addCommentContainer"> 
<form class="add-comment-form" id="addCommentForm3" method="post" action=""> 
<input type="hidden" value="3" name="comentonpost" id="comentonpost" /> 
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea> 
<input type="button" class="add-comment-submit" value="Submit" /> 
</form> 
</div> 
<div id="addCommentContainer"> 
<form class="add-comment-form" id="addCommentForm3" method="post" action=""> 
<input type="hidden" value="3" name="comentonpost" id="comentonpost" /> 
<textarea class="commentinput" name="body" id="body" cols="20" rows="5"></textarea> 
<input type="button" class="add-comment-submit" value="Submit" /> 
</form> 
</div> 

這裏是我的javascript:

$(document).ready(function() { 

$(".add-comment-submit").on("click", function (e) { 



    e.preventDefault(); 



    var submit = $(this); 



    if (submit.hasClass("working")) return false; 



    submit.addClass("working"); 

    submit.val("Working..."); 



    $.post('submit.php',$(this).serialize(),function(msg){ 

     submit.removeClass("working"); 

     submit.val("Submit"); 

     $("<div></div>").html(msg).fadeIn("slow").insertBefore(submit.parent($(".addCommentContainer"))).slideDown(); 

    }); 

}); 

}); 

這是我submit.php正在打印的 「Hello World」 提交後:

<?PHP 
$message = 'Hello World'; 
echo $message; 
?> 

我不知道爲什麼,但現在看來,這不是張貼在submit.php的信息還是我在錯誤的地方,因爲當我嘗試這submit.php:

<?PHP 
$message = $_POST["body"]; 
echo $message; 
?> 

這不是印刷是什麼在名稱爲「body」的文本區域中鍵入。我如何讓它打印什麼是寫在身體輸入文本區域?

在此先感謝!

+0

你的ids應該是唯一的一個頁面..爲什麼你重複相同的形式兩次? – swapnesh

+0

因爲我有許多形式,它必須被提交併且只在提交表單之前發佈結果。 –

+0

在你的'submit.php'中,print_r($ _ POST)是什麼;'print? – MaX

回答

0

您在觸發器內部使用this,但觸發器位於按鈕上,而不是表格,所以this引用了該按鈕。這也意味着你正在序列化按鈕而不是表單。試試這個POST呼叫接續:

$.post('submit.php',$(this).parent().serialize(), ... 

編輯:

順便說一句,它通常是更好的做法觸發添加到形式,例如如解釋here

相關問題