My code is below, I am trying to delete records from mysql database but before deleting the browser has to prompt the user whether the deletion should continue. My problem is my logic is not working its deleting the record no matter what. Any help will be appreciated.

   if (isset($_POST['outofqcellchat'])){
?>
<script type ="text/javascript">
var question = confirm("Are you sure you want to unsubscribe\nThis will delete all your facebook information in QCell Facebook");
if(question){
<?php
$delusr = mysql_query("delete  from `chat_config` where `phone` = '$phonenumb'");
$row = mysql_num_rows($delusr);
if($row>=1){
header("Location:http://apps.facebook.com/qcellchat");
}
?>
alert("Unsubscribed, You can register again any time you wish\nThank You");
}else {
alert("Thanks for choosing not to unregister \nQCell Expand your world");
}
</script>
<?php
}
?>

Thats my code. Please help

link|flag

3 Answers

You have a fundamental misunderstanding between PHP and Javascript here. The PHP code will be executed regardless of any JavaScript conditions (which will be processed long after PHP is done, in the browser).

You will need to change the logic so that confirming the deletion redirects the user to a PHP page that deletes the record, or starts an Ajax request with the same effect.

link|flag
I am m using only one page not two – Sheriffo Ceesay Oct 11 at 10:56
@Sheriffo yes, that is exactly your problem. – Pekka Oct 11 at 10:57
So i cant do this in one page – Sheriffo Ceesay Oct 11 at 11:00
@Sheriffo you could in theory, by sending a GET request to the same page, but the solution @noobcode presents is a good one too. – Pekka Oct 11 at 11:02
in @noobcode i still need delete.php page plus the main page which i dont want to do. There is a delete query fragment that i want to execute based on what the user click. – Sheriffo Ceesay Oct 11 at 11:39
show 10 more comments

you want to prompt the user upon click of a anchor tag or button. For eg using anchor tag

<a href="delete.php" onclick="return javascript:confirm("Are you sure you want to delete?");" />Delete</a>

This will prompt user.

Or you might use a javascript function such as

    <a href="delete.php" onclick="return check();" />Delete</a>

    <script type="text/javascript">
    function check(){
    var question = confirm("Are you sure?");
    if(question){

    return true;

    }else{

    alert("Thanks for not choosing to delete");
    return false;

    }

    }
</script>

Hope this helps.

link|flag
+1 for good example code. Maybe you should add an example ?id=1234 to delete.php though for full clarity – Pekka Oct 11 at 10:44

The PHP runs on the server before the client even sees the JavaScript. Use AJAX or a form submission instead.

link|flag

Your Answer

 
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.