tags:

views:

39

answers:

3

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

+1  A: 

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

Ignacio Vazquez-Abrams
+3  A: 

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.

Pekka
I am m using only one page not two
Sheriffo Ceesay
@Sheriffo yes, that is exactly your problem.
Pekka
So i cant do this in one page
Sheriffo Ceesay
@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
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
@Sheriffo in that case, do the if condition in PHP: `<? if (isset($_GET["delete"])) .... ` this will also need a page reload, though
Pekka
This was exactly what i was doing take a look at the code I attached, but this will not work with Java script.
Sheriffo Ceesay
@Sheriffo no, it won't. And it never will. You will need to reload the page after the confirm, or do an Ajax request. There is no other way.
Pekka
What i was saying is this will not prompt the user, and thats my main concern. The user has to get a prompt which he/she can click on yes or no to proceed.
Sheriffo Ceesay
@Sheriffo yes. Do the `confirm` in Javascript. If it is true, do a `location.href='mypage.php?delete=1234', 1234 being the ID of your record. Then do the PHP if statement I wrote above.
Pekka
Thanks i will try that and get back to you
Sheriffo Ceesay
ok i understand, but still now i have pass this to another php page for processing
Sheriffo Ceesay
@Sheriffo no you don't, you can have the if condition in your original page, but yes you will need to reload that
Pekka
Please dont be pissed off with me, my original page is where i will put the javascript lets say the name of that page is original.php now if a user click on OK it should redirect to mypage.php where the delete query should be. So what i am saying is i want to do everything in original.php...
Sheriffo Ceesay
@Sheriffo then do the processing in original.php. It is possible, you just need to redirect there with the `delete` parameter after the JavaScript confirm dialog was displayed.
Pekka
+3  A: 

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.

noobcode
+1 for good example code. Maybe you should add an example `?id=1234` to `delete.php` though for full clarity
Pekka