开发者

Php array is destroyed and replaced each time I make an ajax request to it?

At the click of a button an ajax POST is made to my php script which has the following code

<?php
    $number = $_POST["id"];
    $myarray[$number] = $_POST["marker"];
?>

The two POST entries are id and marker. I was hoping that each click of the button by the end user would construct an increasingly larger array called $myarray, because $number usually changes.

Instead what happens is each click of the button destroys the original $myarray and creates a new $myarray with only one data pair (the newly sent $number and $_POST["marker"]).

How can I code it so the array is built up with each click of the button?开发者_运维百科


HTTP is a stateless protocol, so there is no way for the server to know about $my_array after each request. PHP just generates some HTML and the server serves that generated HTML. You could store the info on the client side in javascript, though.

As for your comment-question: No, it wouldn't. The solution really depends on your use case, if you need information to be available for later use, you'd have to store it in a database or file. If only for the current browsing page, store it in the user's browser with javascript. Storing it as session variable is another option.


You need to store it in a session. Documentation.

Basically,

session_start();

$myarray = $_SESSION['myarray'];
//work with $myarray here

//store it back in the session
$_SESSION['myarray'] = $myarray;


push the data onclick using javascript. Then try to pass the whole queue on each click

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜