Maybe I'm doing this wrong, but here's what I'm trying to do.
Ok, so how do I send the URL to a new page? Right now I understand the writing ajax to a div, replacing the contents of the div, appending it etc. But what about if I am using the below code, for like a login area, I can get the form to return wrong username etc based on the submitting, but if everything checks out as correct in my ajax-login.php file, the form doesn't follow the headers at the bottom of the page. What would I use to make it follow the headers from this page? Or is there a better way?
Thanks
ajax-login.php

a bunch of code, grabbing the values from the form and checking if they are valid passwords and things like that like;
if ($usernamecheck == ''){
  echo "<div id=\"log_res2\">";
        echo "<span class=\"error\">*Must enter a username.</span><br />";
          echo "</div>";
  }else{
header("location: /index.php");
}

And my ajax is

$(document).ready(function()
{
$("#Login").click(function(){


var $password = $("#password").val();
var $username = $("#username").val();

var dataString = 'username=' + $username + '&password=' + $password;


$.ajax({
type: "POST",
url: "../includeajax/ajax-login.php",
data: dataString,
cache: false,
success: function(html){
$("#log_res2").replaceWith(html);
}
});


return false;
});
});

I get the errors when I put in the wrong usernames or passwords, but how do I return that header to make it redirect to a new page when there aren't any errors?
Thanks

Member Avatar for stbuchok

Does your login work if someone has JavaScript disabled? More importantly, can someone circumvent the login if JavaScript is disabled for the browser?

Nope, I'm only letting javascript people get on my site, because it's pretty much all ajax based. I got the login to work, well, mostly, it gets the sessions and everything, and if I hit refresh if there are no errors, then the cookies have been set etc. I just can't get the ajax to refresh the page after the page runs successful. Basically I put an echo "success"; in my php file and I put

success: function(html){
if(html == 'success'){

  window.location.replace('http://www.thetrue1.com');

}else{
$("#log_res2").replaceWith(html);
}
}

In my ajax file, but the window.location.replace doesn't work, it won't refresh the page. :(

Member Avatar for stbuchok

The replace function takes 2 arguments. Also, you should do your redirecting on the serverside not in JavaScript.

Umm, ok, what are those 2 arguments? And how do you do this on the server side? I was doing a header thru the php file that the ajax script looks at, but that doesn't work for me. also, whats the purpose of doing this on the server side rather than in Javascript? Benifits? Also, you mentioned earlier doing this for people without javascript as well, is there a simple way of doing this? Do you just include the php to call to the action functions on the php script, because I had these in for my whole site, but decided to do it all in ajax, so now I have ajax instead of php includes to the $_POSTs instead of on action from the forms.
Thanks

Ok, I figured this out, the redirect didn't take 2 arguements, I had an error in my PHP file when I alerted that, it didn't just give 'success' it also gave the error code, all fixed now. :)

Member Avatar for stbuchok

I never said redirect takes 2 arguements, I said replace does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.