weixin_33697898 2015-04-17 23:43 采纳率: 0%
浏览 12

聊天表格未提交

i'm working on a php chat and everything work good but i wanted ajax for stoping the refresh page.

<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 />

i see the msg box and the page wont refresh but my $_POST['msg'] is empty so what i missed?

  • 写回答

1条回答 默认 最新

  • weixin_33719619 2015-04-18 00:01
    关注

    You aren't very clear where your message should go, but this should fix it...to some degree:

    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; } ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度