duanhui1869 2015-11-16 03:56
浏览 20
已采纳

数组不使用ajax帖子

everything work good except my array. when i click few time on sent the count($a) stay at 1. $catbotreply is the reply of my msg.

$a=array();
array_unshift($a,$catbotreply); 

 <script>
    $(document).ready(function () {
        $("#submit").on("click", function (event) {
            event.preventDefault();
            $.ajax({
                type: "POST",
                url: "index.php",
                data: $("#catbot").serialize() + "&d=d",
                success: function (data) {
                    console.log(data);
                    $('#catbotResult').html(data);
                }
            });
        });
    });
</script>



 if (isset($_POST['msg'])) {





   $response.= ' <b class="yousay">You say:</b>' . stripslashes($usermessage) . '<br /><br />';
$response.= '<b class="catbotsays">Bot:</b>' . $catbotreply . '<br /><br />';
 $response.= count($a);              
echo $response;
}






     <form name="catbot" id="catbot" action="<?php echo($_SERVER["PHP_SELF"]); ?>" method="POST">
                    <b>Msg:</b>
                    <input type="text" name="msg" size="30" class="text">
                    <input type="submit" id="submit" name="submit" value="Send!" class="bttn">
                    <input type="text" name="lastcatbot" value="<?php echo $simplecatbot; ?>">
                    <input type="text" name="lastwascomm" value="<?php echo $command; ?>">
                </form>

                <hr>
                <div class="leftalign" id="catbotResult">
                    <b class="yousay">You say:</b>Connected!<br /><br />
                    <b class="catbotsays">Bot:</b>Connected!<br /><br />
                </div>

before i add the ajax function i was using a hidden text to save my last msg and i understand if i use echo in the same page the data won't refresh so its why i added the count($a) to $response. my msg and bot reply refresh at every msg but my count stay at 1 . anyone know why?

  • 写回答

1条回答 默认 最新

  • dqysi86208 2015-11-16 03:59
    关注

    You should change the way you are sending the data in ajax.

    data: {catbot:$("#catbot").serialize() ,d:"d"},
    

    And receive as

    $_POST['catbot'];
    $_POST['d'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?