thank you so much Tom and Sally.
My issue was indeed to not send registr_submit to.
In reallity I didn’t well understand ajax, now it’s good. I thought that serialize string of inputs form was enough to get the submit value. Mainly I convinced that php send to jquery/ajax while it is obviously the opposite.
So, in order :
ajaxdata.js:
$(function(){
$('#registr_submit').on('click', function(e){
e.preventDefault();
var userdata = $('#my_registration_form').serialize();
userdata += '&action=add_new_user®submit=registr_submit';
var username = $('#username').val();
$.ajax({
url : wpAjax.ajaxUrl,
data : userdata,
type : 'POST',
dataType: 'json',
success : function(response){
if(username == ''){
if(response.success == false) {
$('#error-emp-name').html(response.data);
}
}
else{
if(response.success == true) {
$('#error-emp-name').html('Correcte');
}
}
}
}
});
});
phpforajax.php:
wp_localize_script('ajax', 'wpAjax', array('ajaxUrl' => admin_url('admin-ajax.php')));
wp_enqueue_script('data-login', plugins_url('', LQUSER) . '/data/ajaxdata.js', array('jquery'), '1.0', true);
add_action('wp_ajax_add_new_user','add_new_user');
add_action('wp_ajax_nopriv_add_new_user','add_new_user');
function add_new_user(){
function add_new_user(){
global $wpdb;
if (isset($_POST['regsubmit']) && wp_verify_nonce($_POST['csrf'], 'csrf')) {
$username = $_POST['username'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$newpassword = $_POST['newpassword'];
$confirmpassword= $_POST['confirmpassword'];
$roleuser="subscriber";
if (empty($username)) {
wp_send_json_error('Username is required');
}else{
wp_send_json_success('Ok');
}
}