I've got the following script that updates a set of columns in my database.
acknowledge.php
<?php
if(isset($_POST['action'])){
$field = $_POST['db_field'];
$value = $_POST['db_value'];
$ticket = $_POST['ticket'];
$ack_date = date("Y_m_d");
$link = mysql_connect("localhost", "dbadmin", "dbpw");
mysql_select_db("dbname", $link);
mysql_query("UPDATE table SET $field = '$value', ack_date = '$ack_date' WHERE id = '$ticket', $link);
mysql_close($link);
}
?>
What I'm trying to do is in my HTML table I have a link for each column that says whether or not an item has been acknowledged. If it has, show the date and user who did otherwise display a text link to allow for someone to acknowledge it.
<?php
// Display acknowledgement data; if not acknowledged, display link to accept it
if(empty($details['ack_by']) AND empty($details['ack_date'])){
echo "<center><a href='#' class='ack-link' db_field='ack_by' db_value='".$username."' ticket='".$id."' />Take Ownership</a></center>";
}else{
echo $details['ack_by']." on ".$details['ack_date'];
}
?>
What I want to happen is, if it's not acknowledged, clicking the link will do this without navigating away from the page. I figured ajax and jQuery would be the best for this so I wrote this:
<script type="text/javascript">
function performAjaxSubmission() {
$.ajax({
url: 'acknowledge.php',
method: 'POST',
data: {
action: 'save',
field: $(this).attr("db_field"),
val: $(this).attr("db_value"),
ticket: $(this).attr("ticket_id")
},
success: function() {
alert("success!");
}
});
return false; // <--- important, prevents the link's href (hash in this example) from executing.
}
jQuery(document).ready(function() {
$(".ack-link").click(performAjaxSubmission);
});
</script>
What happens is clicking the link will generate the "Success!" prompt but the db isn't actually updated.