0

I have problem with reload in jQuery. After login user, the page not reload. Why?

I send a JSON for jQuery after login successfully. If JSON status is 1, must reload page, but it's not working. I have to refresh the page in browser and my profile.

This is my PHP code:

function loginUser()
{
    $strs = explode('/', $_SERVER['HTTP_REFERER'] );

    if ( strcmp($strs[2] ,$_SERVER['HTTP_HOST'])!=0)
    {
        header("HTTP/1.1 404 OK");
        exit;
    }
    //print $_POST['username'];die();
    $username = trim($_POST['username']);
    $password = trim($_POST['password']);

    if(empty($username) || empty($password))
    {
       echo  $error = "نوشتن نام کاربری و گذرواژه الزامی ست.";
    }
    else
    {
        $data=array('username'=>htmlentities(check_input($username),ENT_QUOTES, "UTF-8"),
                    'password'=>md5($password));
        //print_r($data);die();
        $user=new user();
        $result=$user->loginUser($data);
        //print_r($result);die();
        if ($result['status']) 
            {
                $_SESSION['username']=$result['username'];
                $_SESSION['u_id']=$result['id'];
                echo $user->returnJson(1);
            }
            else
            {
                echo $user->returnJson(0);
            }
    }
}

and this is my jQuery code:

function signin()
{
    $("#btn_signin").click(function(){
        if($("#username").val()!="" && $("#password").val()!=""){
            $("#frm_signin").ajaxSubmit({
                 url:"index.php?op=loginUser",
                 dataType:"json",
                 success:function(jret){
                     if(jret.status==1)
                     {
                         $(".err").hide();
                         location.reload();
                     }
                     else
                     {
                        $(".err").html("نام کاربری یا رمز عبور اشتباه است").show(); 
                     }
                 }
                });
        }
        else
        {
            $(".err").html("لطفا نام کاربری و رمز عبور خود را وارد کنید.").show();
        }
    })
}
Adinia
  • 3,722
  • 5
  • 40
  • 58
parlaq
  • 1
  • 1
  • 6
    [`md5($password)` - Really?](http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php) – Leigh Oct 17 '12 at 10:08
  • What does `echo $user->returnJson(1)` return? I would hope something like `json_encode(array('status' => 1));` so that your javascript can read it correctly? – Jon Oct 17 '12 at 10:09
  • 2
    Please check your JavaScript console for possible errors. Probably `location.reload()` is not a valid function in this context and you need to do a `window.location.reload()` or `window.location.href = ...`. – matthias Oct 17 '12 at 10:15
  • returnJson is a function in base class.public function returnJson($status,$message="",$data=Null) { return json_encode(array('status' => $status, 'message' => $message, 'data' => $data )); } – parlaq Oct 17 '12 at 10:17
  • i test `window.location.reload()` but not work.i use `reload()` and `window.location.href` in this site but work all of them except this!!! – parlaq Oct 17 '12 at 10:25
  • Use firebug console (Firefox Extension) and specify the error here – Umair Aziz Oct 17 '12 at 10:43
  • Does not show any error.its return json: `{"status":1,"message":"","data":null}` – parlaq Oct 17 '12 at 10:56

0 Answers0