Jump to content

JS onclick help


coupe-r

Recommended Posts

Hi Everyone,

 

I have icons that delete records that use JS to open a Confirm pop up window.  My problem is that is JS is not enabled, when a user clicks the icon, it just deletes the record without confirmation.  How can I set this up better?

 

Icon via PHP

<?php
echo '<a href="javascript:void(0);" onclick="deleteEviction(\'eviction.php?id='.$_GET['id'].'&removeRecord=1&del='.$eviction_id.'\');">'.
'<img src="../images/tenants/trash.png" alt="Delete Eviction Record" title="Delete Eviction Record" border="0" width="25" height="25" /></a>';
?>

 

Javascript Function:

function deleteEviction (url) {
   var answer = confirm("You are about to delete the entire eviction record.  Do you want to continue?")
   if (answer){
      window.location = url;
   }
   else{
      return false;
   }
}

Link to comment
Share on other sites

My problem is that is JS is not enabled, when a user clicks the icon, it just deletes the record without confirmation.  How can I set this up better?

 

Don't use Javascript to create the confirm message.

 

Although I agree that a non-JS confirmation would be a better solution in this instance, from a purely academic perspective there is no reason you cannot implement both. There are plenty of articles about to implement JS that onnly enhances the user experience and not a requirement. In this instance you are using a link to initiate a delete. So, you could implement a JS function for the onclick event which will call for the confirmation and - if true - performs the delete from the js. (by the way I would suggest making a standalone function instead of piling a bunch of code into the onclick event). But, the onclick event should return false. By returning false you are telling the HTML page NOT to follow the href in the link. So, if JS is not enabled the user will go to the href of the link. That href should call an HTML confirmation page.

 

Psuedo code

function confirmDelete(recID)
{
  if(confirm('Are you sure you want to delete?'))
  {
    window.location = 'deleteRecord.php?id='+recID;
  }
  return false;
}

<a href="htmlConfirmationPage.php?id=12" onclick="return confirmDelete(12);">Delete Record 12</a>[/code[

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.