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???










share|improve this question


















  • 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















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???










share|improve this question


















  • 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













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???










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 18:54









tebowner

257




257








  • 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














  • 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








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












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 = {&quot;test1&quot;:&quot;123&quot;,&quot;test2&quot;:&quot;456&quot;}&quot;
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>





share|improve this answer



















  • 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 use var 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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 = {&quot;test1&quot;:&quot;123&quot;,&quot;test2&quot;:&quot;456&quot;}&quot;
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>





share|improve this answer



















  • 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 use var 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















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 = {&quot;test1&quot;:&quot;123&quot;,&quot;test2&quot;:&quot;456&quot;}&quot;
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>





share|improve this answer



















  • 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 use var 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













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 = {&quot;test1&quot;:&quot;123&quot;,&quot;test2&quot;:&quot;456&quot;}&quot;
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>





share|improve this answer














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 = {&quot;test1&quot;:&quot;123&quot;,&quot;test2&quot;:&quot;456&quot;}&quot;
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>






share|improve this answer














share|improve this answer



share|improve this answer








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 use var 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




    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 use var 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo