0

When I make a modification to data that is read into a form using MYSQL, I want to apply those changes to the database. But even when successfully submitting a form, the $_POST values are not changing, they remain with the old values.
I am seeing the changed value in AJAX, and I even try an insert into the database with the values in AJAX, but that is not working...
I have spent days on this, can anyone help? If I could get if(isset($_POST['FirstName'])) to fire off, then I would be good, but I can't seem to get this to happen. I will attach portions of my code, there is a lot, but I will attach the ones that are applicable.

// SNIPPETS FROM THE FORM:  This is contained in php_DOC.php
<form id="PhysicianForm" method="POST" enctype="multipart/form-data"
                autocomplete="off" action="<?php echo admin_url('admin-ajax.php'); ?>" >      
 <input type="hidden" name="action" value="CreateProfile">

// This prints out the modified FirstName value correctly, this is the only place I can get the modified value.  This is contained in php_DOC.php
    jQuery('#SubmitInsHrs').click( function () {
        var FirstName = jQuery('#FirstName').val();
            alert('FirstName being updated...', FirstName);
          console.log(FirstName);

        jQuery.ajax({
            url: 'admin-ajax.php',
            type: "POST",
            data: {
                'action': 'post_language',
                'FirstNM': FirstName,

            },
            success: function (data) {
                alert(data);
            }
        });

    });

These are in functions.php

add_action('wp_enqueue_scripts','enqueue_jqery_form');
add_action('wp_ajax_CreateProfile','CreateProfile');

function CreateProfile(){
  wp_send_json_success( $_POST );
}


function enqueue_jqery_form (){
  wp_enqueue_script('jquery-form');
}

I also tried this, to just force insert with new value, but no insert happens….

function post_language(){

    global $wpdb;
    alert('post_language is called');

    $FirstName = isset( $_POST['FirstName'] ) ? $_POST['FirstName'] : '';

    $table = 'Provider';

    $wpdb->insert( $table, array('FirstNM' => $FirstName));
}
add_action('wp_ajax_post_language', 'post_language');
  • Have you considered using the REST API endpoints for your AJAX requests instead? They're much easier to use and debug, if Admin AJAX fails it just gives you a cryptic message. REST API endpoints tell you in plain english why it failed. Also, your code doesn't sanitise or validate values, which can be a security problem, it just assumes the firstname is a first name, and not something else – Tom J Nowell Apr 30 at 18:53
  • Who can send this form? If the form is sent by a logged-in user, it's OK. But if it can be sent without logging into the WordPress, the proper action hook will be wp_ajax_nopriv_{action-name}, in this case wp_ajax_nopriv_CreateProfile. – nmr May 2 at 20:40
1

Add this to functions.php

add_action('wp_ajax_post_language','post_language');

and then make either of following changes in your code.

Because your jquery code is sending FirstNM to server (not FirstName). Therefore in post_language() function, change this line

$FirstName = isset( $_POST['FirstName'] ) ? $_POST['FirstName'] : '';

to

    $FirstName = isset( $_POST['FirstNM'] ) ? $_POST['FirstNM'] : '';

OR
Change 'FirstNM': FirstName, to 'FirstName': FirstName, in ajax call.

data: {
            'action': 'post_language',
            'FirstName': FirstName,     // 'FirstNM': FirstName,
        }

UPDATE:
Also remove this line,from post_language() function if yo have not defined alert() in your PHP It seems to be a JS function added to PHP by mistake, resulting in a PHP error.

    alert('post_language is called');
  • 1
    I think FirstNm => FirstName is indeed the issue here. – phatskat May 1 at 19:25
  • Well, I changed all occurrences of FirstName to FirstNM, so that all are the same. FirstNM is the column name in my database. FirstName is the form name. But this did not fix the issue. Thanks, – Monica May 2 at 14:02
  • Did you added add_action('wp_ajax_post_language','post_language'); to functions.php? – Qaisar Feroz May 2 at 16:25
  • @phatskat you are right. – Qaisar Feroz May 2 at 16:26
  • @Monica see the updated answer. – Qaisar Feroz May 2 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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