How to give parameter values in jquery url - javascript

$('#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 =

Related

Javascript variable not working in php echo

My code :
<?php echo ' <script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques".p);? >
The value of p is displayed as 0.
You need to close your php tag properly as well as the <script> tag like so:
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques" +p);</script>'; ?>
Also, change the . to a + as you are concatenating in javascript not PHP
The correct answer is:
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){p++;}alert("ques" + p)'; ?>
Strings in single quotes will be escaped, use quotation marks instead.
<?php echo "<script>var p=0;for(var i=0;i<=5;i++){p++;}alert('ques' +p);</script>"; ?>
The mistakes in your code are,
Close your php tag properly - ?> instead of ? >
Close the script tag in the end - </script>
Close the single quote you started with echo
The correct code would be
<?php echo '<script>var p=0;for(var i=0;i<=5;i++){ p++; } alert("ques" + p); </script>'; ?>

escaping apostrophe in javascript shows extra backslash

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
}

qtip and html data in title="" is causing conflict

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`;

Adding php within a php code

Basically what i mean is i want to echo a piece of a php using a php method code below is getting errors
<?php
$myString = '<?= $_GET['content'] ?>';
echo $myString;
?>
maybe theres a javascript alternative?
full code below
<?php
if (in_array($_GET['content'], array(tree, 3, 4, 5)) ) {
$myString = "<?= $_GET['content'] ?>";
$myParsedString = htmlentities($myString);
echo $myParsedString;
}
else {
echo "nothing to see here";
}
?>
you can directly access $_GET inside <?php ?>no need to use tag again inside :try this.
$myString = $_GET['content'];
This should work for you:
if ( in_array($_GET['content'], array("tree", 3, 4, 5)) ) {
$myString = $_GET['content'];
echo $myParsedString = htmlentities($myString);
} else {
echo "nothing to see here";
}
No need to use another PHP tag inside PHP tag. Simply can try this
<?php
if (in_array($_GET['content'], array('tree', 3, 4, 5)) ) {
$myString = $_GET['content'];
$myParsedString = htmlentities($myString);
echo $myParsedString;
}else {
echo "nothing to see here";
}
?>
Just escape the PHP code you get and then u can echo it
$myString = mysql_real_escape_string('<?= $_GET['content'] ?>');
echo $myString;
I hope that helps

.replace() not working inside a jQuery function

I don't understand why replace() function doesn't work into my jQuery function:
jQuery(document).ready(function($){
var amount_min = <?php if($_GET['amount_min']) echo $_GET['amount_min']; else echo '0'; ?>;
var amount_min = amount_min.replace(/[^\d]/g, "");
$('input[name=amount]').val(amount_min);
});
Whatever input I give (for example "100ab" or "10.000") it doesn't replace it with "100" or "10000".
How to do?
You forgot to put double-quotes.
var amount_min = "<?php if($_GET['amount_min']) echo $_GET['amount_min']; else echo 0; ?>";
Because, replace works in Strings.
UPDATE #1
If for any religious reason you don't want to wrap the PHP in double quotes then output them along with the number.
var amount_min = <?php echo '"' . ($_GET['amount_min'] ? $_GET['amount_min'] : 0) . '"'; ?>;
UPDATE #2
Compulsory validation you can use:
var amount_min = <?php echo '"' . (int)($_GET['amount_min']) . '"'; ?>;
can you please try this:
$(document).ready(function(){
var amount_min = "<?php if($_GET['amount_min']){ echo $_GET['amount_min'];}else{ echo '0';} ?>";
console.log("original-> "+amount_min);
var amount_min = amount_min.replace(/\D/g,'');
console.log("replaced-> "+amount_min);
});
Your PHP code is outputting a number:
var amount_min = 100;
Since you're expecting a string, wrap it in quotes:
var amount_min = "<?php if($_GET['amount_min']) echo $_GET['amount_min']; else echo '0'; ?>";
I haven't touched PHP in years, but I think you could simplify your code a little:
var amount_min = "<?php echo($_GET['amount_min'] || '0'); ?>";
Also, why don't you just fetch the GET parameter with JavaScript?
You don't need the .replace() code (it also only works on strings); PHP can already do the proper conversion for you:
$(function() {
var amount_min = <?php echo isset($_GET['amount_min']) ? (int)$_GET['amount_min'] : 0; ?>;
$('input[name=amount]').val(amount_min);
});
You could also use filtering for this:
<?php echo filter_input(INPUT_GET, 'amount_min', FILTER_VALIDATE_INT, array('options' => array('default' => 0))); ?>
Note that filter_input would not accept a value like 100abc, so use wisely.
If you still want to use strings safely in JavaScript you should use json_encode().
Btw, any answer that involves an unmodified echo of a request variable from PHP inside JavaScript code is wrong and can cause XSS attacks! You have been warned.
Update
The regular expression based replacement can also be done in PHP:
var amount_min = <?php echo (int)preg_replace('/\D+/', '', isset($_GET['amount_min']) ? $_GET['amount_min'] : 0); ?>;
Since all non-digits are removed, you can safely apply the (int) cast.

Categories

Resources