admin管理员组

文章数量:1429027

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');

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');
Share Improve this question edited Apr 30, 2019 at 18:44 Qaisar Feroz 2,1471 gold badge9 silver badges20 bronze badges asked Apr 30, 2019 at 17:23 MonicaMonica 1 2
  • 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 Commented Apr 30, 2019 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 Commented May 2, 2019 at 20:40
Add a comment  | 

1 Answer 1

Reset to default 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');

本文标签: phpA POST should occur when submit form but is not