Hello, I have an issue with my ajax script. Basically I want to be able to stop a php script from executing when I click on the Cancel button of the Confirm Dialog box. Here are the code snippets for my application:
policy.html:
....
$(document).ready(function(){
$('#process').click(print_check);
});
function print_check(){
var policy = $('#policy').val();
if(policy == ""){
alert("Enter Policy Number");
}else{
//Ajax function
jQuery.ajax({
type: "POST",
url: "loyalty.php",
data: 'policy='+ policy,
cache: false,
success: function(response){
if(response == 1){
var r=confirm('Document has already been printed, reprint?')
if (r==true)
{
$("form").submit();
return true;
} else {
// Insert something here???
return false;
}
}// end Ajax function
....
<form method = "post" action = "printloyalty.php">
<input type="text" name="policy" id="policy" size="30" />
<input type="button" name="process" id="process" value="Print Card" class="bigbutton" />
....
And loyalty.php:
....
$policy = mysql_real_escape_string($_POST['policy']);
mysql_query( "SET AUTOCOMMIT = 0" );
mysql_query( "START TRANSACTION" );
//Select Query
$sql = "SELECT policyNumber FROM policydetails WHERE policyNumber = '".$policy."' AND policyNumber LIKE 'CB%' AND LCardPrinted = 'Y' ";
$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$num = mysql_num_rows($result);
//Insert Query
mysql_query("INSERT INTO tblloyaltycardaudit (policyNumber, userID)
VALUES( '$policy', {$_SESSION['loggedUserID']} )");
$num1 = mysql_affected_rows();
if($num == 1 && $num1 == 1){
mysql_query("COMMIT");
echo $num;
}
else{
mysql_query("ROLLBACK");
}
.....
In the above code I am inserting a record into one table and selecting from another at the same time using Transactions. The value of echo $num will be returned to the policy.html ajax function for the response. So when I click on the Print Card button, a confirm box appears with two buttons, OK and Cancel.
The problem is that when I click the Cancel button, it still inserts records into the table but I want that to only happen when I click the Ok button. Any help or alternatives would be greatly appreciated.