1

I'm trying to call a javascript function within php that will pop up a confirmation button. If the user presses yes, then it will proceed onto the page, otherwise it'll stay on the same page. I wrote it, but I have no idea what's wrong.

php:

echo "<a href='edit_members.php?id=$studentid'>Edit</a> or <a href=\"javascript:deleteMembers('del_member.php', '$studentid');\">Delete</a><br/><br/>";

javascript (i placed it right before the tag):

<script type="text/javascript">
    function deleteMembers(url, id) {
       var deleteMemberConfirmation = confirm("Are you sure you want to delete?");
       if(deleteMemberConfirmation) {
         window.location="http://mvcsf.com/admin/"+url+"?"+id;
        }
       else {
         window.location="http://mvcsf.com/admin/view_members.php";
       }     
     }
</script>

I enabled ERROR_REPORTING(E_ALL); at the top of the page, but it's not returning anything. What did I do wrong?

Edit: I changed the variable names to deleteMemberConfirmation, but still nothing works. I just click the link, but nothing happens.

1
  • What happens when you load this page? Do you get a blank white screen? Does it look right but the button doesn't work? etc. Commented Mar 11, 2013 at 23:01

3 Answers 3

4

delete is a reserved keyword in javascript, and not a valid variable name!

And you got the quotes wrong:

"<a href=\"javascript:deleteMembers('del_member.php', '$studentid');\">";
Sign up to request clarification or add additional context in comments.

Comments

2

You're using ' as a designator in your HTML AND in your JS. You will have to use it in one place and " in others.

A working version would be something like:

echo "<a href=\"edit_members.php?id=$studentid\">Edit</a> or <a href=\"javascript:deleteMembers('del_member.php', '$studentid');\">Delete</a><br/><br/>";

Comments

0

For your echo, be careful when using single quote ' and double quote ". A single quote will be closed when it meets another single quote unless it is escaped like this \'. The same goes for double quote.

I'm not 100% sure if you can use javascript inside href, but another solution is to use onclick when calling javascript function, and just use javascript:void(0) or # for href attribute.

echo "<a href='edit_members.php?id=$studentid'>Edit</a> or <a href='javascript:void(0)' onclick=\"deleteMembers('del_member.php', '$studentid');\">Delete</a><br/><br/>";

As for the delete, change the delete word to something else (I.e: del), because delete is a reserved word for javascript.

<script type="text/javascript">
function deleteMembers(url, id) {
    var del = confirm("Are you sure you want to delete?");
    if(del) {
        window.location="http://mvcsf.com/admin/"+url+"?"+id;
    }
    else {
        window.location="http://mvcsf.com/admin/view_members.php";
    }
}
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.