Prashant Prashant - 4 years ago 116
Javascript Question

Cannot call function on click

I am a beginner in PHP, I want to show a confirm box on a link click but I cannot get any result in that. It gives me error onclick but I cannot read this error because it suddenly refreshes the page. Is there any mistake in single quote?

Is it possible for me to use alert in link tag?

echo '<tr><td align="center"><a onclick="confirm return("You Want To Delete?");" href="persontype.php?person_type_id='.$postRow['person_type_id'].'">Delete</a></td>';

Answer Source

You need to escape the quote, use javascript: and it's return confirm(), not confirm return()

echo '<tr><td align="center"><a onclick="javascript: return confirm(\'You Want To Delete?\');" href="persontype.php?person_type_id='.$postRow['person_type_id'].'">Delete</a></td>'; 

I copied your exact php code and modified. The above php code should output the exact functionality you are expecting.

enter image description here

To show SweetAlert:

<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="http://t4t5.github.io/sweetalert/dist/sweetalert.min.js"></script>
<link rel="stylesheet" href="http://t4t5.github.io/sweetalert/dist/sweetalert.css">

<?php
echo '<tr><td align="center"><a onclick="javascript: swal({title:\'You Want To Delete?\',   text: \'You really want to delete this user?\',   type: \'warning\',   showCancelButton: true,   confirmButtonColor: \'#DD6B55\',   confirmButtonText: \'Yes, delete it!\',   cancelButtonText: \'No, cancel plx!\',   closeOnConfirm: false,   closeOnCancel: true }, function(isConfirm){   if (isConfirm) {   window.location.href = \'persontype.php?person_type_id='.$postRow['person_type_id'].'\';   } else {     return false;   } }); return false;" href="persontype.php?person_type_id='.$postRow['person_type_id'].'">Delete</a></td>';

Sorry, the code looks ugly in one line, but it works and it's the only way to show sweet alert inside link.

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download