php quoting problem - php

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>
';

Related

PHP : echo a href

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

JavaScript confirm box not appearing on PHP page

Can anyone see why this line in PHP doesn't work. The game card is deleted, but the confirm box does not appear for the user to okay or cancel.
echo "<a href='gamecard.php?selection=" . $row['gamedate'] . " onclick='return confirm('Delete game card?');'>Delete</a>";
Thank you.
You have single quotes inside of single quotes, you need to escape 'em. You also forgot the closing quote on the href attribute.
echo "<a href='gamecard.php?selection=" . $row['gamedate'] . "' onclick='return confirm(\"Delete game card?\");'>Delete</a>";
echo "Delete";
codepad example
echo "Delete";
will do it. HTML attributes, as a general practice, should be double quoted. JS strings should be single quoted.
You have some issues with missing quotes and using double quotes when you should be using singles (and vice/versa). This should do the trick.
echo "<a href='gamecard.php?selection=" . $row['gamedate'] . "' onclick='return confirm(\"Delete game card?\");'>Delete</a>";

How to PHP echo when using multiple quotes

How should I quote this:
<tr onclick="$.colorbox({href:'information1.html'});">
When put in an echo " "; ?
I have tried this:
echo "<tr onclick='$.colorbox({href:'information1.html'});'>";
Which shows a Jquery error.
And I tried this:
echo "<tr onclick="$.colorbox({href:'information1.html'});">";
Which shows a PHP error.
Any workarounds? Thanks
You need to escape the quotes symbols:
echo '<tr onclick="$.colorbox({href:\"information1.html\"});">'
Note that using inline script is not considered to be a good practice!
echo '<tr class="foo">'
In the javascript code:
$('.foo').click(function() {
$.colorbox({ href: "information1.html" });
});​
Simply escape the quotes. Whilst on this subject I feel it important to mention the fact that generally speaking, you should use single quotes for 'code' and double quotes only for displayed strings.
This stems from C standards and keeping this consistent will help you in the future if for example you wanted to implement gettext() and translate your website into multiple languages.
echo '<tr onclick="$.colorbox({href:\'information1.html\'});\">';
Having said that, there's a better way to achieve what you're doing. Give the row an id:
<tr id="inforow" />
And use jQuery to bind to it's click event when the DOM is ready.
$(document).ready(function() {
$(".inforow").click(function() {
$.colorbox({href:'information1.html'});
});
});
Anytime you want to print a string with a quote in it, just use the escape character '\' to ignore the quote as a literal closing quote, like so:
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
Try that:
echo "<tr onclick=\"$.colorbox({href:'information1.html'});\">";
I would use PHP-methods instead of caring about the quotes
echo '<tr onclick="'.
htmlentities('$.colorbox('.json_encode(array('href'=>'information.html'))).')">';
...will always create proper JSON and proper HTML, no matter what characters you use.
NO NEED to quote it.
NO NEED to put in an echo " ";
Just leave it AS IS:
?>
<tr onclick="$.colorbox({href:'information1.html'});">
<?
as well as any other HTML.
It's PHP. It's embedded in HTML. You can leave PHP mode any time
Another way is using EOD
$string = <<<EOD
"duble quotation" and 'quotation' all enable
EOD;
echo $string;

PHP convert javascript when echo

I have following PHP echo statement:
echo "<td><a href='delete-news.php?deleteID=".$id." onclick='return confirm('Really delete?');'>Delete</a></td>";
which is convert to html as:
<td class="last-td nth-8"><a delete?');'="" confirm('really="" return="" href="delete-news.php?deleteID=5 onclick=">Delete</a></td>
As you can see something has gone wrong?
What is the problem? I have already tried swaping " " for single ' '.
Your have double single quotes in the onclick statement, try confirm(\'Really delete?\') instead.
You have forgot ' after href. Use it like
Double quotes:
echo "<td>Delete</td>";
Single quotes:
echo '<td>Delete</td>';
You have href not closed. Also, your 'Really delete' cause trouble too. Try this
echo "<td><a href='delete-news.php?deleteID=$id' onclick='return confirm(\"Really delete?\");'>Delete</a></td>";

php javascript ajax weird string error

I am building some html in a php script in order to send it back to a page via Ajax.
$other_content = Model_Db::dbSelect($query);
$div_center .= "<table>";
for($i = 0;$i<sizeof($other_content);$i++) {
$div_center .= "<tr>";
$div_center .= "<td><a href='#' onclick='changediv('test','0')'>".$other_content[$i]->content_name."</a></td>";
$temp = "<td><a href='#' onclick='changediv('test','0')'>".$other_content[$i]->content_name."</a></td>";
die($temp);
$div_center .= "</tr>";
}
$div_center .= "</table>";
As you can see I am doing a die() to see the created string.
My ouput should be something like: <a href="#" onclick="changediv(" test','0')'>Content Name</a>
But instead I get: Content Name
I do not understand where this ="" comes from after my onclick declaration...
Can anybody see what's wrong here? I am a bit puzzled as I really don't see where it could come from!
Cheers
I would suggest you to escape the quote characters
$temp = "<td><a href=\"#\" onclick=\"changediv('test', '0')\">" .
$other_content[$i]->content_name. "</a></td>";
The \" escapes the double quoute in a string
You messed up the function interpretation of single-quote marks as double-quotes:
yours <a href='#' onclick='changediv('test','0')'>
func <a href="#" onclick="changediv(" test','0')'="">
It assumes this (between % signs) %test','0')'=""% is a parameter of your tag, try substituting single-quotes with double-quotes and make it html/xhtml compliant:
<a href="#" onclick="changediv('test','0')">
So single-quotes and double-quotes will be correctly set.
You have to change PHP quotes too

Categories