I was making a school assignment with involves a shoutbox. A found great tutorial wich uses jquery,ajax,mysql and php. Now i run into a little problem with the following sentence:
$result .= "<li><strong>".$row['user']."</strong><img src="\" alt="\"-\"" />".$row['message']." <span class="\"date\"">".$row['date']."</span></li>";}
I was wondering if anybody could find out why it gives errors. So far I came to this conclusion $row['message'] and then it thinks the rest of the code as a string. So it probably is a apostrophe problem.
Just for the sake of making your life easier: use ' for the php and " for html like this:
$result .= '<li><strong>'.$row['user'].'</strong><img src="" alt=""/>'.$row['message'].' <span class="date">'.$row['date'].'</span></li>';
Pretty sure you should get the idea.
$result .= "<li><strong>{$row['user']}</strong><img src='http://www.' alt='My Alt Tag' />{$row['message']}<span class='date'>{$row['date']}</span></li>";
You're confusing yourself by coming in and out of quotations - you can wrap variables with {} to force the interpolation in such cases.
$result .= "<li><strong>".$row['user']."</strong><img src='' alt='-'/>".$row['message']." <span class='date'>".$row['date']."</span></li>";}
Avoid using " inside of the string - it is easy to forget about escaping special chars. Instead of " use '.
Besides - you use " only when there is any PHP parsing necessary within this string. E.g.
$var1 = 1;
$test = "$var1"; //evaluates to '1'
$test = '$var1'; //evaluates to '$var1'
It appears that you are attempting to escape quotes and making your job harder. A great feature in PHP for HTML output is using quoted strings so that you don't have to worry about escaping double quotes. Please reference the PHP Manual for Strings.
In other words your line becomes:
$result .= '<li><strong>' . $row['user'] . '</strong><img src="" alt="-" />' . $row['message'] .
'<span class="date">' . $row['date'] . '</span></li>' .
'<li><strong>' . $row['user'] . '</strong><img src="" alt="-" />' . $row['message'] .
'<span class="date">' . $row['date'] . '</span></li>';
Related
So I've created a CMS that takes text input. This is how the data is used when I grab it from the database.
echo "<img src=" . $row['image_url'] . " alt=" . $row['caption'] . ">";
Now the problem is, whenever there's a comma or a semi colon, php treats it as part of the code and the page ends up either not rendering well or completely breaking with errors like
Parse error: syntax error, unexpected '=' in
I've tried using htmlspecialcase() when posting the data to the MySQL database but it didn't fix the problem.
EDIT: The main problem is with the alt part not the src part.
When you're using double quotes (") to create a string literal and in that string literal you want to use double quotes ("), then you may escape those double quotes (") to form a valid string.
echo "<img src=\"" . $row['image_url'] . "\" alt=\"" . $row['caption'] . "\">";
You can try like that:
<img src="<?php echo $row['image_url'];?>" alt="<?php echo $row['caption'];?> ">
I'm sure I'm missing something obvious here: the following is echoing lat-long variables from MySQL, and the longitude variable begins with a minus sign, which prevents the echo statement from reading it and all that follows it. I'm sure there is a way to clean/escape that but just can't work it out.
echo "http://maps.google.com/maps?ll=" . $row['latitude'] . "," . $row['longitude'] . " target=_new>View in Google Maps";
This is output from a PDO query and testing passing the lat-long into Google Maps.
As I understand, it's a link?
Then, use urlencode for string.
The minus signs are not a problem. You may need to urlencode() because of the comma, but you need quotes around the URL in the href as well:
echo '<br /><a href="http://maps.google.com/maps?ll='
. urlencode($row['latitude'] . ',' . $row['longitude'])
. '" target="_new">View in Google Maps</a>';
I am having a problem in concating the PHP variable , have a look below
$attributes .= ' href="javascript:bookmarkscroll.scrollTo('.$varpost->post_name.')"';
I want the output like
javascript:bookmarkscroll.scrollTo('about-us');
Thanks All.
Just escape your quotes inside the js function,
$attributes .= ' href="javascript:bookmarkscroll.scrollTo(\''.$varpost->post_name.'\')"';
Here you go:
$attributes .= ' href="javascript:bookmarkscroll.scrollTo(\''. $varpost->post_name .'\')"';
Use json_encode. Keep in mind that JSON-encode will only escape " (not ') so make sure your output HTML uses only '.
$js = 'javascript:bookmarkscroll.scrollTo("' . $varpost->post_name . '");';
$attributes .= ' href="' . json_encode($js) . '"';
Ideally you'd use a more reliable way to generate the variable parts of JavaScript. In this case you would still use json_encode.
I want to create an HTML Message to send an email in PHP.
$message = $mess0 . "</br>" . $mess1 . "</br>" . $mess2 . "</br>" . $mes1 . "</br></br>" . $mes2 . "</br>" . $mes23 . "</br></br>" . $mes3 . "</br></br>" . $mes4 . "</br>" . $mes5 . "</br>" . $mes6 . "</br>" . $mes7 . "</br>" . $mes8 . "</br>" . $mes9 . "</br></br>" . $mes10 ;
$message = <html><body><p>$message</p></body></html>;
Here are some variables.
I am getting the following error.
Parse error: syntax error, unexpected '<' in /home/thevowaa/public_html/iphoneapp/webservice/file.php on line 214
Add HTML tags between double quotes.
$message = "<html><body><p>".$message."</p></body></html>";
Where are the double quotes see below
$message = "<html><body><p>$message</p></body></html>";
There are two possible ways to hold HTML in PHP variable. You can use single quote or double quotes. You also need to put a dot(.) before and after single/double quotes. Your PHP string could be constructed in following two ways:
$message = '<html><body><p>'.$message.'</p></body></html>';
or like this,
$message = "<html><body><p>".$message."</p></body></html>";
Also, use of single quotes(') is encouraged in PHP coding because it's doesn't clash with javascript or css double quotes(") when constructing html pages using PHP.
For more information on usage of quotes in PHP, check out this stackoverflow answer
This code is giving a syntax error. can anyone tell me where is the problem? thanks in advance.
echo "<div class='cvtitle'><div><a class="bloc_ca" href="'.$video['video_id'].'_'.str_replace(" ","-",substr(html_entity_decode($video['video_title']),0,20)).'.html"><b>".html_entity_decode(substr($video['video_title'],0,100))."..</b></a></div><div class='cvdisc'><span style='word-break:wrap'>".html_entity_decode(substr($video['video_desc'],0,100))."</span></div><div class='cvviews'> View Count: <b>".$video['views']."</b></div></div></div>";
You have to do escaping. Instead of:
echo 'some text' . "aaaa"aaaa";
write:
echo 'some text' . "aaaa\"aaaa";
Rewrite your example to something like this:
echo "<div class='cvtitle'><div><a class=\"bloc_ca\" href=\"" . $video['video_id']
. '_' . str_replace(" ","-",substr(html_entity_decode($video['video_title']),0,20))
. '.html"><b>'
. html_entity_decode(substr($video['video_title'],0,100))
. "..</b></a></div><div class='cvdisc'><span style='word-break:wrap'>"
. html_entity_decode(substr($video['video_desc'], 0, 100))
. '</span></div><div class="cvviews"> View Count: <b>'
. $video['views']
. '</b></div></div></div>';
p.s. code is a bit hard to read. Try only using one type of quotes to wrap around string and then another one can be used safely inside of that string.
Also - remember - if you wrap your string in ' or " - you have to escape this character inside of the string by adding backslash in front of it: \
http://php.net/manual/en/language.types.string.php
echo '<div class=\'cvtitle\'><div><a class="bloc_ca" href="'.$video['video_id'].'_'.str_replace(" ","-",substr(html_entity_decode($video['video_title']),0,20)).'.html"><b>"'.html_entity_decode(substr($video['video_title'],0,100))."..</b></a></div><div class='cvdisc'><span style='word-break:wrap'>".html_entity_decode(substr($video['video_desc'],0,100))."</span></div><div class='cvviews'> View Count: <b>".$video['views']."</b></div></div></div>";
There. You had some escaping issues. You were starting some strings with ' and ending them either with " or you were accidentally closing them without escaping
It is a classic case of mixing double quotes and single quotes, and forgetting to escape characters.
The string you return also seems to contain one extra </div>
echo '<div class="cvtitle">
<div>
<a class="bloc_ca" href="'. $video['video_id'] . '_' . str_replace(' ','-',substr(html_entity_decode($video['video_title']),0,20)) . '.html">
<b>' . html_entity_decode(substr($video['video_title'],0,100)) . "..</b></a>
</div>
<div class='cvdisc'>
<span style='word-break:wrap'>" . html_entity_decode(substr($video['video_desc'],0,100))."</span>
</div>
<div class='cvviews'>
View Count: <b>".$video['views']."</b>
</div>
</div>";
I'm getting this code:
echo "
".html_entity_decode(substr($video['video_title'],0,100))."..
".html_entity_decode(substr($video['video_desc'],0,100))."
View Count: ".$video['views']."
";