Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

OK, not really sure if this is possible but thought I'd ask anyway :)

I have a HTML page which contains some information and a link. The link calls a JavaScript confirm dialog box. If the user selects OK then I want to call a function in a PHP file, but in the background, i.e I don't want to change page, reload or anything.

Is this possible?

Thanks

T

like image 319
user913059 Avatar asked May 18 '12 16:05

user913059


People also ask

How to use JavaScript confirm box in PHP?

Confirm dialog box displays a predefined message with two buttons: OK and Cancel buttons. The user will have to click either of the button to proceed. If the user clicks an OK button, the box returns true to the program. If the user clicks the Cancel button, the box returns false to the program.

How to show confirmation message box in JavaScript?

Confirm Box In this scenario, use the built-in function confirm() . The confirm() function displays a popup message to the user with two buttons, OK and Cancel . The confirm() function returns true if a user has clicked on the OK button or returns false if clicked on the Cancel button.

What is confirm in js?

Javascript | Window confirm() Method The confirm() method is used to display a modal dialog with an optional message and two buttons, OK and Cancel. It returns true if the user clicks “OK”, and false otherwise. It prevents the user from accessing other parts of the page until the box is closed. Syntax: confirm(message)

How do you make a confirmation box in HTML?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .


2 Answers

Use AJAX. The following 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 script (i.e. echo / print )
        }
    });
}

For more information, check out jQuery's documentation for AJAX. If you cannot use jQuery, there are many tutorials on using native JavaScript to make AJAX Requests.

If you are returning a large amount of data back to your success function from PHP, it would be worthwhile for you to return the data from PHP as a JSON encoded array, which can be safely parsed client-side into a JavaScript object.

like image 103
Jeff Lambert Avatar answered Oct 19 '22 23:10

Jeff Lambert


$('.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');
        }
    });

  }

});
like image 29
Naveed Avatar answered Oct 19 '22 22:10

Naveed