0

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?

5
  • Parse your data using $.JSON.parse(data) Commented Nov 16, 2015 at 3:57
  • what's in the $a ? Commented Nov 16, 2015 at 3:59
  • $a is my array, used for keeping my last msg Commented Nov 16, 2015 at 4:02
  • Where did you pushed value into $a ? @MartinDubois Commented Nov 16, 2015 at 4:05
  • with unshift, i tryed with push also but i prefer from beginning of my array Commented Nov 16, 2015 at 4:06

1 Answer 1

2

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'];
Sign up to request clarification or add additional context in comments.

2 Comments

what's problem in $("#catbot").serialize() + "&d=d" as op ha catching only msg ?
data can be sent both object and name value pair

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.