-1

I have a button Delete File in script1.php.

After clicking on that button, I wan to display a JS confirm box to confirm the action.

I'm using this code :

<script language="javascript">
    if (confirm("Are you sure to delete this file ?")) {
        // Will continue executing the script
    } else {
        window.stop();
    }
</script>
<?php
// Delete the file ...
?>

This code does what I want : The php won't execute unless the user confirms the action.

Now, if I want to customize the confirm box (Add another button, modify the style ...), I thought of using jQuery .dialog

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="dialog-confirm" title="Do you wanna continue ?">
  <p>Are you sure to delete this file ?</p>
</div>

<script language="javascript">
$( document ).ready(function() {
  $("#dialog-confirm").dialog({
    resizable: false,
    height: "auto",
    width: 400,
    modal: true,
    buttons: {
      "Do it": function() {
        $(this).dialog("close");
      },
      Cancel: function() {
        $(this).dialog("close");
        window.stop();
      }
    }
  });
});
</script>
<?php
// Delete the file ...
?>

Unfortunately, this does not work, and the rest of the php code will be executed any way.

So, is there a way to "halt" the execution til the click on the "Do it" button?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Hamza Abdaoui
  • 2,029
  • 4
  • 23
  • 36
  • 9
    That PHP code will run regardless of whatever happens in JavaScript. The whole page is rendered *completely* before the HTML is sent to the client. The client receives the JavaScript and can decide if it wants to run it or not. – tadman Oct 31 '18 at 16:21
  • 3
    In order to have a JavaScript action initiate something in PHP you must either redirect or make an AJAX call. There is no other way. – tadman Oct 31 '18 at 16:21
  • 3
    It feels like you have a misunderstanding about what runs server side and what runs client side. – sjahan Oct 31 '18 at 16:22
  • Well, I'm feeling stupid after reading what you have said! AJAX it's then. – Hamza Abdaoui Oct 31 '18 at 16:22
  • 1
    In addition to @sjahan's point I'd suggest reading [this](https://stackoverflow.com/q/13840429/519413) as a grounder on the differences between client and server code. – Rory McCrossan Oct 31 '18 at 16:35
  • Thank you @RoryMcCrossan, I have a good understanding about that. Just maybe this is what happens after 8 hours of work ! I'll figure this out, and i'll post an answer here for future viewers. – Hamza Abdaoui Oct 31 '18 at 16:40

1 Answers1

0

Separate front- and backend, send messages between two.

Javascript asks for confirmation and if gets it - sends a request to php script.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div id="dialog-confirm" title="Do you wanna continue ?">
  <p>Are you sure to delete this file ?</p>
</div>

<script language="javascript">
$( document ).ready(function() {
  $("#dialog-confirm").dialog({
    resizable: false,
    height: "auto",
    width: 400,
    modal: true,
    buttons: {
      "Do it": function() {
        $(this).dialog("close");
        $.post( "post.php", {filename: "error"})
            .done(function( data ) {
                alert('Success: '+JSON.stringify(data));
            })
            .fail(function(jqXHR, textStatus, errorThrown) {
                alert('ERROR: '+JSON.stringify(errorThrown));
            });
  },
      },
      Cancel: function() {
        $(this).dialog("close");
        window.stop();
      }
    }
  });
});

php script waits for a filename, deals with file manipulation (e.g. deleting) and sends some feedback.

<?php
$fn = $_POST['filename'];
//delete file with name $fn
//send result
if($result=='error') //something went wrong
{
    header('HTTP/1.1 500 Ended up with bad result');
    header('Content-Type: application/json');
    $response_array['status'] = "error"; 
    $response_array['data'] = $data;   
}else{ //everything ok
    header('Content-type: application/json');
    $response_array['status'] = 'success'; 
    $response_array['data'] = $data;
}   
echo json_encode($response_array);
?>
Sven Liivak
  • 1,323
  • 9
  • 10