tags:

views:

1503

answers:

8
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. Any help would, as usual, be much appreciated.

Cheers guys!

+1  A: 
echo "<td><a href='delete.php?id=$row[id]&&category=$a' onclick='return confirm(&quot;are you sure you wish to delete this record&quot;);'>delete</a></td>";

Use Firefox's HTML syntax highlighting to your advantage. :-)

ceejayoz
Greg
Yes, afaik this won't work because the "$row[id]" won't evaluate correctly in the string.
MDCore
+3  A: 

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 )

Ken
Indeed. <input type=submit name="delete_row_id_123"> can be used to avoid multiple forms with hidden fields, or <button name=delete value=123>delete</button> if you don't care about IE.
porneL
+3  A: 

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];?>&amp;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().

MDCore
Nice answer - calling a function like that is how we do it alright, makes life much easier in the long run!
ConroyP
Note that on most servers, php short tags are disabled. Therefore, <?= would not work.
Darryl Hein
My experience is that on most servers short_tags are enabled. But to each his own.
Paolo Bergantino
I've also never had a problem with short_tags. According to my reading of http://www.php.net/ini.core short tags are on by default.
MDCore
god help you then when your application is put on a server which doesn't have short tags on.
nickf
@nickf I think you might be right. I'm going to ask a question about this though (assuming there isn't already one.)
MDCore
Yeah, it's going to be hell turning them on ^_^
Paolo Bergantino
A: 

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 !-)

roenving
escaping quotes with a backslash doesn't work in HTML.
nickf
No but php strips out the first backslash, so what is written in the code that reaches the browser is: onclick='return confirm(\'are you sure you wish to delete this record\');'
roenving
A: 

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.

Darryl Hein
it will. if a link's onclick function returns false, then the action is cancelled. confirm() returns true or false depending on whether the user clicks "ok" or "cancel".
nickf
A: 
Brad
+1  A: 

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.

cole
ah, nice touch with the extra variable added to the end. I tend to be of the opinion, however, that users who have JS turned off get what they deserve.
nickf
A: 

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&amp;category=%s">Delete</a></td>', $row["id"], $a);

and then some javascript 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("...");
});
nickf