I'm using language files with Codeigniter to display any kind of messages, such as:
$lang['home']['msg1'] = "We couldn\'t proceed...";
I'm calling these variables in Javascript (in my footer) with the following code:
var Settings = {
base_url: '<?php echo base_url(); ?>',
hire_text: '<?php echo $this->lang->line('hire'); ?>',
msg1: '<?php echo $this->lang->line('home')['msg1']; ?>'
}
Unfortunately I haven't managed to handle properly sentences with an apostrophe. I've tried the following:
$lang['home']['msg1'] = "We couldn\'t proceed...";
shows:
We couldn\'t proceed...
and
$lang['home']['msg1'] = "We couldn't proceed...";
returns a Javascript error message
After reading multiples questions/posts I still can't figure out the proper way
You are breaking both expressions (JS and PHP) by using single quotes with no escaping. You can escape inside quote mark or use combination of single and double quotes.
var Settings = {
base_url: "<?php echo base_url(); ?>",
hire_text: "<?php echo $this->lang->line('hire'); ?>",
msg1: '<?php echo $this->lang->line(\'home\')[\'msg1\']; ?>'// should be working either way
}
Related
I'm trying to load html content into visjs timeline and using qtip2 to display a tooltip when a hyperlink inside a timeline is clicked. This tooltip needs to display html content, so it has "" in it, which causes a problem when its inserted into a title="". Normally i would be able to use a single quote in the title='<-html - content->' so it should be able to handle the quotes. But now i'm facing a problem, because i'm trying to do this from inside javascript which already uses a single quote from the javascript itself.
if i want to add a item to visjs timeline, i need to insert new items to the timeline like this.
{id: <?php echo $key; ?>, group: <?php echo $aspects['group'][$key]; ?>, content: '<a title="<?php if(isset($aspects['interpretation'][$key])): echo $aspects['interpretation'][$key]; endif;?>" role="button"><?php echo $aspects['symbols'][$key]; ?></a>', start: new Date(<?php echo $aspects['dates']['start'][$key]['year']; ?>, <?php echo $aspects['dates']['start'][$key]['month'] - 1;?>, <?php echo $aspects['dates']['start'][$key]['day'];?>, <?php echo $aspects['dates']['start'][$key]['hour'];?>, <?php echo $aspects['dates']['start'][$key]['min'];?>), end: new Date(<?php echo $aspects['dates']['end'][$key]['year']; ?>,<?php echo $aspects['dates']['end'][$key]['month'] - 1; ?>, <?php echo $aspects['dates']['end'][$key]['day']; ?>, <?php echo $aspects['dates']['end'][$key]['hour']; ?>, <?php echo $aspects['dates']['end'][$key]['min']; ?> ) },
But as you can see, content:' href etc ' already uses a single quote. So how can i escape this html quotes so it doesn't cause a conflict? I tried things like title = \" \" in the href, but i am not finding any solution yet to solve my problem.
You should use htmlspecialchars before outputting your PHP variables.
Try using templating strings in JavaScript.
var string = `hello " and ' work in this string`;
CKEDITOR.instances.content.getData() has a text like This is a' ball .
i send the following data to a php page using ajax to store it to a database
var data="answer_body="+CKEDITOR.instances.content.getData()+"&userpost_post_id=<?php echo $contents[0]->post_id;?>&users_user_id=<?php echo $userdata->user_id; ?>";
But the answer body throws away rest of the text after quote.
$answer=addslashes($_POST['answer_body']);
echo $answer; // it contains only -----------This is a
how i can solve this problem?
Try below Ajax code to send data from client to server
$.ajax({
type: 'POST',
url: "Here_Your_Server_Url",
data: {
answer_body: encodeURIComponent(CKEDITOR.instances.content.getData()),
userpost_post_id: "<?php echo $contents[0]->post_id; ?>",
users_user_id: "<?php echo $userdata->user_id; ?>"
}
})
Try below code in PHP
$strAnswerBody = urldecode($_POST['answer_body']);
I am trying to get information to post via PHP on button click, everything in my code looks right, and when I view the page source everything is filled in the way it should be. When I pull up the debugger when the button is clicked it does not make any calls at all, here is my button
<button type="button">Continue</button>
here is my ajax call
$(document).ready(function() {
$("button").click(function(){
var Id = <?php echo $give ?>;
var Ids = <?php echo $rec ?>;
var ex = <?php echo $exchange ?>;
var ret = <?php echo $tost ?>;
var email = <?php echo $email ?>;
var name = <?php echo $name ?>;
var pe = <?php echo $pe ?>;
var re = <?php echo $re ?>;
$.post("postts.php", {
Id: Id,
Ids: Ids,
ex: ex,
ret: ret,
email: email,
name: name,
pe: pe,
re: re
}, function(data){
alert(data);
$("p").text(data);
}, 'json' );
});
});
all of my vars will populate with the correct information, I just don't know why when I click the button it does nothing. I am not sure if I have made a typo, or if my syntax is wrong, this is only the second time I have done something like this.
You have error in writing variable values from PHP to JavaScript, you must wrap PHP output by quotation marks, so JavaScipt will "see" string ;)
instead of:
var email = <?php echo $email ?>;
write:
var mail = '<?php echo $email ?>';
I have PHP Script / HTML
$string1 = 'Foo';
$string2 = 'Bar';
$string3 = 'This string contains some URL';
$string4 = 'This string contains multi-line short description which contains single quotes, double quotes and some comma characters as well.';
Here is my onClick
<a class="someClass" title="someTitle" href="#" onclick="someFunction('<?php echo $string1; ?>', '<?php echo $string2; ?>', '<?php echo $string3; ?>', '<?php echo $string4; ?>')">Click Here</a>
But while receiving, I am getting all sorts of string parsing errors on someFunction()
In php side use:
<?php echo addslashes($string1); ?>
And inside someFunction( string1 ) use:
string1 = string1.replace(/\\/g, '');
To get the normal value of the string;
$('#add').click(function(){
$.jeegoopopup.open({
url: 'update.php?cids= <?php echo $dt['callerid'] ?> && eid= <?php echo $dt['eid'] ?> && sts= <?php echo $dt['status'] ?>',
width: 500,
height: 200,
center: true,
skinClass: 'jg_popup_round',
resizable: false,
scrolling: 'no',
If i give parameter values in url, the button not performing action
can any one help me
Updated
&& is WRONG in Query string , its only & . The && operator comes ONLY in logical operations like a==2 && b==2
Use proper quotes like following in JS Ajax code to concatenate PHP cariables.
url: 'update.php?cids='+<?php echo $dt['callerid'] ?>+'&eid='+<?php echo $dt['eid'] ?>+'&sts='+<?php echo $dt['status'] ?>,
Use Double quotes instead of single quotes in start and end like this
url: "update.php?cids= <?php echo $dt['callerid'] ?> && eid= <?php echo $dt['eid'] ?> && sts= <?php echo $dt['status'] ?>"
and check your console also for errors
You should check for the spaces after = and lookout for quotes and make a single & ampersands too:
url: 'update.php?cids=<?php echo $dt["callerid"] ?>&eid=<?php echo $dt["eid"] ?>&sts=<?php echo $dt["status"] ?>',
//--------------------^----------------------------^^---^-----------------------^^---^
There are issues with :
Quotes
&& double ampersands.
space after =