String concatentation in html and echo statement - php

So I have this line of code in a php file:
echo "<a class='button' onclick=report('$row[id]')></a>";
I'd like to add an id to the line, so I tried this:
echo "<a class='button' id='"report".$row[id]' onclick=changeColor('$row[id]')></a>";
and in my javascript file
document.getElementById('report'+id).style.color('blue');
but I keep getting errors in javascript
cannot read property 'style' of null,
because I'm doing something wrong with the concatenation. I'm trying to get the id to be something like "report100" by concatenating it with $row[id] and then in the javascript file, change it's font color. I've tried other variations, but I just seem to get it right.

try like this.
echo "<a class='button' id='report".$row['id']."' onclick=changeColor(".$row['id'].")></a>";
UPDATE :
To concatenate a string and variable together, use the string concatenation operator . (a dot).
As well it is depend on how you start the string
Starting the string with '' (single quotes) will be
echo '<a class="button" id="report'.$row['id'].'">';
Starting the string with "" (double quotes) will be
echo "<a class='button' id='report'".$row['id'].">";

The to prevent the " from terminating the string, you can use \".
echo "<a class=\"button\" id=\"report$row[id]\" onclick=\"changeColor('$row[id]')\"></a>";
Should result in
<a class="button" id="report100" onclick="changeColor('100')"></a>

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 . "'>"

Removing unwanted characters from name tag with php

I have the following code snippet:
echo "<label><input type='checkbox' class='selector' name='{$data['ColA']}'>" .preg_replace('/[^0-9]+/','', $data['ColA'])."</label>";
Here I'd like to use preg_replace in "name" tag as well. How can I make it work? I tried the same code in name tag but it doesn't work. Thanks.
The output of this code is like this:
<label>
<input type='checkbox' class='selector' name='7b'>7</label>
I need to remove "b" from 7 in name tag too.
Using functions inside of this syntax with {} is not really a good idea and may lead to some issues you can avoid in the most simple way, just terminate the string, concatenate it with your function output and the rest of the string, like so:
echo "<label><input type='checkbox' class='selector' name='" . preg_replace('/[^0-9]+/','', $data['ColA']) . "'>" . preg_replace('/[^0-9]+/','', $data['ColA'])."</label>";
So in general:
echo "Something: " . a_function($variable) . ", the rest of the string.";
Edit: and one thing I'd forget about, depending on what your data is, you may want to use htmlspecialchars function on in before inserting it anywhere into your HTML DOM, if it's user-provided data, in order to avoid XSS attack.

php echo a hyperlink with javascript confirm function

I have been using this code for deleting data from database. What i wan is whenever a user clicks an image link to delete data from the confirm function prompts up and ask for action, i am getting error in this code.
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
echo "<a href=".$delurl.";
echo "onclick='return confirm('Are you sure you want to delete.')'>".$img."</a>";
Maybe the error is in double quotes or single quotes, Any help
Thanks in advance
change
echo "<a href=".$delurl.";
to
echo "<a href=\"".$delurl."\" ";
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
$confirm_box <<<CONFIRM
<a href="$delurl"
onclick="return confirm('Are you sure you want to delete?')">$img</a>
CONFIRM;
// then elsewhere ...
echo $confirm_box
Always tend towards using the HEREDOC syntax to construct HTML/JS output, it will save you a lot of heartache. Just watch out for the major gotcha, DO NOT INDENT THE FIRST/LAST lines of the heredoc declaration.
EDIT The benefit being that you can mix single and double quotes as much as you like, you only have to worry about the JS quoting - PHP variables are interpolated without the quotes. You can further wrap curly quotes around your PHP variables like {$this} to make it easier to read, but also to delineate $this and {$this}tle.
I would us the following instead of escaping, this is more readable to me:
$delurl = "delete_dish.php?dish_id=".$row['id'];
$img = "<img src = 'images/delete.png'>";
?>
<?=$img?>
You can, may and should escape when handling stuff like this:
echo "<a href=\".$delurl.\"";
echo " onclick=\"return confirm('Are you sure you want to delete.')\">".$img."</a>";
lg,
flo

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