0

I know that this has been frequently asked here, but none of the solutions I find seem to work for me.

The issue is as follows: whenever I post the form, I have a console log with the response which is a full html page rather than a simple message.

Backend script:

    add_action( 'admin_ajax_nopriv_email_verification_form', 'verify_and_sanitize_email_form' );
add_action( 'admin_ajax_email_verification_form', 'verify_and_sanitize_email_form' );

// Email verification callback
function verify_and_sanitize_email_form() {
    // Check referer
    check_ajax_referer( '9pU0Pk7T01', 'security' );

    if(empty($_POST) || !isset($_POST['rguroo_email']) || !isset($_POST['rguroo_email_confirmation']) || !isset($_POST['rguroo_desired_action'])) {
        echo 'There is one or more empty fields';
        wp_die();
    }
    $sanitized_email = sanitize_email( esc_html($_POST['rguroo_email'] ));
    $sanitized_email_confirmation = sanitize_email( esc_html($_POST['rguroo_email_confirmation'] ));
    $desired_action = esc_html($_POST['rguroo_desired_action']);

    if(!is_email( $sanitized_email ) || !is_email( $sanitized_email_confirmation )) {
        echo 'Email is not valid';
        wp_die();
    }

    if($sanitized_email !== $sanitized_email_confirmation) {
        echo 'Emails do not match';
        wp_die();
    }

    if($desired_action !== 'purchase' || $desired_action !== 'renewal' || $desired_action !== 'trial') {
        echo 'Fatal error with radio buttons';
        wp_die();
    }

    if(!isset($_COOKIE['rguroo_form_type'])) {
        echo 'Server error';
        wp_die();
    }

    // student email verification logic
    $form_type = $_COOKIE['rguroo_form_type'];
    if($form_type === 'student') {
        $trail = substr($sanitized_email, -4);
        if($trail !== '.edu') {
            echo 'Not a valid student email';
            wp_die();
        }
        // Other university specific logic here
    }
    setcookie('rguroo_form_action',$desired_action, 14 * DAY_IN_SECONDS);
    setcookie('rguroo_form_email', $sanitized_email, 14 * DAY_IN_SECONDS);
    echo "success";
    wp_die();
}

Frontend javascript:

jQuery(document).ready(function () {
  jQuery('form#email-verification').on( 'submit', function () {
    var form_data = jQuery(this).serializeArray();
    form_data.push( { "name" : "security", "value" : ajax_info.ajax_nonce } );
    jQuery.ajax({
          url : ajax_info.ajax_url,
          type : 'post',
          data : form_data,
          success : function( response ) {
              console.log(response);
              if(response !== 'success') {
                jQuery('.error').innerHTML = response;
              } else {
                location.reload();
              }
          },
          fail : function( err ) {
              jQuery('.error').innerHTML = "Cannot contact server";
          }
      });
    return false;
  });
});

Form (used in a shortcode):

function output_email_verification() {
    return '<form action="'.esc_url( admin_url("admin-ajax.php") ).'" method="post" id="email-verification">
                <p class="error">'.$error.'</p>
                <input type="radio" label="Purchase 12 months access" value="purchase" name="rguroo_desired_action" checked>Purchase 12 months access</input>
                <input type="radio" label="Renew account" name="rguroo_desired_action" value="renewal">Renew account</input>
                <input type="radio" label="Create trial account" name="rguroo_desired_action" value="trial">Create trial account</input>
                <p class="form-subtext">* indicates required field</p>
                <p>Email address*</p>
                <input type="text" name="rguroo_email" required>
                <p>Re-type email address*</p>
                <input type="text" name="rguroo_email_confirmation" required>
                <input type="hidden" name="action" value="email_verification_form">
                <input type="submit" value="Submit"> 
            </form>';
}

Things I know: there isn't an issue with jQuery sending the post (or at least making the post request). On submit jQuery sends the post with all of the correct parameters. I also know that I'm not using different nonces. I have tried placing error_logs() all around the callback function, but none of them ever got displayed in debug.log. This leads me to believe that the callback is never actually called, but I don't know why. Also, even if I give the form a test which should fail, jQuery still reads it as a success.

I've wasted all day on this form and feel like an idiot. Could someone with a few more braincells than me please tell me where I'm going wrong?

Thank you so much.

  • You should not echo your php, use wp_send_json for your response back to javascript. – Nathan Powell Jun 19 at 21:24
  • @NathanPowell I've tried changing the code but it still doesn't work. I used this for errors: wp_send_json_error(array('error'=>'Email is not valid')); and I have a wp_send_json_success(); at the end. One thing of note though is that I'm entering "test" and "test" for both email fields. Which means that the response should fail with wp_send_json_error(). However, it still succeeds and outputs the page to the console – Eli Jun 19 at 21:37
0

A call to this function complements the die() PHP function. The difference is that HTML will be displayed to the user in the case of a typical web request

Taken from here

Have you tried to use die() instead of wp_die() or exit()?

  • Still not working. Besides, if the callback was working properly, the test case that I'm using would fail, which would result in nothing being console logged at all. In other words, I don't think I'm even getting to wp_die() – Eli Jun 20 at 9:28
0

you need an action in your ajax request to call a php function

url : ajax_info.ajax_url,
type : 'post',
data : form_data,
action: email_output_verification,
  • That's not it. There is already an action. From the form: <input type="hidden" name="action" value="email_verification_form">, from the javascript: var form_data = jQuery(this).serializeArray();. It gets included when I serialize the form. Just as a sanity check I used the dev tools and looked at the parameters that my post was getting sent with. As you can see from this screenshot, the parameters are getting properly set and sent – Eli Jun 20 at 4:46

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.