Use the JavaScript confirmation dialog to make a PHP call in the background

Ok, not quite sure if this is possible, but I thought I was asking anyway :)

I have an HTML page containing some information and a link. The link dialog box invokes the JavaScript confirmation dialog box. If the user selects OK, then I want to call the function in the PHP file, but in the background, that is, I do not want to change the page, reload or something else.

Is it possible?

thank

T

+5
source share
2 answers

Use AJAX. The following example uses jQuery:

if(confirm('Are you sure?')) {
    $.ajax({
        url: '/path/to/file.php',
        data: 'url=encoded&query=string', // Can also be an object
        success: function(output) {
            // This function is called after the PHP file completes.
            // The variable passed in is a string containing all output of
            // your PHP  (i.e. echo / print )
        }
    });
}

jQuery AJAX. jQuery, JavaScript AJAX.

PHP, PHP JSON , JavaScript.

+6
$('.LinkClass').click(function(){

  if( confirm('Is it OK?') ) {

    $.ajax({
        url: 'action.php',
        type: 'POST',
        data: 'data=value', // Can also be an object
        success: function(data) {
            // Do Nothing
        },
        error: function(){
           alert('Error');
        }
    });

  }

});
+3

All Articles