2

I'm new to WordPress. I have a jQuery code like this... in my header.php file.

jQuery(document).ready(function(){
   jQuery("#wp-submit").click(function(){  
      username=jQuery("#userName").val();
      password=jQuery("#passWord").val();
      jQuery.ajax({
       type: "POST",
       url: "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
      data: "name="+username+"&pwd="+password,
       success: function(html){    
      if(html=='true')    {
       jQuery("div#result").css('display', 'inline', 'important');
       jQuery("div#result").html("Login Successful");
       console.log('true');
      }
      else{
       jQuery("div#result").css('display', 'inline', 'important');
       jQuery("div#result").html("Wrong username or password");
      }

       } });
    return false;
  });
});

and here is the php code in the same file (header.php):

   <?php 
      $uName = $_POST['name'];
      $pWord = md5($_POST['pwd']);
      $results= $wpdb->get_results("select * from wp_users where user_email='".$uName."' and user_pass='".$pWord."'");
      if(count($results)==1)
      {
       echo 'true'; 
      }

      else {
        echo 'false';
      }

I'm not able to send request to server, i got a 404 error.

What am i doing wrong?

  • Hi. Are you trying to access your website using www.example.com?name=Jay&pws=kapoor? Try adding a forward slash to it : www.example.com/?name=Jay&pws=kapoor. This might save you from a 404 error. – Jack Johansson Jun 6 '17 at 8:39
  • If you get a 404 error, you are able to send a request to the server. But apparently it is an invalid request. – cjbj Jun 6 '17 at 8:54
  • my question was how to link php file.. i mean what url should i provide in ajax url – Jay Kapoor Jun 6 '17 at 9:36
2

Ajax url should be set to wp-admin/admin-ajax.php as ajax requests should be handled via wp-admin/admin-ajax in wordpress. if ajaxurl is not defined you can get it by defining html attribute data-url as data-url="<?php echo get_admin_url().'admin-ajax.php'?>".

jQuery(document).ready(function($){
   $("#wp-submit").on('click',function(e){
      e.preventDefault();
      username = $("#userName").val();
      password = $("#passWord").val();
      ajaxurl  = $(this).data('url');
      $.ajax({

      type: "POST",
      url: ajaxurl,
      data : {

            name     : username,
            password : password,
            action   : 'check_user_login'

        },
       success: function(response){    
         if(response =='true')    {
             $("div#result").css('display', 'inline', 'important');
             $("div#result").html("Login Successful");
             console.log('true');
         }else{
            $("div#result").css('display', 'inline', 'important');
            $("div#result").html("Wrong username or password");
         }

       } 
    });
  });
});

Now Create your ajax function as

function user_login(){


   //do your stuff here
    global $wpdb;
    $username=$_POST['name']; 
    $password=$_POST['password']; 
    $hash = md5($password);
    $query = $wpdb->prepare( "SELECT * FROM `wp_users` WHERE `user_email` = %s AND `user_pass` = %s", $username, $hash );
    $result = $wpdb->get_results($query);
     if (count($result)==1) { echo 'true';
     echo '<span>Login Successful</span>'; exit(); 
    } else if(count($result) < 1) {
     echo 'false'; exit(); }
}

Now the ajax function should be hooked to hook wp_ajax_* and wp_ajax_nopriv_*. Here, * is your action value i.e data.action so it becomes

 add_action('wp_ajax_nopriv_check_user_login','user_login');
 add_action('wp_ajax_check_user_login','user_login');
  • Try to put this code in your funttions.php file ini_set('display_errors', 1); and check console if any error message is displayed. that's all for today. Good luck :) – Bikash Waiba Jun 6 '17 at 11:26
  • i think your sql query should have user_login instead of user_email as you are taking the username input not the email if you are using default wordpress wp_users table – Bikash Waiba Jun 6 '17 at 11:45
  • can u tell me how to compare wordpress hashed password. – Jay Kapoor Jun 7 '17 at 4:06
  • Go to this [link]codex.wordpress.org/Function_Reference/wp_hash_password here, go to example to see how it is to be used. Hope this works for you. I don't know what you are doing but you can use builtin login system instead of creating your own login system. – Bikash Waiba Jun 7 '17 at 5:25

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.