聊天表格不提交

时间:2015-04-17 23:43:32

标签: php html ajax

我正在进行php聊天,一切正常,但我想用ajax停止刷新页面。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
    $("#submit").on("click", function (event) {
        event.preventDefault();
        $.ajax({
            type: "POST",
            url: "index.php",
            data: $("#chat").serialize(),
            success: function () {
                alert('lorem ipsum');
            }
        });
    });
});
</script>

<form name="chat" action="" method="post" id="chat">
<b>Msg:</b>
<input type="text" name="msg" size="30" class="text">
<input type="submit" name="submit" value="Send!" class="bttn" id="submit">
<input type="hidden" name="lastcat" value="<?php echo $simplecat; ?>">
<input type="hidden" name="lastwas" value="<?php echo $command; ?>">
</form>

 <hr>
 <div class="leftalign">
  <b class="yousay">You say:</b> <?php echo stripslashes($usermessage); ?><br /><br />

 <b class="catsays">Cat replies:</b> <?php echo $catreply;?><br /><br />

我看到msg框并且页面不会刷新,但是我的$ _POST ['msg']是空的,所以我错过了什么?

1 个答案:

答案 0 :(得分:1)

你不清楚你的信息应该去哪里,但这应该在某种程度上修复它......

<强> page1.php中

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
    $("#submit").on("click", function (event) {
        event.preventDefault();
        $.ajax({
            type: "POST",
            url: "index.php",
            data: $("#chat").serialize(),
            success: function (response) {
                alert('lorem ipsum');
                // You need somewhere to drop the response into.
                // I've chosen the id "response"
                $("#response").html(response);
            }
        });
    });
});
</script>

<form name="chat" action="" method="post" id="chat">
<b>Msg:</b>
<input type="text" name="msg" size="30" class="text">
<input type="submit" name="submit" value="Send!" class="bttn" id="submit">
<input type="hidden" name="lastcat" value="<?php echo $simplecat; ?>">
<input type="hidden" name="lastwas" value="<?php echo $command; ?>">
</form>
<hr>
<div class="leftalign">
    <!-- I replaced your php response with where the AJAX should drop in -->
    <b class="yousay">You say:</b><div id="response"></div><br /><br />
    <b class="catsays">Cat replies:</b> <?php echo $catreply;?><br /><br />

<强>的index.php

<?php if(isset($_POST['msg'])) { print_r($_POST); return; } ?>