• Resolved dgcov

    (@dgcov)


    In my javascript file:

    jQuery(document).ready( function($) {
            data={'action': 'check_email_exists','umail': field.value};
            jQuery.post( ajax_url, data, function(response) {
              console.log(data);
              console.log(response);
              if(response=='true'){
                warning=document.getElementById('email_r');
                warning.innerHTML='This email is already taken';
                warning.style.display='inline';
                warning.scrollIntoView();
                return true;
              }
            });
          });

    In my plugin’s php file:

    add_action('wp_ajax_nopriv_checkemail', 'check_email_exists');
    add_action( 'wp_enqueue_scripts', 'call_external_scripts' );
    
    function check_email_exists(){
      global $wpdb;
      $sql="SELECT id FROM ".$wpdb->prefix."users WHERE user_email = '".$_POST['umail']."';";
      //echo $sql;
      $results = $wpdb->get_results($sql);
      if($results){
        echo 'true';
      }else{
        echo 'false';
      }
      exit;
    }

    The function is not called.

    If I add this to the php file:

    if($_POST['action']){
        global $wpdb;
        if($_POST['action']=='check_email_exists'){
          $sql="SELECT id FROM ".$wpdb->prefix."users WHERE user_email = '".$_POST['umail']."';";
          //echo $sql;
          $results = $wpdb->get_results($sql);
          if($results){
            echo 'true';
          }else{
            echo 'false';
          }
        }//*/
        exit;
      }//*/

    Then it works. The javascript gets ‘true’ if the user exists and ‘false’ if he doesn’t.

    The problem is that the site is borked. Any act of saving a page or a post or editing a user results in a whitescreen.

    Baffling…

Viewing 1 replies (of 1 total)
  • Thread Starter dgcov

    (@dgcov)

    Okay.

    No idea why the whitescreen occurs, but I’ve removed the code:

    if($_POST['action']){
        global $wpdb;
        if($_POST['action']=='check_email_exists'){
          $sql="SELECT id FROM ".$wpdb->prefix."users WHERE user_email = '".$_POST['umail']."';";
          $results = $wpdb->get_results($sql);
          if($results){
            echo 'true';
          }else{
            echo 'false';
          }
        }
        exit;
      }

    and the whitescreen no longer occurs.

    I’ve changed this:

    add_action('wp_ajax_checkemail', 'check_email_exists');
    add_action('wp_ajax_nopriv_checkemail', 'check_email_exists');

    to this:

    add_action('wp_ajax_checkemail_exists', 'checkemail_exists');
    add_action('wp_ajax_nopriv_checkemail_exists', 'checkemail_exists');

    ie wp_ajax_checkemail_exists matches checkemail_exists

    The Ajax callback now works. ??

Viewing 1 replies (of 1 total)
  • The topic ‘Ajax does not fire my function’ is closed to new replies.