Combine Javascript confirm prompt with php code

As a general rule, you can’t combine a javascript confirm prompt with your php code because one is server side and the other is client side. Here’s a work-around that will get you a basic confirm prompt if, for example, you’ve done a database inquiry and you want the user to confirm an update before continuing. There’s one confirm-variable (the user) and one response variable (system post-processing)

Here’s the code flow ..


// enter your php code here and use one variable (ie number of records) that you want the user to confirm


<script type="text/javascript">
var answer = confirm("Do you want to update <?php echo addslashes("$your-confirm-variable ?"); ?>")
if (answer){ alert("<?php OKtrue(); ?>") } else {alert ("<?php CANCEL(); ?>") }


function OKtrue() {
global $your-confirm-variable;

// enter your php processing (ie database update) - do not echo or print any statements until final response

// ie $response = number of records updated etc
Echo addslashes("$response Records Updated");

function CANCEL() {
echo "The information WAS NOT Processed";


This method can be used anytime you want the user to confirm something that can be passed as a simple variable and then return another simple variable after the action has been completed.

Leave a Reply