PHP session / json / javascript
up vote
0
down vote
favorite
I need to figure out why i cannot get this Session to JS for a JSON object. I am sure i missing something simple.
I have a Session var being set in laravel
Session::put('test', "{'test1':'123','test2':'456'}");
I want to set this in a page in js :
<script>var testvar = "{{Session::get('test')}}";
then i try and do this in js :
var test_json = JSON.parse(testvar);
but i am seeing the js testvar is set with the quotes encoded?
what i am i doing wrong???
javascript php laravel session
add a comment |
up vote
0
down vote
favorite
I need to figure out why i cannot get this Session to JS for a JSON object. I am sure i missing something simple.
I have a Session var being set in laravel
Session::put('test', "{'test1':'123','test2':'456'}");
I want to set this in a page in js :
<script>var testvar = "{{Session::get('test')}}";
then i try and do this in js :
var test_json = JSON.parse(testvar);
but i am seeing the js testvar is set with the quotes encoded?
what i am i doing wrong???
javascript php laravel session
1
why do you want to useSession
in a blade? You need to pass these values as parameters to the blade, respecting MVC.
– vivek_23
Nov 21 at 19:00
1
1. That's not JSON, 2. There's no such thing as a "JSON object"
– Andreas
Nov 21 at 19:00
@Andreas If it walks like a duck and talks like a duck, it's a json object. :)~
– Justin Schwimmer
Nov 21 at 19:06
@JustinSchwimmer If something is wrong, its wrong. Just because there's a site that's using the wrong term for it doesn't make it correct.
– Andreas
Nov 21 at 19:20
1
@Andreas If it's wrong - we have to right a wrong. And sometimes, in order to right a wrong, you have to do a wrong-right.
– Justin Schwimmer
Nov 21 at 19:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to figure out why i cannot get this Session to JS for a JSON object. I am sure i missing something simple.
I have a Session var being set in laravel
Session::put('test', "{'test1':'123','test2':'456'}");
I want to set this in a page in js :
<script>var testvar = "{{Session::get('test')}}";
then i try and do this in js :
var test_json = JSON.parse(testvar);
but i am seeing the js testvar is set with the quotes encoded?
what i am i doing wrong???
javascript php laravel session
I need to figure out why i cannot get this Session to JS for a JSON object. I am sure i missing something simple.
I have a Session var being set in laravel
Session::put('test', "{'test1':'123','test2':'456'}");
I want to set this in a page in js :
<script>var testvar = "{{Session::get('test')}}";
then i try and do this in js :
var test_json = JSON.parse(testvar);
but i am seeing the js testvar is set with the quotes encoded?
what i am i doing wrong???
javascript php laravel session
javascript php laravel session
asked Nov 21 at 18:54
tebowner
257
257
1
why do you want to useSession
in a blade? You need to pass these values as parameters to the blade, respecting MVC.
– vivek_23
Nov 21 at 19:00
1
1. That's not JSON, 2. There's no such thing as a "JSON object"
– Andreas
Nov 21 at 19:00
@Andreas If it walks like a duck and talks like a duck, it's a json object. :)~
– Justin Schwimmer
Nov 21 at 19:06
@JustinSchwimmer If something is wrong, its wrong. Just because there's a site that's using the wrong term for it doesn't make it correct.
– Andreas
Nov 21 at 19:20
1
@Andreas If it's wrong - we have to right a wrong. And sometimes, in order to right a wrong, you have to do a wrong-right.
– Justin Schwimmer
Nov 21 at 19:24
add a comment |
1
why do you want to useSession
in a blade? You need to pass these values as parameters to the blade, respecting MVC.
– vivek_23
Nov 21 at 19:00
1
1. That's not JSON, 2. There's no such thing as a "JSON object"
– Andreas
Nov 21 at 19:00
@Andreas If it walks like a duck and talks like a duck, it's a json object. :)~
– Justin Schwimmer
Nov 21 at 19:06
@JustinSchwimmer If something is wrong, its wrong. Just because there's a site that's using the wrong term for it doesn't make it correct.
– Andreas
Nov 21 at 19:20
1
@Andreas If it's wrong - we have to right a wrong. And sometimes, in order to right a wrong, you have to do a wrong-right.
– Justin Schwimmer
Nov 21 at 19:24
1
1
why do you want to use
Session
in a blade? You need to pass these values as parameters to the blade, respecting MVC.– vivek_23
Nov 21 at 19:00
why do you want to use
Session
in a blade? You need to pass these values as parameters to the blade, respecting MVC.– vivek_23
Nov 21 at 19:00
1
1
1. That's not JSON, 2. There's no such thing as a "JSON object"
– Andreas
Nov 21 at 19:00
1. That's not JSON, 2. There's no such thing as a "JSON object"
– Andreas
Nov 21 at 19:00
@Andreas If it walks like a duck and talks like a duck, it's a json object. :)~
– Justin Schwimmer
Nov 21 at 19:06
@Andreas If it walks like a duck and talks like a duck, it's a json object. :)~
– Justin Schwimmer
Nov 21 at 19:06
@JustinSchwimmer If something is wrong, its wrong. Just because there's a site that's using the wrong term for it doesn't make it correct.
– Andreas
Nov 21 at 19:20
@JustinSchwimmer If something is wrong, its wrong. Just because there's a site that's using the wrong term for it doesn't make it correct.
– Andreas
Nov 21 at 19:20
1
1
@Andreas If it's wrong - we have to right a wrong. And sometimes, in order to right a wrong, you have to do a wrong-right.
– Justin Schwimmer
Nov 21 at 19:24
@Andreas If it's wrong - we have to right a wrong. And sometimes, in order to right a wrong, you have to do a wrong-right.
– Justin Schwimmer
Nov 21 at 19:24
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
The problem
Your following code
<script>
var testvar = "{{Session::get('test')}}";
var data = JSON.parse(testvar);
</script>
would be interpreted as:
<script>
var testvar = {'test1':'123','test2':'456'};
var data = JSON.parse(testvar);
</script>
{'test1':'123','test2':'456'}
is invalid JSON, as you need to use double quotes instead of single quotes.
So you need:
Session::put('test', '{"test1":"123","test2":"456"}');
But this creates another problem.
Laravel auto escapes HTML for security reasons to avoid XSS attacks.
This means the above is now interpreted as :
<script>
var testvar = {"test1":"123","test2":"456"}"
var data = JSON.parse(testvar);
</script>
If you are sure the data you are storing is safe, you can use raw tags like so, and avoid using JSON.parse
(so we don't need to re-escape any of the quotes again) like so:
<script>
var testvar = {!! Session::get('test') !!};
</script>
This will then output the HTML:
<script>
var testvar = {"test1":"123", "test2":"456"};
</script>
A better solution
A better way is to store your data as an array in the PHP session:
Session::put('test', [
'test1' => '123',
'test2' => '456'
]);
Then you could just do:
<script>
var testvar = @json(Session::get('test'));
</script>
This is explained here:
https://laravel.com/docs/5.7/blade#displaying-data
Side Note
As a side note, consider passing a variable into blade to avoid doing logic in your views like so:
return view('your-view-name', [
'testvar' => Session::get('test'),
]);
Then you could just do:
<script>
var testvar = @json($testvar);
</script>
1
The properties (and if required also the values) have to be wrapped in double quotes
– Andreas
Nov 21 at 19:02
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
– Yahya Uddin
Nov 21 at 19:07
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
– tebowner
Nov 21 at 19:12
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and usevar testvar = @json(Session::get('test'));
.
– Yahya Uddin
Nov 21 at 19:18
1
In your first version of the answer you were using"{!! Session::get('test') !!}"
which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");
)
– Andreas
Nov 21 at 19:18
|
show 8 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
The problem
Your following code
<script>
var testvar = "{{Session::get('test')}}";
var data = JSON.parse(testvar);
</script>
would be interpreted as:
<script>
var testvar = {'test1':'123','test2':'456'};
var data = JSON.parse(testvar);
</script>
{'test1':'123','test2':'456'}
is invalid JSON, as you need to use double quotes instead of single quotes.
So you need:
Session::put('test', '{"test1":"123","test2":"456"}');
But this creates another problem.
Laravel auto escapes HTML for security reasons to avoid XSS attacks.
This means the above is now interpreted as :
<script>
var testvar = {"test1":"123","test2":"456"}"
var data = JSON.parse(testvar);
</script>
If you are sure the data you are storing is safe, you can use raw tags like so, and avoid using JSON.parse
(so we don't need to re-escape any of the quotes again) like so:
<script>
var testvar = {!! Session::get('test') !!};
</script>
This will then output the HTML:
<script>
var testvar = {"test1":"123", "test2":"456"};
</script>
A better solution
A better way is to store your data as an array in the PHP session:
Session::put('test', [
'test1' => '123',
'test2' => '456'
]);
Then you could just do:
<script>
var testvar = @json(Session::get('test'));
</script>
This is explained here:
https://laravel.com/docs/5.7/blade#displaying-data
Side Note
As a side note, consider passing a variable into blade to avoid doing logic in your views like so:
return view('your-view-name', [
'testvar' => Session::get('test'),
]);
Then you could just do:
<script>
var testvar = @json($testvar);
</script>
1
The properties (and if required also the values) have to be wrapped in double quotes
– Andreas
Nov 21 at 19:02
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
– Yahya Uddin
Nov 21 at 19:07
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
– tebowner
Nov 21 at 19:12
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and usevar testvar = @json(Session::get('test'));
.
– Yahya Uddin
Nov 21 at 19:18
1
In your first version of the answer you were using"{!! Session::get('test') !!}"
which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");
)
– Andreas
Nov 21 at 19:18
|
show 8 more comments
up vote
4
down vote
accepted
The problem
Your following code
<script>
var testvar = "{{Session::get('test')}}";
var data = JSON.parse(testvar);
</script>
would be interpreted as:
<script>
var testvar = {'test1':'123','test2':'456'};
var data = JSON.parse(testvar);
</script>
{'test1':'123','test2':'456'}
is invalid JSON, as you need to use double quotes instead of single quotes.
So you need:
Session::put('test', '{"test1":"123","test2":"456"}');
But this creates another problem.
Laravel auto escapes HTML for security reasons to avoid XSS attacks.
This means the above is now interpreted as :
<script>
var testvar = {"test1":"123","test2":"456"}"
var data = JSON.parse(testvar);
</script>
If you are sure the data you are storing is safe, you can use raw tags like so, and avoid using JSON.parse
(so we don't need to re-escape any of the quotes again) like so:
<script>
var testvar = {!! Session::get('test') !!};
</script>
This will then output the HTML:
<script>
var testvar = {"test1":"123", "test2":"456"};
</script>
A better solution
A better way is to store your data as an array in the PHP session:
Session::put('test', [
'test1' => '123',
'test2' => '456'
]);
Then you could just do:
<script>
var testvar = @json(Session::get('test'));
</script>
This is explained here:
https://laravel.com/docs/5.7/blade#displaying-data
Side Note
As a side note, consider passing a variable into blade to avoid doing logic in your views like so:
return view('your-view-name', [
'testvar' => Session::get('test'),
]);
Then you could just do:
<script>
var testvar = @json($testvar);
</script>
1
The properties (and if required also the values) have to be wrapped in double quotes
– Andreas
Nov 21 at 19:02
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
– Yahya Uddin
Nov 21 at 19:07
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
– tebowner
Nov 21 at 19:12
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and usevar testvar = @json(Session::get('test'));
.
– Yahya Uddin
Nov 21 at 19:18
1
In your first version of the answer you were using"{!! Session::get('test') !!}"
which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");
)
– Andreas
Nov 21 at 19:18
|
show 8 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
The problem
Your following code
<script>
var testvar = "{{Session::get('test')}}";
var data = JSON.parse(testvar);
</script>
would be interpreted as:
<script>
var testvar = {'test1':'123','test2':'456'};
var data = JSON.parse(testvar);
</script>
{'test1':'123','test2':'456'}
is invalid JSON, as you need to use double quotes instead of single quotes.
So you need:
Session::put('test', '{"test1":"123","test2":"456"}');
But this creates another problem.
Laravel auto escapes HTML for security reasons to avoid XSS attacks.
This means the above is now interpreted as :
<script>
var testvar = {"test1":"123","test2":"456"}"
var data = JSON.parse(testvar);
</script>
If you are sure the data you are storing is safe, you can use raw tags like so, and avoid using JSON.parse
(so we don't need to re-escape any of the quotes again) like so:
<script>
var testvar = {!! Session::get('test') !!};
</script>
This will then output the HTML:
<script>
var testvar = {"test1":"123", "test2":"456"};
</script>
A better solution
A better way is to store your data as an array in the PHP session:
Session::put('test', [
'test1' => '123',
'test2' => '456'
]);
Then you could just do:
<script>
var testvar = @json(Session::get('test'));
</script>
This is explained here:
https://laravel.com/docs/5.7/blade#displaying-data
Side Note
As a side note, consider passing a variable into blade to avoid doing logic in your views like so:
return view('your-view-name', [
'testvar' => Session::get('test'),
]);
Then you could just do:
<script>
var testvar = @json($testvar);
</script>
The problem
Your following code
<script>
var testvar = "{{Session::get('test')}}";
var data = JSON.parse(testvar);
</script>
would be interpreted as:
<script>
var testvar = {'test1':'123','test2':'456'};
var data = JSON.parse(testvar);
</script>
{'test1':'123','test2':'456'}
is invalid JSON, as you need to use double quotes instead of single quotes.
So you need:
Session::put('test', '{"test1":"123","test2":"456"}');
But this creates another problem.
Laravel auto escapes HTML for security reasons to avoid XSS attacks.
This means the above is now interpreted as :
<script>
var testvar = {"test1":"123","test2":"456"}"
var data = JSON.parse(testvar);
</script>
If you are sure the data you are storing is safe, you can use raw tags like so, and avoid using JSON.parse
(so we don't need to re-escape any of the quotes again) like so:
<script>
var testvar = {!! Session::get('test') !!};
</script>
This will then output the HTML:
<script>
var testvar = {"test1":"123", "test2":"456"};
</script>
A better solution
A better way is to store your data as an array in the PHP session:
Session::put('test', [
'test1' => '123',
'test2' => '456'
]);
Then you could just do:
<script>
var testvar = @json(Session::get('test'));
</script>
This is explained here:
https://laravel.com/docs/5.7/blade#displaying-data
Side Note
As a side note, consider passing a variable into blade to avoid doing logic in your views like so:
return view('your-view-name', [
'testvar' => Session::get('test'),
]);
Then you could just do:
<script>
var testvar = @json($testvar);
</script>
edited Nov 21 at 20:06
answered Nov 21 at 18:58
Yahya Uddin
5,4791451104
5,4791451104
1
The properties (and if required also the values) have to be wrapped in double quotes
– Andreas
Nov 21 at 19:02
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
– Yahya Uddin
Nov 21 at 19:07
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
– tebowner
Nov 21 at 19:12
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and usevar testvar = @json(Session::get('test'));
.
– Yahya Uddin
Nov 21 at 19:18
1
In your first version of the answer you were using"{!! Session::get('test') !!}"
which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");
)
– Andreas
Nov 21 at 19:18
|
show 8 more comments
1
The properties (and if required also the values) have to be wrapped in double quotes
– Andreas
Nov 21 at 19:02
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
– Yahya Uddin
Nov 21 at 19:07
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
– tebowner
Nov 21 at 19:12
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and usevar testvar = @json(Session::get('test'));
.
– Yahya Uddin
Nov 21 at 19:18
1
In your first version of the answer you were using"{!! Session::get('test') !!}"
which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");
)
– Andreas
Nov 21 at 19:18
1
1
The properties (and if required also the values) have to be wrapped in double quotes
– Andreas
Nov 21 at 19:02
The properties (and if required also the values) have to be wrapped in double quotes
– Andreas
Nov 21 at 19:02
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
– Yahya Uddin
Nov 21 at 19:07
No they don't. I think your getting confused with JSON. JavaScript objects allows BOTH single and double quoted properties and values.
– Yahya Uddin
Nov 21 at 19:07
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
– tebowner
Nov 21 at 19:12
i dont understand ... are you saying to set an array in Session var and then do this ? var app = @json(Session::get('test'));
– tebowner
Nov 21 at 19:12
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and use
var testvar = @json(Session::get('test'));
.– Yahya Uddin
Nov 21 at 19:18
I provided 2 solutions. If the session data cannot be changed, then use raw blade tags. However it would be better to set the session data as an array, instead of encoded JSON data, and use
var testvar = @json(Session::get('test'));
.– Yahya Uddin
Nov 21 at 19:18
1
1
In your first version of the answer you were using
"{!! Session::get('test') !!}"
which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");
)– Andreas
Nov 21 at 19:18
In your first version of the answer you were using
"{!! Session::get('test') !!}"
which would have to be parsed because otherwise it would only be a string. And for this to be valid JSON the properties would have to be wrapped in double quotes which wouldn't be the case with the setup of TO (Session::put('test', "{'test1':'123','test2':'456'}");
)– Andreas
Nov 21 at 19:18
|
show 8 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418828%2fphp-session-json-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
why do you want to use
Session
in a blade? You need to pass these values as parameters to the blade, respecting MVC.– vivek_23
Nov 21 at 19:00
1
1. That's not JSON, 2. There's no such thing as a "JSON object"
– Andreas
Nov 21 at 19:00
@Andreas If it walks like a duck and talks like a duck, it's a json object. :)~
– Justin Schwimmer
Nov 21 at 19:06
@JustinSchwimmer If something is wrong, its wrong. Just because there's a site that's using the wrong term for it doesn't make it correct.
– Andreas
Nov 21 at 19:20
1
@Andreas If it's wrong - we have to right a wrong. And sometimes, in order to right a wrong, you have to do a wrong-right.
– Justin Schwimmer
Nov 21 at 19:24