0

I try to update data through ajax, but it not worked. What I did wrong? Here is jquery ajax code

php code in functions.php

function update_records(){
  global $wpdb;
  echo $id = $_POST['update_record'];
 
   $db_updated = $wpdb->update( $wpdb->prefix.'contact_form', 
        array('names'    => $_POST['update_name'],
              'emails'   => $_POST['update_email'],
              'gender'   => $_POST['update_gender'],
              'age'      => $_POST['update_age']), array( 'ID' => $id ) );
}
add_action( "wp_ajax_update_records", "update_records" );
add_action( "wp_ajax_nopriv_update_records", "update_records" );

jquery ajax code

jQuery('.upd_btn').click(function(){
        var id = jQuery(this).attr('data-id');
         var name = jQuery(this).attr('data-name');
         var email = jQuery(this).attr('data-email');
         var gender = jQuery(this).attr('data-gender');
         var age = jQuery(this).attr('data-age');

        alert(age);

        $.ajax({
            url: '<?php echo admin_url('admin-ajax.php');?>',
            type: 'POST',
            data:{ 
            action: 'display_func', 
              update_record:id,
              // update_name:name,
              // update_email:email,
              // update_gender:gender,
              // update_age:age,
            },
            success: function( data ){
                // alert("Records are successfully update");
                location.reload();
            }
         });
    });

ajax error screenshot - https://prnt.sc/wdon1x

2
  • wrong is not an error - please provide more details about how you debug this process and also share what JS or PHP errors you found.
    – Q Studio
    Commented Dec 31, 2020 at 12:55
  • I add screen shot above
    – vishal
    Commented Dec 31, 2020 at 14:30

2 Answers 2

2

First of all, in your jquery event handler the action parameter's value should be the string after wp_ajax_ and wp_ajax_nopriv_. So in your example the action should be update_records. So display_func is wrong.
Then in line 3 of your php code here the echo keyword should be removed.
So your php code should be like this:

function update_records(){
    global $wpdb;
    $id = $_POST['update_record'];

    $db_updated = $wpdb->update( $wpdb->prefix.'contact_form', 
        array(
            'names'  => $_POST['update_name'],
            'emails' => $_POST['update_email'],
            'gender' => $_POST['update_gender'],
            'age'    => $_POST['update_age']
        ), 
        array( 'ID' => $id ) 
    );
}  
add_action( "wp_ajax_update_records", "update_records" );
add_action( "wp_ajax_nopriv_update_records", "update_records" );
3
  • 1
    Sanitization the data before updating into the WordPress table. Here is documentation for that: codex.wordpress.org/… It will prevent your site from hackers/attackers. Commented Jan 1, 2021 at 5:38
  • @Baikare Sandeep, could you tell me how send all form field data through anchor tag
    – vishal
    Commented Jan 1, 2021 at 11:04
  • 1
    @BaikareSandeep The wpdb::update() method calls wpdb::prepare() and it will do all necessary sanitizings.
    – Had1z
    Commented Jan 1, 2021 at 11:17
0

Please sanitize the data before inserting or updating into the database to prevent from attackers Read Documenation:


function update_records(){
    global $wpdb;
    $id = intval(sanitize_text_field($_POST['update_record']));

    $db_updated = $wpdb->update( $wpdb->prefix.'contact_form', 
        array(
            'names'  => sanitize_text_field($_POST['update_name']),
            'emails' => sanitize_email($_POST['update_email']),
            'gender' => sanitize_text_field($_POST['update_gender']),
            'age'    => intval(sanitize_text_field($_POST['update_age']))
        ), 
        array( 'ID' => $id ) 
    );
}  
add_action( "wp_ajax_update_records", "update_records" );
add_action( "wp_ajax_nopriv_update_records", "update_records" );
1
  • The wpdb::update() method calls wpdb::prepare() and it will do all necessary sanitizings.
    – Had1z
    Commented Jan 1, 2021 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.