redirection does not work in i and Firefox but works in chrome

advertisements

my login form execute only in chrome. it doeasn't work in IE and firefox. it understand that this is the correct username but it doesn't redirect to the page. I use ajax and joomla. this is my code:

<script>
function Send()
{
jQuery.post("index.php?option=com_market&view=login&format=raw",
    {Username:jQuery("#username").val(),Pass:jQuery("#password").val()},
    function(data)
    {
        jQuery("#loginValid").html(data);
    }); 

}

</script>

<form name="loginForm" id="loginForm" method="post">
<label>Username:</label><input type="text" name="username" id="username"  /><br />
<label>Password:</label><input type="password" name="password" id="password" /><br />
<input type="button" onclick="Send();" value="login" name="loginSubmit" id="login"  />
</form>

the code of page in address: index.php?option=com_market&view=login&format=raw

<?php
session_start();
if(isset($_POST['Username'])) {
    $Username=$_POST['Username'];
    $db = JFactory::getDBO();
    $pass = md5($_POST['Pass']);
    $query ="select id from sb5qt_market_users where username='$Username' and     password='$pass'";
    $db->setQuery($query);
    $db->query();
    $rows = $db->getNumRows();
    $result = $db->loadResult();

    if ($rows == 0) {
        echo "invalid username or password";
    } else {
        $_SESSION['id']=$result;
        echo  '<META http-equiv="refresh" content="0;URL=http://example.com/profile">';
    }
}
?>


Seems to me like an odd way of redirecting and I'm not really surprised browsers are having trouble with it.

You could try this:

in PHP

if($rows == 0)
{
   echo json_encode(array("error" => "invalid username or password", "redirect" => ""));
}
else
{
   $_SESSION['id']=$result;
   echo json_encode(array("error" => "", "redirect" => "http://example.com/profile"));
}

Then in your Javascript something like...

jQuery.ajax({
    url: 'index.php?option=com_market&view=login&format=raw',
    data: 'Username=' + jQuery("#username").val() + "&Pass=" + jQuery("#password").val(),
    type: 'post',
    dataType: 'json',
    success: function(data)
    {
       if (data['error']) {
         jQuery("#loginValid").html(data['error']);
       }

       if (data['redirect']) {
         location.href = data['redirect'];
       }
   }
});

I haven't tested any of that but you can see what I'm getting at...