How to pass the values of localstorage from view to controller to use as php variable in another view

邮差的信 提交于 2021-02-20 04:12:12

问题


In view on click of button am using location.href to pass on to controller. How can i pass the localstorage value in order to use it as a php variable

In view:

echo Html::button('Proceed', [  
    'onclick' => "
        var dataVal =  localStorage.getItem('Customers');
        if(dataVal.length > 0) {
            location.href = '" . Yii::$app->urlManager->createUrl(['customers/orders']) . "';
        }else{
            alert('Please add some items to order!')
            return false;
        }",]);

回答1:


While this is generally not recommended practice, it is possible to concatenate the value to the URL:

location.href = '" . Yii::$app->urlManager->createUrl(['customers/orders']) . "?Customers=' + encodeURIComponent(JSON.stringify(dataVal));

Now, your PHP side has a query parameter Customers encoded as JSON, so decode as such:

$Customers = json_decode($_GET['Customers']);


来源:https://stackoverflow.com/questions/39038225/how-to-pass-the-values-of-localstorage-from-view-to-controller-to-use-as-php-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!