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
Related
The onclick='return confirm('Delete this comment?')' seems to be not working at all:
<?php
if($sesType == 'Admin') {
echo "<a href = 'editCom.php?id=$commentID'>Edit</a> ";
echo "<a href='deleteCom.php?id=$commentID' onclick='return confirm('Delete this comment?')'>Delete</a>";
} else if($_SESSION["username"] != NULL AND $_SESSION["username"] == $postname) {
echo "<a href = 'editCom.php?id=$commentID'>Edit</a> ";
echo "<a href='deleteCom.php?id=$commentID' onclick='return confirm('Delete this comment?')'>Delete</a>";
}
?>
But the confirmation box does not appear.
You're using single quotes inside of single quotes. Use double quotes to wrap your code instead:
Link
Just change this code, and it will work.
echo "Delete";
You have misused the single quote and double quotes. You have to put slash () on quotes to escape. The html should look like this:
Link
Take note that single quote should be inserted inside the double quotes ""
from your code:
echo " <a href='deleteCom.php?id=$commentID' onclick='return confirm('Delete this comment?')'>Delete</a>";
It should be:
echo "Delete";
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>
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>";
can anybody help me here please?
I am using AJAX for pagination in my application. So I am generating hyperlinks with for loop. as follow:
for($t=1; $t<=$hf; $t++)
{
if($t == $_GET['pageno'])
{
echo $t." ";
}
else
{
echo "<a id ='$t' href='javascript:void(0)' onclick='open_page('ajaxinfo.php','content'); javascript:change('$t');'>$t</a>"." ";
}
}
Above echo statement does not give call to function. But instead of this when i just write html hyperlink it works fine and I get to see page2.html, my HTML code is:
<a id="page2" href="javascript:void(0)" onclick="open_page('ajaxinfo.php','content'); javascript:change('page2');">page2</a>
I don't understand why this is so? But is there any problem in echo's quotes.
Please Help.
that because you have syntax error while building anchors. Try to use double quotes for tag attributes and escape them with backslash.
So, your ECHO should look like this:
echo "<a id =\"{$t}\" href=\"javascript:void(0)\" onclick=\"open_page('ajaxinfo.php','content'); javascript:change('{$t}');\">{$t}</a> ";
You have to have code to add the contents returned by the ajax to the page. I don't see that anywhere.
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>
';