Reputation: 3589
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.
Upvotes: 1
Views: 5993
Reputation: 590
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>
Upvotes: 0
Reputation: 1856
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/>";
Upvotes: 2
Reputation: 318242
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');\">";
Upvotes: 4