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

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

Related

How can I perform math when jquery class equals a certain value?

I'm trying to manipulate an amount based on the name of class that changes with jQuery. My amount comes from here:
<script>
var inboundAmount = <?php echo json_encode($inbound); ?>;
</script>
Let's just say that amount is 10.
I output with a bit of php and script like this:
echo "$<script>document.write(inboundAmount);</script>";
The class changes when a user clicks on it. Sample classes are as follows:
half first3547
pending first3547
paid first3547
What I've been trying to do is make it so that if the class contains the word 'half', then it would do some math:
document.write(inboundAmount/2);
Producing the number 5.
I've tried this and some variations with no luck:
if ($("#mycontent").find("a.half").length > 0) {document.write(inboundAmount/2);}
'mycontent' is the id for the table that a href exists in.
I have also tried the following:
if ( $( this ).hasClass( 'half' ) ) { {document.write(inboundAmount/2);}
still to no avail.
A full example is this:
<td class='grav-results-td'>
<?php if (!empty($check_inbound)):echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\" onclick=\"comtype($id,'inbound',
$vendor)\">$<script>if ( $( this ).hasClass( 'half' ) )
{document.write(inboundAmount/2);}</script></a>"; endif; ?></td>
And I have also tried the manipulation in the success area of my ajax script.
Answering some of the questions below:
$inbound_status first$id is a PHP variable.
The output of document.write(inboundAmount/2); is working correctly and showing half the amount of document.write(inboundAmount);
The this scope inside your <script> tag will not point to the element you expect. Instead of checking in javascript you can check the condition in php itself. Otherwise, you can refer the code below to do it on javascript. You can use the php variable to check whether it contains the string half
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">
$<script>if ('$inbound_status' == 'half' )
{document.write(inboundAmount/2);}
</script>
</a>";
endif;
echo "</td>";
If inboundAmount is a php variable you can do it easily without using javascript
echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
echo "<a href=\"#inbound\"
class=\"$inbound_status first$id\"
onclick=\"comtype($id,'inbound',$vendor)\">$";
if ($inbound_status == 'half'):
echo $inboundAmount/2;
endif;
echo "</a>";
endif;
echo "</td>"

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

onClick echo PHP code to div (AJAX)

So what I'm trying to do is the following:
first: When the button add-location is clicked this loads a php file using AJAX.
jQuery
$('.add-location').click(function() {
var locationName = $(this).data('location');
$.post('http://www.example.com/reload-location-2.php?addparam',
{locationName: locationName}, function(data) {
$("textarea#location").html(data);
})
});
PHP FILE
<?php start_session();
$locationName = array_key_exists('locationName',
$_POST) ? (string) $_POST['locationName'] : '';
if(isset($_GET['delparam'])){
unset($_SESSION['locations'][$locationName]);
}
if(isset($_GET['addparam'])){
$_SESSION['locations'][$locationName] = $locationName;
}
?>
<?php foreach ($_SESSION['locations'] as $location): ?>
<?php echo htmlspecialchars($location); echo "\n"; ?>
<?php endforeach;?>
This all works like a charm! No worries here. No what I'm trying to do is when the button add-location is clicked this echo's the following code to a div called textarea#thema.
<?php foreach ($_SESSION['products'] as $product): ?>
<?php echo htmlspecialchars($product); echo "\n"; ?>
<?php endforeach;?>
OR, if that's easier when the page is loaded -> echo that code to the textarea#thema div.
I do not understand AJAX that much but I'm getting there. But I think the last option maybe the easiest solution?
Could anyone help me figure this one out?
What I tried
Right now, When the button add-location is clicked I reload a previous jQuery script:
$('.add-location').click(function() {
var productName = $(this).data('product');
$.post('http://example.com/reload-2.php?addparam',
{productName: productName}, function(data) {
$("textarea#thema").html(data);
})
});
This works, but it adds an extra <p>-tag leaving me with a line break in the code.
I also changed the .html(data); to .val(data); for the textareas. Also I made a new PHP file with just this code:
<?php foreach ($_SESSION['products'] as $product): ?>
<?php echo htmlspecialchars($product); echo "\n"; ?>
<?php endforeach;?>
And this jQuery code:
$('.add-location').click(function() {
var productName = $(this).data('product');
$.post('http://www.example.com/reload-3.php',
{productName: productName}, function(data) {
$("textarea#thema").val(data);
})
});
But still no go... And I don't think this is the way to go?? I hope I gave enough information.
You should use .val() function to fill textarea, not .html().

How to give parameter values in jquery url

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

How to retrieve $_SESSION value in a JavaScript/HTML

I have this working script yet when I change it to retrieve(supposedly) the value inside $_SESSION["username"], it doesn't retrieve anything. The whole page is saved as .php as I am using some codes that uses PHP.
Code:
echo "<script type=text/javascript>";
echo "var hidden = false;";
echo "function actiondb1() {";
echo "if(!hidden) {";
echo "document.getElementById(\'clickdb1\').style.visibility = \'hidden\';";
echo "document.getElementById(\'db1\').style.visibility = \'visible\';";
echo "document.getElementById(\'db1\').disabled = false;";
echo "document.getElementById(\'db1\').value =".$_SESSION["username"];.";";
echo "}";
echo "}";
echo "</script>";
How can I make the script to properly retrieve the data inside $_SESSION["username"];?
Observe that, for instance, if the value of $_SESSION["username"] is John, your echo will be generating this result:
document.getElementById('db1').value = John;
But John is supposed to be a string and should be wrapped in quotation marks, otherwise JavaScript will understand it as a variable name (which value will be probably undefined).
As Havenard mentioned, this line is missing Javascript single quotes to properly denote a string variable:
echo "document.getElementById(\'db1\').value ='".$_SESSION["username"];."';";
However, you really shouldn't print JS out with PHP if you can help it. Though iatboy's answer answer won't ultimately fix your bug, it is a much cleaner way of doing things.
?>
<script type=text/javascript>;
var hidden = false;
function actiondb1() {
if(!hidden) {
document.getElementById('clickdb1').style.visibility = 'hidden';
document.getElementById('db1').style.visibility = 'visible';
document.getElementById('db1').disabled = false;
document.getElementById('db1').value ='<?php echo $_SESSION["username"];?>';
}
}
</script>;
<?php
Did you start session in this page?If you didn't,use the follow code to start session.
session_start();
Then change your code to
echo "<script type=text/javascript>";
echo "var hidden = false;\n";
echo "function actiondb1() {\n";
echo "alert('".$_SESSION['username']."')\n"; //test code
echo "if(!hidden) {\n";
echo "document.getElementById('clickdb1').style.visibility = 'hidden';\n";
echo "document.getElementById('db1').style.visibility = 'visible';\n";
echo "document.getElementById('db1').disabled = false;\n";
echo "document.getElementById('db1').value ='".$_SESSION["username"]."';\n";
echo "}\n";
echo "}\n";
echo "</script>";
it will be ok.

Categories

Resources