How Do You Deal With ', Single Quote In Person's Names In Php, Javascript
I noticed that some of name entries, like O'Rourke or O'reilly breaks javascript in some cases. I wonder how you deal with it. I pull these names out of PHP/MYSQL and put them in j
Solution 1:
You're going through a lot of messy and error-prone string-appending work just to output a javascript variable. What you should do instead is build the data structure you want in PHP (an array of arrays) and then use json_encode()
to emit a javascript-compatible literal. All quotes and such will be automatically escaped by the encoder.
$itemOutput = array();
if(count($items)) {
foreach($items as $item) {
foreach($advisors as $key=>$advisor) {
if($item['advisor']==$advisor['id']) {
$ad=$advisor['last_name'];
}
}
$active_icon = ($item['active']=='1'?'tick':'cross');
$editlink = anchor('auth/admin/members/form/'.$item['id'],$this->bep_assets->icon('pencil'));
$itemOutput[] = array(
$item['first_name'],
$item['last_name'],
$item['email'],
$item['parent_email'],
$item['parent_email2'],
$ad,
$this->beep_assets->icon($active_icon),
$editLink
);
}
}
echo "data.addRows(" . json_encode($itemOutput) . ");" ;
If you have an object or an associative array, it gets emitted as a javascript object:
echo json_encode(
array( 'a'=>'aa', 'b'=>'bb'),
array( 'c'=>'cc', 'd'=>'dd')
);
==> [{"a":"aa","b":"bb"},{"c":"cc","d":"dd"}]
Post a Comment for "How Do You Deal With ', Single Quote In Person's Names In Php, Javascript"