问题
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