I can\'t understand why I get always a null or undefined response while trying to pass an php error from registration form into ajax script.
I work in a custom plugin to add login, registration and dashboard with the folder.
History : I create an ajax registration form , for each field I want create an error (if there are errors) and display it below the field.
I tried with basic var ($_POST
), json_encode
and wp_send_json_error
.
Each time I get a null or undefined response in console.log
.
I know that my ajax script work because data $_POST are well pass.
But I can\'t solve my issue or I don\'t understand what I do with Json !
I have read ajax doc and followed lot of docs about the same issue without results.
Currently basic thing doesn\'t work , could you help me ?
I use the same js/ajax file for login, registration and dashboard .
Can there be a conflict ?
For now I want to test only the username
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(){
if (isset($_POST[\'registr_submit\']) && wp_verify_nonce($_POST[\'csrf\'], \'csrf\')) {
// User data registration
$username = $_POST[\'username\'];
/*
$firstname = $_POST[\'firstname\'];
$lastname = $_POST[\'lastname\'];
$email = $_POST[\'email\'];
$newpassword = $_POST[\'newpassword\'];
$confirmpassword= $_POST[\'confirmpassword\'];
$roleuser = \'subscriber\';
verify only username
*/
if(empty($username)){
$data = [];
$data[\'success\'] = false;
$data[\'message\'] = \'Username is required\';
echo json_encode($return);
}
// before to use wp_send_json_error I must find my mistake or my forgetting
//if i use wp_die(); the form isn\'t pass to ajax
}
my_registration_form.php
<form class="registr-form" action="" method="POST" id="my_registration_form">
<input type="text" name="username" id="username" placeholder="Votre nom d\'utilisateur" autocomplete="username"/>
<span id="error-emp-name"></span>
<input type="hidden" name="csrf" value="<?php echo wp_create_nonce(\'csrf\'); ?>"/>
<input type="submit" id="registr_submit" name="registr_submit" class="submit-registr-btn" value="Je valide">
</form>
ajaxdata.js
:
$(function(){
$(\'#registr_submit\').on(\'click\', function(e){
e.preventDefault();
$.ajax({
url : wpAjax.ajaxUrl,
data : {action:\'add_new_user\'},
type : \'POST\',
dataType: \'json\',
success : function(response){
var data = $.parseJSON(response);
// below work and display input
console.log(username);
// below doesn\'t work
if(data.success == false){
console.log(response.message); //must display : Username is required.
}
}
});
});
SO网友:imagIne
非常感谢汤姆和萨利。我的问题确实是不向发送registr\\u submit。实际上,我对ajax不是很了解,现在它很好。我认为序列化输入字符串表单足以获得提交值。我主要相信php发送到jquery/ajax,而这显然是相反的。因此,按顺序:
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\');
}
}