How to check if a constant is defined in main layout? - exception

I've this snippet of code in main layout file (protected/views/layouts/main.php):
<?php if (defined('YII_DEBUG')) { ?>
<button id="search-hidden-button"></button>
<script>
$(function(){
$('#search-hidden-button').click(function(){
$('#search-query-form').submit();
});
});
</script>
<?php } ?>
But I get this error and I dont know why:
Use of undefined constant - assumed ' '
<?php if (true) { ?>
<button id="search-hidden-button"></button>
<script>
$(function(){
$('#search-hidden-button').click(function(){
$('#search-query-form').submit();
});
});
</script>
<?php } ?>

You pressed a combination of ALT-GR + SPACE that your editor shows as a normal space but in fact is a special character that PHP can't decode.
This happens usually on Italian Keyboards, where "{" is printed with ALT-GR + SHIFT + [.
When you type really fast (as good programmers do :) you do like this
Press shift and ALT-GR almost at the same time
Press [
Release [
Release ALT-GR and SHIFT
Press Spacebar
Sometimes it happens that step 4 and 5 are switched, and you get your unknown character.
Remove all spaces in your IF statements and try again, it will be fixed (then re-space correctly!)

No need defined again, try :
if(YII_DEBUG){}

Related

Summernote executes escaped html

I fetch data from a MySQL database, the data stored is this:
<p><script>alert('123');</script><br /></p>
When I fetch the data normally I get this as result:
<script>alert('123');</script>
This is fine and works as expected, however when I fetch the data into a textarea which is initialized with Summernote I get an alert like this:
Somehow Summernote converts the escaped html tags to functioning HTML.
How do I fix this?
I have already tried the answer of this question:
Escaped HTML in summernote
It did not work.
Why are you not sanitising data both at the time of storage, and when displayed in the Editor, or outside of the editor? Typically, in my CMS, I don't allow <script/> tags as way to help mitigate users adding potentially dangerous scripts.
That said, there is a PR that is being discussed about how we can best go about fixing this issue. https://github.com/summernote/summernote/pull/3782 information or help would be greatly appreciated to move it along, or even another PR fixing the issue.
I managed to fix it by instead of fetching the data in the textarea fetching it in via jQuery like this:
<textarea name="description" id="description"></textarea>
<script>
$('#description').summernote({
height: 250,
codeviewFilter: false,
codeviewIframeFilter: true,
// toolbar
toolbar: [
['font', ['bold', 'italic', 'underline', 'clear']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['view', ['fullscreen', 'codeview', 'help']]
],
}).on("summernote.enter", function(we, e) {
$(this).summernote('pasteHTML', '<br />&VeryThinSpace;');
e.preventDefault();
});
$("#description").summernote("code", "<?php echo $video->getDetails('', $fileName, 'desc'); ?>");
</script>
Now it doesn't convert > and $lt; to <> if it is the script tag.
See more information here:
https://github.com/summernote/summernote/pull/3782#issuecomment-774432392
Using javascript you can easily fix this. It worked for me in a React + Django project. I also used django_summer_note and it was also showing data like yours. Then I got that solution:
//simply just create a function like this which will return your data (which one you used with django_summernote).
const createBlog = () => {
return { __html: blog.description };
};
// now in your HTML(JSX) show your data like this.
<div className='' dangerouslySetInnerHTML={createBlog()} />

Json query - could not get results from array

I just making small code for tracking app to my website.
I'm pretty new at json so I could not find out what is wrong at my code. It's been two days now:
Here is the code
<http://code.jquery.com/jquery-latest.min.js>
<script type="text/javascript">
$(document).ready(function() {
var inputField = $('#tracking');
var outputElement = $('#textResult');
inputField.keyup(function() {
if (inputField.val().length > 1) {
$.getJSON('http://sporing.bring.no/sporing.json?q=' + inputField.val(),
function(data){
outputElement.html('ID' + data.consignmentSet.consignmentId);
});
} else {
outputElement.html('No result!');
}
});
});
</script>
<div>
<input type="text" id="tracking" style="width: 17;" maxlength="30"/><br/><span id="textResult"></span>
</div>
</body></html>
My source is: http://developer.bring.com/api/trackingapi.html#json
Tracking json source: http://sporing.bring.no/sporing.json?q=TESTPACKAGE-AT-PICKUPPOINT
Links gives result, but i'n not able to display it.
It is simple code, so hopefully someone can explain me what I'm doing wrong...
Thanks
data.consignmentSet contains an array with one element so
data.consignmentSet[0].consignmentId
should work!
You can use JSONLint to validate and pretty print your JSON to better dig into it's structure.
Also, you could use a debugger to explore the content of the data at runtime. Most browsers have their F12 developer tools or you could install Firebug. Then switch to the Script tab, go to the line in code where you access the data and click on the line number. A breakpoint will be set where code execution will stop when running the script. When the breakpoint is reached, you can explore the data in the Watch window.

Conflict between jQuery Tools and another jQuery script

I'm using jQuery Tools(specifically, the form validator) and a JQuery Facebook-related script on one page of my website.
Each script requires referencing both an external file in the "head" of my HTML as well as a separate script in the "body" of my HTML.
Here is my code for the scripts in the "body" of my HTML (simplified):
First Script (Facebook script)
`
function init() {
FB.api('/me', function(response) {
$(".s_name").replaceWith('<input type="hidden" name="s_name" id="' + response.name + '" value="' + response.name + '" />');
..do more replaceWith stuff
});
}
//Live update of page as user selects recipient and gift options
$(".jfmfs-friend").live("click", function() {
var friendSelector = $("#jfmfs-container").data('jfmfs');
...do stuff});
`
Second script (jQuery Tools - validator)
`
$("#form").validator({
position: 'top left',
offset: [-5, 0],
message: '<div><em/></div>',
singleError: true
});
`
Everything works correctly until the .click function of the first script is activated. At that point, the validator script stops working. I believe the issue is related to conflicting jQuery $'s, but I'm not sure how to fix it. I've tried using jQuery.noConflict() in various areas, but I haven't been successful and I'm not exactly sure how I should be using it.
Any help would be greatly appreciated!
Try using 'jQuery' in place of all '$' like so:
jQuery(document).ready(function() {}); as against $(document).ready...
I was forced to use the long version during a "conflict" of interest situations.

GetJSON and using a Show More link

I'm using getJSON to get data from the facebook pages api, and it works just fine, using this code:
$(document).ready(function(){
$.getJSON('url',function(json){
$.each(json.data,function(i,fb){
var output='';
//here I add to output, as this example line:
output += '<div"><a href="http://www.facebook.com/profile.php?id='+fb.from.id+'>'+fb.from.name+'</a>';
$("#results").append(output);
});
});
However, what I'd like to do is similar to what facebook does in it's social plug in where it starts off with 5 entries and has a Show More link, which when clicked, brings in 5 more entries.
Is there a way to do this by altering the code I have?
Thanks
Well, sure there is. Do you want to fetch the other results when a user clicks the "more link" to save bandwidth or is it OK to fetch it at the same time? (async vs sync)
This answer considers the bold text:
output += '<div' + (i >= 5 ? ' style="display: none;"' : '') + '><a href="http://www.facebook.com/profile.php?id=' + fb.from.id +'>'+fb.from.name+'</a></div>';
Oh, and check that line in your code, you had a syntax error and an unmatched div. Also you should have quotation marks around your HTML element's attributes.
For showing the links when the more link is clicked you could do something like:
$('.more').click(function() {
$(this).hide();
// Find the closest ancestor which is a parent of both
// the more link and the actual results list
var $parent = $(this).closest('.parentSelector');
$('.listSelector', $parent).children().show();
return false; // Don't follow the link
});
The parts with the parent stuff above is for the case when you have multiple such results list on the same page and you need to separate them. If you don't need it, here is a simpler variant:
$('.more').click(function() {
$(this).hide();
$('#results').children().show(); // Show all other list items
return false; // Don't follow the link
});

how do I display a variable w/ jquery?

This is a fairly newbish question, so apologies in advance.
I want the scrolltimes variable to update each time the user scrolls the page. I've tried a half dozen different ways and can't quite figure out how to make the variable show up. I've managed it w/ append/prepend, but that doesn't remove the old version... do I need to remove the previous version and add in the new one each time or is there a way I can display the <p>Scrolled: N</p> substituting the value of scrolltimes for N?
Script:
var scrolltimes = 0;
$(document).ready(function() {
$('#clay').scroll(function() {
$('#scrollamount p')
.html({'<p>Scrolled: '+ .scrolltimes++ + '</p>'});
});
html:
<div id="clay" style="height:100px; overflow:scroll;">
<div id="scrollamount">
<p>Scrolled: 0</p>
</div>
<p>Pretend there is lots of content here</p>
</div>
You have a few syntax errors in your script. Try this:
var scrolltimes = 0;
$(document).ready(function() {
$('#clay').scroll(function() {
$('#scrollamount p').html('<p>Scrolled: '+ ++scrolltimes + '</p>');
});
});
The errors were:
Closed the scroll event function, but not the ready function - I added a }); to the end
Unnecessary dot at the beginning of the scrolltimes variable - I removed the .
Don't need to put the argument to the html call in an object - I removed the { and }
In addition to that, in order to make your output correct, I moved the increment on the scrolltimes variable from post-increment to pre-increment. Thay way the variable gets incremented before it is shown and now the first scroll event shows 1 instead of 0. The same thing could have been achieved by initializing var scrolltimes = 1;