So I'm having a bit of difficulty getting user input to post to my database.
Whenever I try to update the table, I am able to do it successfully on the front end, but on refresh, the data change does not stick. Checking the database further verifies that the data has not been changed... It seems like the "success" is passing in the AJAX, but for some reason, it's not updating the database.
In fact, for my HTTP request, Chrome tells me that my POST request is being processed as a GET request for some reason and I'm not sure why...
JS:
jQuery(document).ready(function($){
$(".edit_tr").click(function () {
var commitID = $(this).attr('id');
// on click, hide the text
$("#name_"+commitID).hide();
$("#created_"+commitID).hide();
$("#status_"+commitID).hide();
$("#disbanded_"+commitID).hide();
// on click, show the input fields for editing
$("#name_input_"+commitID).show();
$("#created_input_"+commitID).show();
$("#status_input_"+commitID).show();
$("#disbanded_input_"+commitID).show();
}).change(function() {
var commitID = $(this).attr('id');
var name = $("#name_input_"+commitID).val();
var date_created = $("#created_input_"+commitID).val();
var status = $("#status_input_"+commitID).val();
var disbanded = $("#disbanded_input_"+commitID).val();
var dataString = {
id: commitID,
name: name,
date_created: date_created,
status: status,
disbanded: disbanded
};
console.log(dataString);
// can place loading image here
jQuery.post({
url: "/wp-admin/admin-ajax.php",
data: dataString,
action: 'editCommittee',
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
},
success: function (response) {
$("#name_"+commitID).html(name);
$("#created_"+commitID).html(date_created);
$("#status_"+commitID).html(status);
$("#disbanded_"+commitID).html(disbanded);
console.log("Got this from server: " + response);
console.log("This is what happened to data: ");
console.dir(dataString);
}
});
});
functions.php:
add_action('wp_ajax_editCommittee', 'editCommittee');
add_action('wp_ajax_nopriv_editCommittee', 'editCommittee');
function editCommittee() {
global $wpdb;
if($_POST['id']) {
$id = esc_sql($_POST['id']);
$name = esc_sql($_POST['name']);
$created = esc_sql($_POST['date_created']);
$status = esc_sql($_POST['status']);
$disbanded = esc_sql($_POST['disbanded']);
$wpdb->update('wp_committees',
array(
'name' => $name
),
array(
'committee_id' => $id
),
array(
'%s'
)
);
exit;
}
}
My data on the front end is being printed out into a table, so I don't have a form... Any insight on this would be helpful!
Edit:
var_dump($_REQUEST);
on the page that is outputting the data in the table gives array(0){}
twelve times-- the same number of items that I'm printing in the table.
var_dump($_REQUEST);
on functions.php doesn't print anything
console.log(response)
on JS file prints 0
.
Regardless if I put var_dump($_REQUEST)
in the if statement (before $wpdb->update
) or before the if statement, nothing gets printed.
Edit 2:
function getAjax() {
wp_localize_script( 'ajax_url_script', 'postAjax', array('ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script('ajax_url_script');
}