ASD
ASD

Reputation: 4967

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

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;
        }",]);

Upvotes: 0

Views: 2211

Answers (1)

Vivio Takamachi
Vivio Takamachi

Reputation: 39

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']);

Upvotes: 3

Related Questions