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.
Related
Ok, so I have the php echo working and pulling specific url's from the mysql database but this string that I got from an example is adding in single quotes to my href. So instead of being localhost/newdesign/about.php it is localhost/newdesign/'about.php'.
here is the code:
<p>
<?php
echo '' . $row['url'] . '';
?>
</p>
Thank You
It would be simpler if you use <?php echo only around the variables, not the literal HTML parts as well. It also looks like part of the onclick got lost when you were copying to SO.
<p><?php echo $row['url'] ?></p>
No need for the 2nd apostrophe.
"' . $row['url'] . '"
I am having trouble with my PHP code. I've been changing everything for 6 hours and I still get Parse errors no matter what I do. This is the code:
$slider3 = '<img src="'templates/' . $this->template . '/images/slider/slider3.jpg'">' . '" alt="' . $sitename . '" />';
The only way I can figure to not get it to throw an error is by writing it this way:
$slider3 = '<img src="templates/" . $this->template . "/images/slider/slider3.jpg" . "/>"';
but I don't think that's right.
I want $slider3 = "templates/MYTEMPLATE/images/slider/slider3.jpg" then later I will echo $slider3;
I get so confused with all the single and double quotation marks. I think the first one is right - I look at it and study it and it looks right to me. But it throws a parse error.
$slider3 = '<img src="templates/'.$this->template.'/images/slider/slider3.jpg"/>';
should work.
Explanation:
'<img src="templates/'
is a single-quoted string, which happens to contain a double-quote (which is needed for the html src attribute, or any other html attribute value really)
.
(dot) is the string concatenation operator. It concatenates ("glues") the first string together with...
$this->template
which is presumably a string containing the name of the template (not clear from your code example). Note that if $this->template comes from user input, or an otherwise unvalidated source, it could be used for cross-site scripting, eg. if it contains "><script>alert("XSS!")<script>, javascript is executed in the browser!
.
another concatenation with...
'/images/slider/slider3.jpg"/>'
which is another single-quoted string which happens to contain a double-quote, ending the src attribute value.
Try this:
$slider3 = '<img src="templates/"' . $this->template . '"/images/slider/slider3.jpg"/>';
$template = "MYTEMPLATE";
$slider3 = '<img src="templates/'.$template.'/images/slider/slider3.jpg"/>';
echo $slider3;
Will echo - >
<img src="templates/MYTEMPLATE/images/slider/slider3.jpg"/>
Just write:
<?php
$templates = "var";
echo "<img src='templates/${templates}/images/slider/slider3.jpg'/>";
it will result in
<img src='templates/var/images/slider/slider3.jpg'/>
I'm trying to pass on the parent url variables to the variables of the iframe url.
For example if the parent url is:
http://mywebsite.com/autos-zoeken?sessid=s838c7e5c3be0452fc38f4ffb6f307ed7&code=be3f&whsearch_key=6568
The iframe url needs to become:
http://anotherwebsite.com/s838c7e5c3be0452fc38f4ffb6f307ed7/be3f/stock/6568/
The code I'm using now is:
<?php
$val1 = $_GET[“sessid“];
$val2 = $_GET[“code“];
$val3 = $_GET[“whsearch_key“];
echo "<iframe src='http://anotherwebsite.com/' . $val1 . '/' . $val2 . '/stock/' . $val3 . '/' id='blockrandom' width='1000' height='1200'
scrolling='auto'
frameborder='0'
class='wrapper'>
Your browser doesn't support inline frames.</iframe>";
?>
The result on the website is:
http://anotherwebsite.com/' . . '/' . . '/stock/' . . '/' id='blockrandom1' etc
So the variables aren't being put in the right place in the iframe url
What am I doing wrong here?
this should work fine:
echo "<iframe src='http://anotherwebsite.com/$val1/$val2/stock/$val3/' id='blockrandom' width='1000' height='1200' scrolling='auto' frameborder='0' class='wrapper'> Your browser doesn't support inline frames.</iframe>";
the problem is with all your quote's inside your src, it thinks it needs to stop your src after http://anotherwebsite.com/ and you don't need to use "." inbetween because you used doubleqouote on start, you can just use variables inside doublequotes.
echo "<iframe src='http://anotherwebsite.com/$val1/$val2/stock/$val3/' id='blockrandom' width='1000' height='1200' scrolling='auto' frameborder='0' class='wrapper'>Your browser doesn't support inline frames.</iframe>"; ?>
you don't need to escape your variables
You're not actually concatenating multiple strings, you're just building a single string. Take a look at a simplified example:
"<iframe src='http://anotherwebsite.com/' . $val1"
That's just one string which happens to have a period between some spaces. In order to use the . concatenation operator you need to terminate the string first:
"<iframe src='http://anotherwebsite.com/'" . $val1
I have this php line which works fine:
echo "<p>" . $post['message']. "</p>";
But I want to change it so it will link to my page (not to a single post). So it should look like that.
echo "<p>" . $post['message']. "</p>";
I have tried a lot many proposition gathered on different website, but each time I am getting an error.
Any idea ?
Thanks a lot!
Using single and double quotes, you avoid escaping issues. Try this:
echo '<p>'. $post['message']. '</p>';
i see that you didn't escaped from double quote that closes href attribute:
echo "<p><a href=\"https://www.facebook.com/rscmovement\" target=\"_blank\">"
I guess You have missed the back slash () before " after www.facebook.com/rscmovement.
"https://www.facebook.com/rscmovement\" "\"target=\"_blank\">" will
<?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'"});'