Skip to main content
1 of 5

Ajax not updating to database

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!