Reputation:
echo "<td><a href='delete.php?id=$row[id]&&category=$a' onclick='return confirm(\'are you sure you wish to delete this record\');'>delete</a></td>";
Above is the code I am trying to use. Every time it does nothing and I cannot see how I can use 'proper' JavaScript methods. What is the reason?
Upvotes: 2
Views: 5764
Reputation: 546055
You should try to separate your JavaScript from your HTML as much as possible. Output the vanilla HTML initially and then add the event to the link afterwards.
printf('<td><a id="deleteLink" href="delete.php?id=%d&category=%s">Delete</a></td>', $row["id"], $a);
And then some JavaScript code somewhere on the page:
document.getElementById('deleteLink').onclick = function() {
return confirm("Are you sure you wish to delete?");
};
From your example however, it looks like you've probably got a table with multiple rows, each with its own delete link. That makes using this style even more attractive, since you won't be outputting the confirm(...)
text over and over.
If this is the case, you obviously can't use an id
on the link, so it's probably better to use a class. <a class="deleteLink" ...
If you were using a JavaScript library, such as jQuery, this is very easy:
$('.deleteLink').click(function() {
return confirm("...");
});
Upvotes: 2
Reputation: 1021
Just a suggestion, are you using a framework?
I use MooTools then simply include this script in the HTML
confirm_delete.js
window.addEvent('domready', function(){
$$('a.confirm_delete').each(function(item, index){
item.addEvent('click', function(){
var confirm_result = confirm('Sure you want to delete');
if(confirm_result){
this.setProperty('href', this.getProperty('href') + '&confirm');
return true;
}else{
return false;
}
});
});
});
All it does is find any "a" tags with class="confirm_delete" and attaches the same code as your script but i find it easier to use. It also adds a confirmation variable to the address so that if JavaScript is turned off you can still send them to a screen with a confirmation prompt.
Upvotes: 2
Reputation: 12262
Here is what I use for the same type of thing.
I do not echo/print it, I will put the html between ?> html
?> <td><a href="?mode=upd&id=<?= $row[id] ?>&table=<?= $table ?>">Upd</a> / <a href="?mode=del&id=<?= $row[id] ?>&table=<?= $table ?>" onclick="return confirm('Are you sure you want to delete?')">Del</a></td> <?php
Upvotes: -1
Reputation: 144987
Another solution:
echo '<td><a href="delete.php?id=' . $row[id] . '&category=' . $a . '" onclick="return confirm(\'are you sure you wish to delete this record?\');'>delete</a></td>';
I changed the double quotes to single quotes and vise versa. I then concatinated the variables so there is no evaluation needed by PHP.
Also, I'm not sure if the return on the onclick will actually stop the link from being "clicked" when the user clicks no.
Upvotes: 0
Reputation: 18115
I think $row[id] is not evaluating correctly in your echo statement. Try this:
echo "<td><a href='delete.php?id={$row[id]}&&category=$a'...
Note the squiggly brackets.
But THIS is much easier to read:
?><td><a href="delete.php?id=<?=$row[id];?>&category=<?=$a;?>" onclick="return confirm('are you sure you wish to delete this record');">delete</a></td><?
As an aside, add a function to your js for handling the confirmation:
function confirm_delete() {
return confirm('Are you sure you want to delete this record?');
}
Then your onclick method can just be return confirm_delete()
.
Upvotes: 3
Reputation: 2636
And if you insist on using the echo-thing:
echo "<td><a href='delete.php?id=$row[id]&&category=$a' onclick='return confirm(\\'are you sure you wish to delete this record\\');'>delete</a></td>";
-- because the escaping is treated from the php-interpretator !-)
Upvotes: -1
Reputation: 78852
It is also a bad idea to use GET methods to change state - take a look at the guidelines on when to use GET and when to use POST ( http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist )
Upvotes: 3
Reputation: 180024
echo "<td><a href='delete.php?id=$row[id]&&category=$a' onclick='return confirm("are you sure you wish to delete this record");'>delete</a></td>";
Use Firefox's HTML syntax highlighting to your advantage. :-)
Upvotes: 1