I have the below function defined that passes a dynamic id to the function as a parameter and output to an html link. The script it calls updates a database record and returns an alert of success or failure based on query being successful, or the id failing some validation.
function cancel_file(id){
jQuery.ajax({
type: "POST",
url: "https://www.mysite.com/cancel.php",
data: 'id='+ id,
cache: false,
success: function(response){
console.log(response);
if(response == 1){
alert('File successfully cancelled!');
}
}
error: function(response){
console.log(response);
if(response == "invalid_id"){
alert('The file ID format is invalid');
}else if(response == "no_record_found"){
alert('No file found with ID specified');
}
}
});
}
And I am calling it in the section of my document as follows
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script>
<script type="text/javascript" src="/js/jquery.js"></script>
The link on the page is output using the following php code
<?php echo "<a href='#' onclick='cancel_file(".$id.")'>Cancel </a>"; ?>
When the link is clicked nothing happens and when I examine the Chrome JS Debugger Console it shows “Uncaught ReferenceError: cancel_file is not defined”. I did some reading that functions defined within the document.ready() are ignored by Chrome and FF so I removed that declaration from the top. I am still new to jQuery so any help is greatly appreciated.
TIA
cancel.php
<?php
// Get the fileid and make sure it's in a valid format
$id = $_POST['id'];
if(!ctype_digit($id)){
$response = "invalid_id";
}else{
// If validation passes proceed to queries and cancellation process
$q1 = "UPDATE `table_files` SET `is_invalid` = 1 WHERE `id` = '".$id."'";
$r1 = mysql_query($q1) or die(mysql_error());
if(mysql_affected_rows() > 0){
$response = mysql_affected_rows();
}else{
$response = "no_record_found";
}
echo $response;
?>
$response is what should be passed back to the jQuery function.
I found a workaround by loading the PHP script into a jQuery UI modal and processed it there. Not exactly what I was hoping but it does the trick nicely.