user3323092
user3323092

Reputation:

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>'; 

Upvotes: 1

Views: 281

Answers (4)

Andreas L.
Andreas L.

Reputation: 2923

<a onclick="confirm('You Want To Delete?');" href="#">Delete</a>

Works...

Upvotes: 0

Rahul Saxena
Rahul Saxena

Reputation: 465

Try this one

<button onclick="myFunction()" href="#">Delete</button>

<script>
function myFunction() {
    confirm("Press a button!");
}
</script>

Upvotes: 0

Adnan
Adnan

Reputation: 2031

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

Upvotes: 2

Sougata Bose
Sougata Bose

Reputation: 31739

You are calling it in a wrong way.If you want to return what confirm() is returning the it should be -

onclick="return confirm('You Want To Delete?');"

You also need to escape the quotes or change them to 's for the message. Or you can try with if you want some other checks or other processings -

onclick="cnf_delete()"

And define the function -

function cnf_delete() {
    return confirm('You Want To Delete?');
}

Upvotes: 2

Related Questions