1

I want to call function deleteUser() after JavaScript confirmation. Here is my code. Please help me.

<?php
session_start();
include_once("DataSourceController.php");
$DeletedBy = $_SESSION['ID'];
$Name = $_GET['uname'];
echo '
<script type="text/javascript">
var responce=confirm("Are you sure you want to delete this user?");
if (!(responce==true)){
deleteUser();
}
</script>';
function deleteUser(){
$sql_DeleteUser="UPDATE login
            SET Deleted=1,DeletedAt= now(),DeletedBy=".$DeletedBy."
            Where User='".$Name."';";
mysql_query($sql_DeleteUser);
echo'
        <script type="text/javascript">                     
        alert("User '.$Name.' Successfully deleted."); 
        window.location.href = "../pages/DeleteUser.php";
        </script>'; 
}
?>
Chamith
  • 129
  • 1
  • 13
  • 11
    You need to learn the difference between server-side and client-side. It's a fundamental part of web development. – John Conde Apr 16 '14 at 14:12
  • 2
    PHP is processed server side before JS (client side) the only way to achieve this is to use AJAX – Pwner Apr 16 '14 at 14:14

3 Answers3

3

You have a few mistaken theories in your initial question. Although JavaScript is a client-side language, PHP is not. You will get an undefined function error with your current code, since it is not defined in javascript.

In order for JavaScript to execute a PHP function, it would be highly recommended to learn and use AJAX. AJAX can be used to dynamically execute PHP code when a user does a certain action. Many websites use this to query the database without reloading a page.

JavaScript will send a request to a PHP page, where the function will be executed. Refer to this page for a more in-depth example: Call PHP function from javascript

Community
  • 1
  • 1
Brandon White
  • 997
  • 9
  • 22
0

Use this code:

<?php
session_start();
include_once("DataSourceController.php");
$DeletedBy = $_SESSION['ID'];
$Name = $_GET['uname'];

function deleteUser(){
$sql_DeleteUser="UPDATE login
            SET Deleted=1,DeletedAt= now(),DeletedBy=".$DeletedBy."
            Where User='".$Name."';";
mysql_query($sql_DeleteUser);
echo'
        <script type="text/javascript">                     
        alert("User '.$Name.' Successfully deleted."); 
        window.location.href = "../pages/DeleteUser.php";
        </script>'; 
}

//first, we check whether the user has confirmed or not
if(!isset($_GET['confirmed'])) { //if they haven't, we display the confirmation message
    ?>
    <script type="text/javascript">
    var responce=confirm("Are you sure you want to delete this user?");
    if (!(responce==true)){
        //if confirmed, reload the page with added 'confirmed' parameter
        window.location.href="<?php echo $_SERVER['PHP_SELF'].$_SERVER['REQUEST_URI'] ?>?confirmed=1"
    }
    </script>
    <?php
}
elseif($_GET['confirmed'] == 1) {
   deleteUser();
}
?>
Al.G.
  • 4,327
  • 6
  • 31
  • 56
0

You can do it by to ways :

1/ Synchronously by redirecting to a php script by sending informations thanks to GET (for example):

<script type="text/javascript">
     var responce=confirm("Are you sure you want to delete this user?");
     if (!(responce==true)){
         window.location.href = ("myScript.php?user="+userName); //var userName should be defined before
     }
</script>';

and myScript.php:

<?php 
    if isset($_GET['user']){
       $name = $_GET['user'];
    }
    //some stuff    
    deleteUser($name); //Here you call your function     
    header('Location: myPage.php'); //You return to your first script    
?>

2/ Asynchronously by calling an AJAX request to myScript.php

Gwenc37
  • 2,064
  • 7
  • 18
  • 22