admin管理员组文章数量:1391987
I have an address field in the locations database, with a value Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala
, the website was working great until I added the location with this address, the google maps plugin no longer works because this single quote has caused a javascript error in the console,
My location controller get function:
public static function get() {
$locations = Location::with('country')->get();
return $locations;
}
My script for loading locations into google maps:
var locations = JSON.parse('{!! $locations !!}');
var lang = "{{ App::getLocale() }}";
$(locations).each(function(key, value) {
allLocationsPins.push({lat: parseInt(value.latitude), lng: parseInt(value.longitude)});
});
The single quote in the address value is causing this error because it's not escaped. I tried adding the JSON encode
function to the locations array before returning it from the controller but nothing happened, I think escaping the single quote by Json_encode
is not working because of the {!! $locations !!} in the script file, but if I tried to remove the {!! !!} every character in the string change.
How can I fix this? If json_encode
is not working what else can I use ?
I tried to add \ before the single quote in a forloop, but it is not doing anything for some reason, here is the forloop and the output.
public static function get() {
$locations = Location::with('country')->get();
foreach ($locations as $location) {
$location["address"] = str_replace("'","\'",$location["address"]);
}
var_dump($locations);
return $locations;
}
// PART OF VAR DUMP OUTPUT..
array(14) {
["id"]=>
int(7)
["title"]=>
string(6) "Hamala"
["title_ar"]=>
NULL
["address"]=>
string(62) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa\' Complex - Hamala"
["address_ar"]=>
NULL
["latitude"]=>
string(9) "26.166246"
["longitude"]=>
string(9) "50.468510"
["phone_numbers"]=>
string(13) "+973 17610612"
["customer_service_email"]=>
string(28) "[email protected]"
["working_hours"]=>
string(84) "From 09:00 AM to 01:00 PM | Evening Shift: From 04:00 PM to 08:00 PM | Friday is off"
["working_hours_ar"]=>
NULL
["country_id"]=>
int(3)
["created_at"]=>
string(19) "2017-11-13 16:21:15"
["updated_at"]=>
string(19) "2017-11-06 19:01:54"
}
["original":protected]=>
array(14) {
["id"]=>
int(7)
["title"]=>
string(6) "Hamala"
["title_ar"]=>
NULL
["address"]=>
string(61) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala"
["address_ar"]=>
NULL
["latitude"]=>
string(9) "26.166246"
["longitude"]=>
string(9) "50.468510"
["phone_numbers"]=>
string(13) "+973 17610612"
["customer_service_email"]=>
string(28) "[email protected]"
["working_hours"]=>
string(84) "From 09:00 AM to 01:00 PM | Evening Shift: From 04:00 PM to 08:00 PM | Friday is off"
["working_hours_ar"]=>
NULL
["country_id"]=>
int(3)
["created_at"]=>
string(19) "2017-11-13 16:21:15"
["updated_at"]=>
string(19) "2017-11-06 19:01:54"
}
As you can see the address location array is found twice the first one which was changed as expected and the second one which is protected and did not change the problem is with is line right here
["address"]=>
string(61) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala"
I have an address field in the locations database, with a value Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala
, the website was working great until I added the location with this address, the google maps plugin no longer works because this single quote has caused a javascript error in the console,
My location controller get function:
public static function get() {
$locations = Location::with('country')->get();
return $locations;
}
My script for loading locations into google maps:
var locations = JSON.parse('{!! $locations !!}');
var lang = "{{ App::getLocale() }}";
$(locations).each(function(key, value) {
allLocationsPins.push({lat: parseInt(value.latitude), lng: parseInt(value.longitude)});
});
The single quote in the address value is causing this error because it's not escaped. I tried adding the JSON encode
function to the locations array before returning it from the controller but nothing happened, I think escaping the single quote by Json_encode
is not working because of the {!! $locations !!} in the script file, but if I tried to remove the {!! !!} every character in the string change.
How can I fix this? If json_encode
is not working what else can I use ?
I tried to add \ before the single quote in a forloop, but it is not doing anything for some reason, here is the forloop and the output.
public static function get() {
$locations = Location::with('country')->get();
foreach ($locations as $location) {
$location["address"] = str_replace("'","\'",$location["address"]);
}
var_dump($locations);
return $locations;
}
// PART OF VAR DUMP OUTPUT..
array(14) {
["id"]=>
int(7)
["title"]=>
string(6) "Hamala"
["title_ar"]=>
NULL
["address"]=>
string(62) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa\' Complex - Hamala"
["address_ar"]=>
NULL
["latitude"]=>
string(9) "26.166246"
["longitude"]=>
string(9) "50.468510"
["phone_numbers"]=>
string(13) "+973 17610612"
["customer_service_email"]=>
string(28) "[email protected]"
["working_hours"]=>
string(84) "From 09:00 AM to 01:00 PM | Evening Shift: From 04:00 PM to 08:00 PM | Friday is off"
["working_hours_ar"]=>
NULL
["country_id"]=>
int(3)
["created_at"]=>
string(19) "2017-11-13 16:21:15"
["updated_at"]=>
string(19) "2017-11-06 19:01:54"
}
["original":protected]=>
array(14) {
["id"]=>
int(7)
["title"]=>
string(6) "Hamala"
["title_ar"]=>
NULL
["address"]=>
string(61) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala"
["address_ar"]=>
NULL
["latitude"]=>
string(9) "26.166246"
["longitude"]=>
string(9) "50.468510"
["phone_numbers"]=>
string(13) "+973 17610612"
["customer_service_email"]=>
string(28) "[email protected]"
["working_hours"]=>
string(84) "From 09:00 AM to 01:00 PM | Evening Shift: From 04:00 PM to 08:00 PM | Friday is off"
["working_hours_ar"]=>
NULL
["country_id"]=>
int(3)
["created_at"]=>
string(19) "2017-11-13 16:21:15"
["updated_at"]=>
string(19) "2017-11-06 19:01:54"
}
As you can see the address location array is found twice the first one which was changed as expected and the second one which is protected and did not change the problem is with is line right here
["address"]=>
string(61) "Shop 2197 - Block 1014 - Road 90 - Al-Delaa' Complex - Hamala"
Share
Improve this question
edited Nov 14, 2017 at 9:38
Bassem Mohamed
asked Nov 13, 2017 at 14:19
Bassem MohamedBassem Mohamed
1132 silver badges7 bronze badges
2 Answers
Reset to default 4If your string has a single quote, the string variable you are passing to JSON.parse() wont work because the method doesnt accept them.
Try first declaring the '{!! $locations !!}'
into a variable before parsing it and see if that works, it shouldnt.
You will have to manually escape the single quote with \'
Example:
var aString = '{ "name":"John", "age":30, "city":"New\'York"}'
var aJson = JSON.parse(aString)
Laravel is still PHP
just use addslashes()
var locations = JSON.parse('{{ addslashes($locations) }}');
I think something like that will work.
https://www.php/manual/en/function.addslashes.php
本文标签: javascriptEscaping single quote in php laravelStack Overflow
版权声明:本文标题:javascript - Escaping single quote in php laravel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744637826a2616931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论