PHP convert javascript when echo - php

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

Related

How to print a variable in href with php?

I'm trying to pass an id in a link href and I need it to be printed in the URL, my code is:
echo " $ville_nom ";
But the id is not print in the url, could you help me with the syntax ?
You have mistake in your usage of echo and your concat was wrong.
The corrected code :
echo "<a href='ville.php?id='".$ville_id."'> $ville_nom ";
When you used the double quote it's not necessary to concat variables to show their content. Be careful: it can be dangerous sometimes.
echo "<a href='ville.php?id=$ville_id'> $ville_nom ";
Try to use this:
echo "<a href='ville.php?id=" . $ville_id . "'>"

Escape the double quotes in css inline style in php echo

I am sorry if this is duplicated but I could not find an answer to this situation. I am trying to escape the double quotes from the inline style in the PHP code.
<?php if (isset($ioTitle)){
echo "<div style='background-color: echo $params->get('colorbgt'); ;' class=\"ioTitleBox\"><h3 class=\"ioTitle\">";
echo $params->get("ioTitle"); echo "</h3></div>";
}
You can't put echo inside a string and expect it to be executed. You need to use concatenation.
echo "<div style='background-color: " . $params->get('colorbgt') . ";' class=\"ioTitleBox\"><h3 class=\"ioTitle\">";
You don't need to escape anything in the style for this. Also, you can use single quotes around the classes, to avoid those escapes.
echo "<div style='background-color: " . $params->get('colorbgt') . ";' class='ioTitleBox'><h3 class='ioTitle'>";
<?php if (isset($ioTitle)){
echo "<div style='background-color:". $params->get('colorbgt').";' class=\"ioTitleBox\"><h3 class=\"ioTitle\">";
echo $params->get("ioTitle"); echo "</h3></div>";
}
Is this how you want it?
You are echo-ing from inside an echo, you just need to concatenate that part into the existing echo statement.

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

php quoting problem

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

Categories