2016-06-15 150 views
0

因此,目前我有一個簡單的形式,發送sendchat.php消息人要鍵入..問題是我真的不想使用iframe使其工作無刷新..有人可以用正確的代碼指出我如何在BACKGROUND中執行表單提交,意味着不會進入其他頁面或刷新。GET請求使用窗體和按鈕

<form target="chat" action="sendchat.php" method="GET"> 
    <input type="text" class="form-control" id="message" maxlength="255" name="message" type="submit" placeholder="Enter your message or use !help for help."><br> 
    <div class="col-xs-6 text-right"> 
     <button type="submit button" class="btn bg-teal-400 btn-labeled btn-labeled-right"><b><i class="icon-circle-right2"></i></b> Send Message</button> 
    </div> 
    </form> 

<style> .iframe { display: none; border-color: rgba(225, 225, 225, 0); border-width: 0px; } </style> 
     <iframe style="" name="chat" width="0px" height="0px" id="chat" onload="clearTextarea();"></iframe> 
+2

獲得自動更新而不刷新。你需要在你的網站中實現AJAX。看到這裏:http://stackoverflow.com/questions/16616250/form-submit-with-ajax-passing-form-data-to-php-without-page-refresh –

回答

0

您可以使用ajax在單擊按鈕時獲取發佈數據而無需刷新頁面。

<form target="chat" action="sendchat.php" method="GET"> 
<input type="text" class="form-control" id="message" maxlength="255" name="message" placeholder="Enter your message or use !help for help."><br> 
<div class="col-xs-6 text-right"> 
    <button type="button" class="btn bg-teal-400 btn-labeled btn-labeled-right"><b><i class="icon-circle-right2" onclick="save_chat()"></i></b> Send Message</button> 
</div> 
</form> 
function save_chat(){ 
$.ajax({ 
    type: 'POST', 
    url: 'sendchat', 
    data: {'message': $('input[name="message"]').val()}, 
    success: function (data) 
    { 
     //do nothing 
    }, 
    error: function (xhr, ajaxOptions, thrownError) 
    { 
     alert(xhr.status); 
     alert(thrownError); 
}); 
} 
+0

僅供將來參考,你能證實我會如果我想要一個以上的數據例​​如消息和用戶的話。 – UKTSLiam

+0

只需將您的變量添加到數據中,以逗號分隔:data({'message':$('input [name =「message」]')。val(),'user':「me」}, – kiong