简体   繁体   English

通过JavaScript函数取消php数组元素?

[英]Unset a php array element via javascript function?

Suppose I have a php session array: 假设我有一个php会话数组:

$_SESSION[MyItems]=Array('A'=>"Apple", 'B'=>"Brownie", 'C'="Coin"))

that is used to display items on a page visited by a user and I want the user to be able to drop one or more of the items from the display using a client-side javascript function (ie. something that hides the item or changes the div to display=none...I get how to do that sort of thing now.) rather than involving a trip back to the server/page reload. 用来在用户访问的页面上显示项目,并且我希望用户能够使用客户端javascript函数从显示器上删除一个或多个项目(例如,可以隐藏该项目或更改该项目div到display = none ...我现在就知道如何做这种事情。)而不是涉及返回服务器/页面重载的旅程。

How can I simultaneously drop the item from the $_SESSION[MyItems] array when the javascript drops the item from view (ie so it does not reappear if the page reloads? 当JavaScript从视图中删除该项目时,如何从$_SESSION[MyItems]数组中同时删除该项目(即,如果页面重新加载,它不会再次出现?

I think maybe the task would involve converting the whole $_SESSION[MyItems] array into a javascript array, dropping the item(s) from that, and then having the javascript version of the array somehow overwrite $_SESSION[MyItems] . 我认为也许该任务将涉及将整个$_SESSION[MyItems]数组转换为javascript数组,从中删除该项目,然后使该数组的javascript版本以某种方式覆盖$_SESSION[MyItems] Is this doable? 这可行吗?

All the session data are stored in cookies and cookies are accessable to both client and server. 所有会话数据都存储在Cookie中,并且客户端和服务器均可访问Cookie。 So you can surely do this by editing the cookies but this practice should be avoided and you should not do this because session is meant for server side only. 因此,您可以肯定地通过编辑cookie来做到这一点,但是应该避免这种做法,并且you should not do this因为会话仅用于服务器端。

hope it helps :) 希望能帮助到你 :)

Thats not so simple, because PHP runs on the server-side, the $_SESSION object from PHP is not available on the client-side, where javascript runs. 那不是那么简单,因为PHP在服务器端运行,所以PHP的$_SESSION对象在运行JavaScript的客户端上不可用。

you would need to put something like this in the head of your template (assuming it is a .php-file with HTML-content): 您需要在模板的开头放置这样的内容(假设它是一个具有HTML内容的.php文件):

<html>
    <head>
        ...
        <script type="text/javascript">
            //parse PHP to json-string for javascript initialisation
            var MyItems = <?php echo json_encode ($_SESSION[MyItems], true); ?>;
        </script>
    </head>
    <body></body>
</html>

Then you would have a copy of the PHP $_SESSION object called MyItems in javascript. 然后,您将在JavaScript中获得一个名为MyItems的PHP $_SESSION对象的副本。 But changes made to the JS-object wont be saved to the PHP-object. 但是,对JS对象所做的更改不会保存到PHP对象。 To do this you could use JQuery´s $.post : 为此,您可以使用JQuery的$ .post

$.post ("target.php", { "MyItems": JSON.stringify (MyItems) }, function (result) {

    if (result == "true")
        //do something
    else
        //do something else

});

With your target.php file looking like this: 您的target.php文件如下所示:

<?php

    $_SESSION['MyItems'] = json_decode ($_POST['MyItems']);
    echo "true";

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM