954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

AJAX Redirect Notworking in XMLHttpRequest

I'm Working with register module with xmlHttpRequest. I have checking all. When i was trying to redirect after successfull its displaying the response with in the given div.

<script type="text/javascript">
		function showRegister()
		{
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
			document.getElementById("result").innerHTML=xmlhttp.responseText;
			}
		  }
		  
		  var fname = document.getElementById("userfname").value;
		  var lname = document.getElementById("userlname").value;
		  var email = document.getElementById("userfemail").value;
		  var pass = document.getElementById("userfpass").value;
		  var gender = document.getElementById("userfgender").value;
		  var umonth = document.getElementById("usermonth").value;
		  var uday = document.getElementById("userday").value;
		  var uyear = document.getElementById("useryear").value;
		  var tc = document.getElementById("tc").checked;
		  
		  var registervalues = "fname=" + fname + "&lname=" + lname + "&email=" + email + "&pass=" + pass + "&gender=" + gender + "&umonth=" + umonth + "&uday=" + uday + "&uyear=" + uyear + "&tc=" + tc + "";
		  
		xmlhttp.open("POST","checkregister.php",true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlhttp.send(registervalues);
		}
		</script>
<?php
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$email = $_POST['email'];
	$pass = $_POST['pass'];
	$gender = $_POST['gender'];
	$month = $_POST['umonth'];
	$day = $_POST['uday'];
	$year = $_POST['uyear'];
	$terms = $_POST['tc'];
	
	if($fname == "" || $lname == "" || $email == "" || $pass == ""){
		echo "<p style='padding: 3px; text-align: center; border: 1px solid red;'>Please fill all the details</p>";
	}
	else if($month == "Month" || $day == "day" || $year == "Year"){
		echo "<p style='padding: 3px; text-align: center; border: 1px solid red;'>You must indicate your full birthday to register</p>";
	}
	else if($terms == "false" || $terms == FALSE){
		echo "<p style='padding: 3px; text-align: center; border: 1px solid red;'>You must agree our terms & conditions</p>";
	}
	else{
		header("Location: profile.html");
		exit;
	}
?>


Please help me ..

krishwebdesigne
Light Poster
27 posts since May 2011
Reputation Points: 14
Solved Threads: 1
 

Can you clarify as to what the issues is?

stbuchok
Master Poster
730 posts since May 2011
Reputation Points: 120
Solved Threads: 93
 

You are trying to redirect in a PHP file that is called via AJAX. That won't work. You should return redirect info to your AJAX call, so you can redirect using Javascript in your 200 success function.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

can you give me an example for 200 success function ?

krishwebdesigne
Light Poster
27 posts since May 2011
Reputation Points: 14
Solved Threads: 1
 

It's what's on your line 16 in your Javascript code above.

You need to somehow detect there that you have to redirect, and act accordingly. I think your best bet is to let your PHP file return a JSON result (instead of just HTML).

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: