user1729708
user1729708

Reputation: 1

using query 'jeditable' with wordpress to update a table

I have a database (calendar) where authorized users can update table cells using jeditable. I am converting the calendar to a wordpress plugin to work on a wordpress site. I've studied a few answers describing how to use jeditable with wordpress ajax but I can't seem to get it working in my plugin. The jeditable plugin is working on my page, but I can't get the plugin to do the actual update to the wpdb table.

in my functions php file:

add_action( 'wp_ajax_update_record', 'update_record' );
function update_record() {
    global $wpdb;
    $field_id = $_POST['field_id'];
    $pieces = explode('-', $field_id);
    $field=$pieces[0];
    $id=$pieces[1];
    $newInfo = $_POST['newInfo'];
    $table_name = "myCalendar";
    $qry_result = $wpdb->update(
        $table_name,
        array( $field => $newInfo, 'id' => $id ),
        array( '%s', '%d' )
    );
    echo stripslashes($_POST['newInfo']);
    wp_die();
}

add_action( 'admin_enqueue_scripts', 'my_cal_scripts' );
add_action( 'wp_enqueue_scripts', 'my_cal_scripts' ); 

function my_cal_scripts() {

    wp_register_script(
        'ajax-script',
        plugin_dir_url( __FILE__ ) . 'js/my-cal-script.js',
        array( 'jquery-jeditable'),
        NULL,
        TRUE
    );

    $script_settings = array(
        'ajax_url' => admin_url( 'admin-ajax.php' ),
        'table_name' => 'myCalendar',
        'field_id' => $_POST['field_id'],
        'pieces' => explode('-', $field_id),
        'field' => $pieces[0],
        'id' => $pieces[1],
        'newInfo' => $_POST['newInfo'],
        'action'  => 'update_record'
    );

    wp_localize_script(
        'ajax-script',
        'ajax_object',
        $script_settings
     );

    wp_enqueue_script( 'jquery-jeditable' );
    wp_enqueue_script( 'ajax-script' );
}

and here's my jQuery script:

( function( $ ) {
    'use strict';
    $( document ).ready( function() {

        var data = {
            'action': ajax_object.update_record,
            'id' : ajax_object.field_id,
            'name' : ajax_object.newInfo
        };

        $('.edit').editable(function(value, settings) {

         $.post(
            ajax_object.ajax_url,
            {
                submitdata : data,
            }, function(response)
               {
           });
              return(value);
         },

         {
            indicator : 'Saving...',
            placeholder : '',
            tooltip   : 'Click to edit, press Enter to save...',
            cancel : 'Cancel',
            submit : 'Save',
            cancelcssclass : 'btn btn-danger',
            submitcssclass : 'btn btn-success',
            maxlength : 200
         });
    } );

} )( jQuery );

It LOOKS like everything is working EXCEPT the actual 'myCalendar' table is not getting updated.

What am I missing?

Upvotes: 0

Views: 52

Answers (1)

user1729708
user1729708

Reputation: 1

FINALLY! figured out the problem. my 'update_record' function to UPDATE the database has an error -- it should be:

$wpdb->update(
        $table_name,
        array( $field => $newInfo ),
        array( 'id' => $id ),
        array( '%s' )
    );

Consider the problem SOLVED.

Upvotes: 0

Related Questions