admin管理员组

文章数量:1397142

How to show alert result from javascript return confirm()?

The script is like this:

<td width="25" align="center" title="Delete">
<?php echo '<a href="'.base_url().'index.php/ticket_controller/confirm_hr_ticket/'.$row->id.'" onclick="return confirm(\'Are you sure to delete '.$row->trans_code.'?\')">'?>
<img style="vertical-align:middle;"src="<?php echo site_url() . 'images/btn_delete.gif'; ?>"></img></a></td>

I want to show alert('Data deleted') after i click yes in confirmation.

How to show alert result from javascript return confirm()?

The script is like this:

<td width="25" align="center" title="Delete">
<?php echo '<a href="'.base_url().'index.php/ticket_controller/confirm_hr_ticket/'.$row->id.'" onclick="return confirm(\'Are you sure to delete '.$row->trans_code.'?\')">'?>
<img style="vertical-align:middle;"src="<?php echo site_url() . 'images/btn_delete.gif'; ?>"></img></a></td>

I want to show alert('Data deleted') after i click yes in confirmation.

Share Improve this question edited Sep 4, 2013 at 10:21 Raptor 54.3k47 gold badges248 silver badges399 bronze badges asked Sep 4, 2013 at 10:12 okywijayaokywijaya 592 silver badges13 bronze badges 3
  • 1 onclick does not handle boolean value returned by confirm() as you want. – Raptor Commented Sep 4, 2013 at 10:17
  • 2 Is not related to php. – Ofir Baruch Commented Sep 4, 2013 at 10:18
  • sidenote: it is suggested to use CSS instead of HTML attribute for styling. – Raptor Commented Sep 4, 2013 at 10:20
Add a ment  | 

4 Answers 4

Reset to default 6

You need to make a function called by the onclick like

Demo

HTML/PHP

<?php echo '<a href="'.base_url().'index.php/ticket_controller/confirm_hr_ticket/'.$row->id.'" onclick="return myfunction(\''.$row->trans_code.'\')">'?>

Javascript

function myfunction(transcode){
  if(confirm('Are you sure to delete '+ transcode + '?'))
  {
    //Delete data
    alert('data deleted');
    return true;
  }else{
    return false;
  }
}

Add a simple JS function:

function DeleteClick(transcode)
{
    if(confirm('Are you sure to delete ' + transCode + '?'))
    {
        alert('Data deleted');
        return true;
    }
    return false;
}

Then change your call to

<?php echo '<a href="'.base_url().'index.php/ticket_controller/confirm_hr_ticket/'.$row->id.'" onclick="return DeleteClick(\''.$row->trans_code.'\')">'?>

Write a function in onclick as onclick="confirmclick(userid);"

Javascript function

function confirmclick(userid)
{
   var answer = confirm("Are you sure to delete " + userid + "?")

    if (answer){
        alert('Data deleted');
        return true;  
    }

    return false;  
} 

If you don't want to write separate function then you may use following.

<td width="25" align="center" title="Delete">
    <?php echo '<a href="'.base_url().'index.php/ticket_controller/confirm_hr_ticket/'.$row->id.'" onclick="if(confirm(\'Are you sure to delete '.$row->trans_code.'?\')){alert(\'Data deleted\');}">'?>
    <img style="vertical-align:middle;"src="<?php echo site_url() . 'images/btn_delete.gif'; ?>"></img></a></td>

本文标签: javascripthow to show return answer from return confirmStack Overflow