You did some mistakes that I have described below:
- First thing you have placed jQuery Ajax code on the wrong place.
- On ajax callback function variable name mashup like $post_id defined and used on update time $post_ID; PHP is case sensitive so it considers 2 different variables.
- In some places, not closed } brackets.
Here is the right code which I have modified in your code.
Check this and let me know it’s work for you or not.
I have tested it works for me.
<?php /*Register the Menu*/
add_action( 'admin_menu', 'ews_metas_add_admin_page');
function ews_metas_add_admin_page() {
// Adds plugin page to menu
add_menu_page ('Effective Web Solutions Metas Descriptions', 'EWS Metas', 'manage_options', 'ews_meta_options_stephen_version', 'ews_meta_page_creation_stephen', get_template_directory_uri() . '/images/admin-ico-gray.png', 110 );
}
/*Page Html Render*/
function ews_meta_page_creation_stephen() { ?>
<div>
<?php settings_errors(); ?>
<form method="post">
<input id="meta" class="testing" type="text" name="2316" value="<?php echo esc_html( get_post_meta( 4189, '_metadescs', true ) ); ?>" />
<?php submit_button();?>
</form>
</div>
<script>
jQuery(document).ready( function($){
jQuery(".toplevel_page_ews_meta_options_stephen_version form").submit(function() {
var post_meta = jQuery("input.testing").val();
var post_ID = jQuery("input.testing").attr("name");
jQuery.ajax({
type: "POST",
url: 'https://dev.ewsproduction.com/dev6/ewstheme/wp-admin/admin-ajax.php',
data: {
action: "update_meta_descriptions",
post_id: post_ID,
post_meta: post_meta,
},
success: function( data ) {
//do something
console.log('It worked!');
}
});
return false;
});
});
</script>
<?php }
/*Update the meta using Ajax request */
add_action( 'wp_ajax_update_meta_descriptions', 'update_meta_descriptions' );
function update_meta_descriptions() {
global $wpdb;
$post_id = $_POST['post_id'];
$meta = $_POST['post_meta'];
$details = update_post_meta( $post_id, '_metadescs', $meta );
echo 'Meta Updated';
die();
}?>