Reputation: 4967
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
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