Implementing fancybox onclick() - php

<?php
echo '<td class="options-width">'.
'<a href="edituser_lightbox.php?user_id=' . $row['user_id'] . ' " onclick:" $.fancybox({ href:'example.jpg',title : 'Custom Title'});"title="Edit" class="icon-1 info-tooltip" </a>'.
' '.
' '.
'</td>';
echo "</tr>";
}
?>
I have to atleast be close here guys? Can you see what im trying to do? I can't change my css to include fancybox either :(

The onclick attribute should be specified with an =, not : as in:
echo '<a href="edituser_lightbox.php?user_id=' . $row['user_id'] . '" onclick="$.fancybox({ href:\'example.jpg\',title : \'Custom Title\'});" title="Edit" class="icon-1 info-tooltip" </a>';
Inside the fancybox statement, you will also need to backslash-escape the single quotes so they are presented as literal ' in the output HTML. You also had an extra space before the closing " in the href= attribute.
It is not considered good practice to place the JavaScript inline inside the tag. Instead it would be better to bind it as in:
$("a#editLink").click(function() {
$.fancybox({ href:'example.jpg',title : 'Custom Title'});
});
Note, you would need to add the id="editLink" to the <a> tag. (It could be any id, or various other jQuery selectors instead of an id.
echo '<a id="editLink" href="edituser_lightbox.php?user_id=' . $row['user_id'] . '" onclick="$.fancybox({ href:\'example.jpg\',title : \'Custom Title\'});" title="Edit" class="icon-1 info-tooltip" </a>';

Looks like a typo:
onclick:"
Make the colon an equal sign:
onclick="
Also, ensure that the object's attributes' values will be strings by adding quotation marks.
onclick='$.fancybox({href: "'example.jpg'", title: "'Custom Title'"});'

Related

How to correctly enter the data-target id in php echo

I am trying to open a bootstrap modal window containing product image and information. My problem appears to be the #uoc-modal inside the data-target. Can some please tell me how to correctly enter the data-taget id inside an echo?
echo '<a "data-toggle='modal'", "data-target='#uoc-modal'" href="' . get_the_permalink() . '">';
From your question code, you should escape it
echo '<a "data-toggle=\'modal\'", "data-target=\'#uoc-modal\'" href="\' . get_the_permalink() . \'">';
But, I think this should work
echo '<a data-toggle="modal" data-target="#uoc-modal" href="'.get_the_permalink().'">Modal</a>';
But anyways, you don't need data-target attribute for <a> element. Just use href instead.
Try this code:
echo '<a "data-toggle=\'modal\'", "data-target=\'#uoc-modal\'" href="\' . get_the_permalink() . \'">';
Good evening

div onclick isn't working

I'm trying to get the caption part of my slider to link to the respective article url on my wordpress site. I'm pretty sure I found the section in the plugin code I think needs to be edited, but when I try to do the following:
<?php
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . onclick="location.href='$url';"</div>";
?>
I get "syntax error, unexpected T_STRING, expecting ',' or ';" showing up in my slider, and the rest of my site not working. I've tried many variations of what I've tried to do here, but I can't seem to find one that works.
Here's the original entire slider code if it's helpful: http://pastebin.com/4nKxXkSa
Your opening/closing of double-quotes is wrong : in PHP, strings must be enclosed in either quotes or double-quotes ; and if you want to put a double-quote in a double-quotes enclosed string, you have to escape it with a \.
For example, you need to open a quote or double-quote before onclick, as this is part of a string.
Also, your onclick should be inside the <div ...> tag, and not between <div> and </div>.
In the end, your PHP code would look a bit like this (I've set up hard-coded values for the variables, to help with testing) :
$sl_caption = 'ID';
$sl_htmlcaption = 'HTML';
$url = "URL";
echo "<div id='"
. $sl_caption
. "' class='nivo-html-caption' onclick=\"location.href='$url'\">"
. $sl_htmlcaption
. "</div>"
;
And you'd get the following HTML as output :
<div id='ID' class='nivo-html-caption' onclick="location.href='URL'" >HTML</div>
Your string quotes are not correctly placed. Try this
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . "onclick=\"location.href='$url';\"</div>";
The problem is what it says - "onclick" isn't a quoted string in your example (T_STRING in PHP). Everything you join together (concatenate) in a string needs to be either a) a string in quotes (single or double), b) something that can be converted to a string or c) a variable/constant/function call.
If you didn't have that error then your current example would also have the "onclick" as the content of the tag, rather than an attribute on the tag. What I think you want is:
<?php
echo "<div id='" . $sl_caption . "' class='nivo-html-caption' onclick='location.href=\'" . $url . "\''>" . $sl_htmlcaption . "</div>";
?>
If you want standard HTML attributes then you'd normally use double-quotes, which would give you:
<?php
echo '<div id="' . $sl_caption . '" class="nivo-html-caption" onclick="location.href=' . $url . '">' . $sl_htmlcaption . '</div>';
?>
Also, is there any reason why you're using an onclick to set the current location rather than a normal <a href>?
the qoute isn't the only problem. The onclick is also out of the div element's properties. It must be (changes the doubles for singles to make it more clear):
echo '<div id="' . $sl_caption . '" class="nivo-html-caption" onclick="location.href=' . $url . ';">' . $sl_htmlcaption . '</div>';
Your quotes are the problem. It should be:
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'>" . $sl_htmlcaption . "onclick=\"location.href='$url';\"</div>";
You needed double quotes to start the string again after the concatenation operator (.), but then you need to escape the double quotes inside this string wit a blackslash so the PHP interpreter won't think the string has ended too soon.
And as Pascal pointed out, the onclick attribute should actually be inside the stating div tag anyway:
echo "<div id='" . $sl_caption . "' class='nivo-html-caption'onclick=\"location.href='$url';\">" . $sl_htmlcaption . </div>";
Using interpolation may make things easier for you also. When using double quotes to delimit a string you can insert variables' values using curly brackets ({}) like this:
echo "<div id='{$sl_caption}' class='nivo-html-caption'onclick=\"location.href='{$url}';\">{$sl_htmlcaption}</div>";
This way you only have to open and close the string once (at the beginning and end).
Beware:
You cannot interpolate function calls directly. You can either concatenate like "<p>".strlen($x)."</p>" or store the result in a variable and the interpolate like before; "<p>{$result}</p>".
You'll still have to escape double quotes within the string though.

pass link text to next page

For my actual web-project, I made a tagcloud. When I click on a tag, it should be passed to the page showRecipesFromTags.php.
<form name="tagform" id="tagform" method="get" action="/showRecipesFromTags.php">
<?php
include('php/getRandomTags.php');
$tagsarr = json_decode($return4634, true);
foreach ($tagsarr['Data']['Tag'] as $key => $tag11) {
echo '<a id="seastags" href="#" onclick="document.tagform.submit()"><font size="' . rand(1,4) .'">' . $tag11['Tag_name'] . '</font> <br/></a> ';
}
?>
</form>
It can already go to this page by clicking on the href, but what I need is to pass the id of the clicked tag. The name of the tag is called Tag_name. The name is shown but I don´t know how the pass the id to the next site. The id is in my JSON array and is called Tag_id.
How can I manage this?
Why do you even need a form? Just use plain links.
Try using this in the foreach loop:
echo '<a id="seastags" href="/showRecipesFromTags.php?tagId=' .$tag11['Tag_id'] . '"><font size="' . rand(1,4) .'">' . $tag11['Tag_name'] . '</font> <br/></a> ';
Your choice are:
use a hidden field
put it in the URL
use a cookie

PHP/ Javascript inserting html with ' and " to a javascript function from PHP

I have HTML in a database which I want to show with facebox (jquery popup).
I use the PHP code below to render the button that launches the facebox
$html.="<img onclick='$.facebox(\"".$db_data[html]."\");' src='img.png' />";
How can I escape things properly so that facebox will get also ' and " in $db_data[html]?
(for example if the html includes styles?)
Use json_encode to convert the string properly to a JavaScript compatible string and htmlspecialchars to encode it for the use in an HTML attribute value:
"<img onclick='" . htmlspecialchars("$.facebox(".json_encode($db_data['html']).");", ENT_QUOTES) . "' src='img.png' />"
Note the use of the quote style ENT_QUOTES to also encode ' that are used to quote the attribute value. This wouldn’t be necessary if you would use " for the HTML attribute value instead:
'<img onclick="' . htmlspecialchars("$.facebox(".json_encode($db_data['html']).");") . '" src="img.png" />'

php quoting problem

I have this PHP code
echo 'Link 1';
which generates a link like this:
Link 1<li>Link 2</li>
Causing the javascript to not be called. How would I make it generate single quotes around the result of $query, in this case ed hardy?
You should html encode it:
echo 'Link 1';
You could also use htmlspecialchars
echo "<a href='#' onclick='updateByQuery(\"Layer3\", \"" . json_encode($query) . "\");'>Link 1</a>";
This produces:
<a href='#' onclick='updateByQuery("Layer3", "Ed Hardy");'>Link 1</a>
Try to do the reverse... use single quotes for html, and double quotes for javascript. That's how we do that in fact.
echo "<a href='#' onclick='updateByQuery(\"Layer3\", " . json_encode($query) . ");'>Link 1</a>";
Quotes are a problem with inline handlers. As RoBerg says, you need to use htmlentities in the text.
Another way around it is to use hook methods and anonymous functions, rather than inline handlers.
echo '
Link 1
<script>document.getElementById("link_1").onclick =
function() { updateByQuery("Layer3", '.json_encode($query).'); }
</script>
';

Categories