2
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?

0

8 Answers 8

3

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 )

Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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().

6 Comments

Note that on most servers, php short tags are disabled. Therefore, <?= would not work.
My experience is that on most servers short_tags are enabled. But to each his own.
I've also never had a problem with short_tags. According to my reading of php.net/ini.core short tags are on by default.
god help you then when your application is put on a server which doesn't have short tags on.
@nickf I think you might be right. I'm going to ask a question about this though (assuming there isn't already one.)
|
2

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.

1 Comment

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.
2

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 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("...");
});

Comments

1
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. :-)

2 Comments

Bit of a weird way of doing it but it will work - it would make more sense to use \" or &apos; Also the && should be &amp; and $a may need url / html encoding
Yes, afaik this won't work because the "$row[id]" won't evaluate correctly in the string.
0

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.

1 Comment

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

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

1 Comment

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\');'
-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.