Nested quotes level 4 javascript error - php

I have the following code (I formated it to more lines, but in my source code I have it in one line, because innerHTML doesn't like new lines somehow - but that isn't the problem...):
<?php
echo "
<img
src='1.png'
onclick='
document.getElementById(\"my_div\").innerHTML=\"
<img src=\\\"1.png\\\" onclick=\\\"alert(\\\\\\\"text\\\\\\\");\\\" />
\";
'
/>
";
?>
And somewhere in the body I have :
<div id="my_div"></div>
So, when I click on the image, i'll have the same image inside my_div. The problem is, that when I click on the 2nd image, javascript doesn't alert anything.
But when I change this:
alert(\\\\\\\"text\\\\\\\");
to this:
alert(MyText);
and add JavaScript variable MyText:
<script>
MyText = "text";
</script>
it now works.
I think the problem is with those nested quotes:
\\\\\\\"
(level 4). Any ideas? Thanks.
EDIT: please don't post here another methods of doing this, I'd like to know why those quotes doesn't work here..
SECOND EDIT: I need that php there, because this is only a piece of my code (in full code I need it to display images in cycle...)

If you want a quote character as data (instead of as an attribute delimiter) in HTML, you represent it as " not \"

There's nothing "dynamic" in your script - you're not inserting PHP variables, so why build that all from within a PHP echo? Simply have:
Or if you want to make it even cleaner:
<script type="text/javascript">
function addImg() {
document.getElementById('my_div').innerHTML='<img src="1.png" onclick="alert(\'text\')" />';
}
</script>
<img src="1.png" onclick="addImg()" />

refactor your JS into an external file (with a function that will do the onclick logic), and try outputting something simpler with php's echo

Use jQuery!
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.2.min.js"></script>
<div id="my_div"></div>
<img src="1.png" class="my_img" />
<img src="2.png" class="my_img" />
<img src="3.png" class="my_img" />
<script type="text/javascript">
jQuery(function() {
$('.my_img').click(function() {
$('#my_div').html($(this).clone().unbind());
alert('text');
});
});
</script>

Related

Concatenate a style with a variable

Trying to use a variable with style to set a position.
Have researched it, but have yet to find an answer.
<div href="#" id="FabofLifeBox" style="position-top: "+ $ptop; +" position-left: "+$pleft; +" class="zoom">
the $ptop and the $pleft are the variables I am trying to set the position in the style statement.
Just open and close PHP tags and this is very straightforward.
... your php code ?>
<div id="FabofLifeBox" style="top:<?php echo $ptop; ?>px;left:<?php echo $pleft; ?>px;" class="zoom">
<?php ...your php code continues
Just mind that your are opening and closing PHP in the right places and this works.
ps... position-top and position-left are not valid CSS properties, I changed to top and left. div tag also does not support href attribute.
Try blending Javascript and PHP, an example of setting the position-top:
//php variable declared up here
<html>
<body>
<h2>Use JavaScript</h2>
<p id="FabofLifeBox">This some random text</p>
<script>
document.getElementById("FabofLifeBox").style="position-top:<?php echo $ptop ?>";
</script>
</body>
</html>
The PHP you added in the comments to the question would work (except for the missing semicolons between the style parameters), but there is no position-top or position-left parameter in CSS. Try something like this instead:
echo '<div href="#" id="FabofLifeBox" style="top: '. $ptop .'; left: ' . $pleft . ';" class="zoom">';

Convert all images into clickable A tags with img inside

I have a block of html with a lot of <img src="..."/> inside of it.
Now I'm looking for a way to make this:
<img src="lol.jpg"/> into:
<a href="lol.jpg" class="image_inside_text" target="_blank">
<img src="lol.jpg"/>
</a>
The idea is to make images open in a new page to view the full size image, and I'm adding a class in case I want to make it into a popup later on.
I'm looking to do this in PHP, can someone please help me?
Thank you
He asked for solution in PHP (see tags).
$string = <<<EOD
<img src="lol.jpg">
<img src="lol.jpg"/>
<img src="lol.jpg" alt="sufix"/>
<img alt="prefix" src="lol.jpg"/>
EOD;
$pattern = '/<img[^>]+src="([a-z.]+)"[^>]*>/i';
$replacement = '${0}';
echo preg_replace($pattern, $replacement, $string);
Online version: http://ideone.com/pY2EWY
Well, just use some jquery, try this :
<script>
$(function(){
$('.thisimg').replace('<img src="your src" class="thisimg" />');
});
</script>
<img src="your src" class="thisimg" />
There are several ways of doing so... Most involving Javascript.
Take a look at jquery's method wrap(), that wraps any selected element into another element:
$("img").each(function() {
var href = $(this).attr("href");
$(this).wrap("<a href='"+href+"' class='image_inside_text' target='_blank'></a>");
});
http://api.jquery.com/wrap/
You can use jQuery:
$(function(){
$('img').each(function() {
var src = $(this).attr("src");
$( this ).replaceWith('<img src="' + src + '"/>')
});
});
You can use output buffering: ob_start and do something similar to this example and replacing the callback with what test30 suggested:
<?php
function callback($buffer)
{
// replace all the apples with oranges
return (str_replace("apples", "oranges", $buffer));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing apples to oranges.</p>
</body>
</html>
<?php
ob_end_flush();
?>

How to write a dynamic text using javascript

I am trying to accomplish something like this :
<?php $url = 'http://www.mydomain.com/img/picture.jpg'; ?>
<img src='<?php echo $url ?>' />
Only the problem is I want to accomplish this using javascript. Something like this
<script>url = 'http://www.mydomain.com/img/picture.jpg'</script>
<img src='<script>document.write(url)</script>' />
Of course it wouldn't work, but you got the idea. Any thought ?
Try - http://jsfiddle.net/m2Prh/
<img id="image" />​
<script>
var url = 'http://lorempixel.com/300/200';
document.getElementById("image").src = url;​
</script>
I suggest reading jquery ,you can acesss every html element

html template if statement

PHP code (functions.php)
$main = $db_sql->query_array("SELECT catid,titel,subcat,startorder FROM $cat_table WHERE catid='$subcat'");
$tpl->register('category_title',stripslashes($main['titel']));
$tpl->register('category_id',$main['catid']);
So I want to add an if statement where if {category_id} is not 0 then do the following code
<img src="{GRAFURL}/{category_id}.jpg" alt="{category_title}" width="50" height="50" border="0" align="middle" /> {category_title}
what would be the correct syntax?
need more code still for the html file?
HTML doesn't have dynamic features like if-statements. Think of HTML as a language to define a layout - unchangeable using HTML itself.
This should do the trick:
<?php
if ( false === is_empty( $category_id )) {
echo "<img src='{$GRAFURL}/{$category_id}.jpg' alt='{$category_title}'
width='50' height='50'
border='0' align='middle' /> {$category_title} ";
}
?>
In case you need to add client-side dynamic features to your HTML-pages, have a look at jQuery or jQuery UI.
HTML has no such capability. You would need to generate your HTML using a programming language. Then the syntax would depend on which language you were using.
e.g. in TT:
[% IF category_id %]
<img src="[% GRAFURL %]/[% category_id %].jpg"
alt="{category_title}" width="50"
height="50" border="0" align="middle"
/> [% category_title %]
[% END %]
Use javascript for generating it client side, PHP for server side. Javascript can change things on the page and provide dynamic content after the page is sent to the client; PHP can determine the content before sending it to the user (while it can edit it afterwords, it is much more convoluted).
For example, if you wanted to add it to a tag with id "dynamic", you could use this:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var img_tag = document.createElement('img');
img_tag.src = "{GRAFURL}/{category_id}.jpg";
img_tag.alt = "{category_title}";
img_tag.width = 50;
img_tag.height = 50;
img_tag.border = 0;
img_tag.align = "middle";
document.getElementById("dynamic").appendChild(img_tag);
};
</script>
</head>
<body>
<div id="dynamic">
</div>
<body>
</html>
I thoroughly recommend learning jQuery, however, where you could do things easier and faster. Also, it is up to you to replace the values in braces with the correct values (either through PHP or javascript), I couldn't tell what you were doing.

click link with javascript

I have a link on my page that id like to click with the below javascript function, but it isnt working. What I am really trying to do is use php to echo out a preclicked link. I think I have used the right function $(selector).click(), but I dont know where to put the link. I dont want to echo out the link, just the alert message. The link is actually a thickbox alert message, that is only able to be clicked on. I was hoping that i could use the .click() to activate it via php. thanks
<?php
echo "
<script type='text/javascript'>$('#link').click();</script>
<a href='wronginput.php?height=40&width=80' id='link' class='thickbox'>Link text</a>";
?>
At the time you output the javascript snippet, the link has not yet been parsed into the DOM, so $('#link') returns a null object. Either wrap it in a .ready() call, or place the javascript AFTER the link in your output.
<script>$(document).ready( function() { $('#link').click(); });</script>
<a href=...>
or
<a href=...>
<script...>
as Marc B said
<?php
echo "<a href='wronginput.php?height=40&width=80' id='link' class='thickbox'>Link text</a>
<script type='text/javascript'>$('#link').click();</script>
";
?>
or
<?php
echo "<script>$(document).ready( function() { $('#link').click(); });</script>
<a href='wronginput.php?height=40&width=80' id='link' class='thickbox'>Link text</a>
";
?>

Categories