Skip to content Skip to sidebar Skip to footer

How To Execute The Code Inside Javascript Confirm Box

I have a JavaScript confirm box and i have some MySQL insert query code to be executed at some condition. This is how my code look like:

Solution 1:

You can't execute MySQL or PHP command inside javascript, what you can do instead is to make a PHP function that you can call by Ajax. The best way is by using jQuery or by redirecting the page with your PHP function in URL.

<scripttype="text/javascript">if (confirm("This seems to be Duplicate. Do you want to continue ?"))
    {
        $.ajax({
            url: 'your_path_to_php_function.php',
            type: 'POST', // Or any HTTP method you likedata: {data: 'any_external_data'}
        })
        .done(function( data ) {
            // do_something()
        });
    }
    else
    {
        window.location = 'your_url?yourspecialtag=true'
    }
 </script>

Solution 2:

You're mixing serverside and clientside scrips. This won't work. You have to use AJAX, which are asynchronous server-client/client-server requests. I recommend jQuery, which is JavaScript which easily handles lot of things, including AJAX.

Run this if user confirms action

$.post("phpscript.php", {action: true})

Php file:

if ($_POST['action'] === TRUE) {
    <your code here>
}

Post a Comment for "How To Execute The Code Inside Javascript Confirm Box"