问题描述
我不知道有关websocket进行聊天的信息,我想使用php ajax进行表单提交以进行聊天我想在不重新加载的情况下动态地求和表格,但是它会重新加载(我不想要).我创建了一个聊天室,其中php将信息发送到xml并显示所有xml信息,并且当用户在下面提交表单时
i have no knowledge about websocket for chat i want to use php ajax for form submit for chati want to sumbit form dynamically without reload but it gets reload ( which i dont want ).i have created a chat in which php sends information to xml as and displays all xml information, and when user submits the form below
<form action="action.php" method="post" id="formpost">
<input type="text" id="input" value="php echo">
<input type="submit" value="send">
</form>
它重新加载以显示此php
it reloads to display this php
<div class="msg"><?php print $message->getName() ." : " . $chat->message . ""; ?></div>
其他信息:当我删除聊天室$chat->message .
时,没有显示任何消息,因为php循环中的唯一名称显示在上面的<div class="msg">
Additional info : when i remove the chat $chat->message .
no msgs display because the php loop only name shows in <div class="msg">
above
我已经尝试过使用javascript动态提交表单,但是当我单击该按钮时,我自己的html <html><body>..</html>
会发出警报,并且当我手动重新加载msg显示的页面时会显示
i have tried this to submit form dynamically by javascript but when i click the button a alert comes with my own html <html><body>..</html>
, and when i reload the page manually msg shows
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#formpost").on('submit', function(event){
event.preventDefault();
var form = $(this);
var url = form.attr('action');
$.ajax({
type: "POST",
url: "club.php",
data: form.serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
});
</script>
推荐答案
$(function() {
$("#formpost").submit(function(event) {
event.preventDefault();
$.ajax({
type: "POST",
url: "club.php",
data: $(this).serialize(),
}).done(function(data) {
var msg = $(data).find('#msg').text();
alert(msg);
});
});
});
这篇关于php ajax表单动态提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!