AJAX / PHP header("Location: http://new-url.php");

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Designer
Forum Newbie
Posts: 12
Joined: Fri Nov 19, 2010 5:57 am

AJAX / PHP header("Location: http://new-url.php");

Post by Designer »

Hi Guy's

Ref: Close Current URL and Transfer control to new URL.

I’m developing a new website where the home page (index.php) has a ‘log-in’ form in the top right hand.

I’m using AJAX to validate which all works completely fine, however once the input has been validated as OK, I want to reload a new page.

My first response was to use:

header("Location: http://www.mysite.com/client-accounts.php");

This sort of works, but unfortunately, it loads the page inside the <div></div> which is populated by the AJAX code.

Basically, once validation is successful I want to transfer control to a completely new page and remove all traces of previous page from the browser.

I think the answer might involve some additional ‘header();’ but I’m running out of ideas.

Any help greatly appreciated.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Neilos »

Could you post your code?
Designer
Forum Newbie
Posts: 12
Joined: Fri Nov 19, 2010 5:57 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Designer »

All of it?
Designer
Forum Newbie
Posts: 12
Joined: Fri Nov 19, 2010 5:57 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Designer »

I'm going to create a very basic example and post up this code.

If we can get this to work, I'll know how to proceed. I'll post up some code a bit later.

Cheers.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Celauran »

Any reason you're not just having the login form's action point to the new page?
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Neilos »

I'm not familiar with AJAX. Maybe someone can answer it easily but I need to see it to know what's happening lol. It'd be handy to know how and where you are writing the header from.

Just give me that bit, that should be enough.
Designer
Forum Newbie
Posts: 12
Joined: Fri Nov 19, 2010 5:57 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Designer »

Any reason you're not just having the login form's action point to the new page?

When using AJAX as I understand it the action of the form point to the javascript file which then returns control back to the php file which is a refresh of the code between the division tags.

I'm almost complete with a very basic example of the code I'm having the problem with. I'll post examples of code and a working example link very soon.
Designer
Forum Newbie
Posts: 12
Joined: Fri Nov 19, 2010 5:57 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Designer »

Right - Sorry for the delay!

Ironically, I've now got more problems with the 'simple example' than I have with the original. However, please take a look at the link below:-

http://www.siteexpeditor.com/example-index.php

I have remove most of the validation so you only need enter a value for email address and password (ie. x & x ).

If you don't enter a value notice it all works correctly apart from the --error message-- (very top of page) I now have on the example, but not in original code! I need to resolve this as well, but this is not the big issue.

Anyway enter some values and click log-in. You might need to click log-in twice at the moment due to this extra error! However, when the next screen does come up it should be on a new URL but instead it has inserted it into the division used by the login form.

QUESTION: - Why does using: -

header("Location: http://www.siteexpeditor.com/example-client-page.php");

Not load a completely new page?
Designer
Forum Newbie
Posts: 12
Joined: Fri Nov 19, 2010 5:57 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Designer »

example-index.php

<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<meta http-equiv='Content-Style-Type' content='text/css' />
<link rel='shortcut icon' href='images/siteexpeditor.ico' />
<link href='css/layout.css' rel='stylesheet' type='text/css' />
<link href='css/style.css' rel='stylesheet' type='text/css' />
<link href='css/style-colours1.css' rel='stylesheet' type='text/css' />
<!-- include jQuery library -->
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript' src='js/example.js'></script>
<title>Example Index</title>
</head>
<body class="twoColFixLtHdr">
<div id="container">
<!-- header -->
<div id="dataArea"></div>
<!-- end #header -->

<!-- #mainContent -->
<div id="mainContent">



</div>
<!-- end #mainContent -->
<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
<br class="clearfloat" />

<!-- footer -->
<?php include ("php/footer.php"); ?>
<!-- end #footer -->

<!-- end #container -->
<?php
$trans_ID = "0";
echo "<script type='text/javascript' src='http://www.siteexpeditor.com/js/example.js#obj1/" . $trans_ID . "/obj2/" . "0" . "/obj3/" . "0" . "/obj4/" . "0" . "/obj5/" . "0" . "'></script>";
?>
</div>
</body>
</html>



Example of Javascript Code

<!--
var myParams = getScriptUrlParams();
str1 = (myParams["obj1"]);
str2 = (myParams["obj2"]);
str3 = (myParams["obj3"]);
str4 = (myParams["obj4"]);
str5 = (myParams["obj5"])

function getScriptUrlParams()
{
var scriptTags = document.getElementsByTagName("script");
// This code is assumed to be in a file so the "src" attribute
// is guaranteed to be present...no error-checking is needed
var urlFrags = scriptTags[scriptTags.length-1].src.split("#");
var urlParams=[];
var urlParamRaw = [];

if (urlFrags.length > 1)
{
urlParamRaw = urlFrags[1].split("/");
if (urlParamRaw.length >= 2)
{
for(var param=0;param<urlParamRaw.length;param+=2)
urlParams[urlParamRaw[param]] = (urlParamRaw.length >= param + 1 ? unescape(urlParamRaw[param+1]) : null);
}
}
return(urlParams);
}



//
// This function controls the main processing
// ** M A I N P R O C E S S I N G C O N T R O L

getData(str1,str2,str3,str4,str5);

function getData(str1,str2,str3,str4,str5) {
var xmlHttp;
str1 = String(str1);
str2 = String(str2);
str3 = String(str3);
str4 = String(str4);
str5 = String(str5);

showData(str1,str2,str3,str4,str5);
}





//Called functions below this line

function showData(str1,str2,str3,str4,str5)
{
xmlHttp=GetDataXmlHttpObject()
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request")
return
}

str1 = String(str1);
str2 = String(str2);
str3 = String(str3);
str4 = String(str4);
str5 = String(str5);


switch(str1)
{
case "0":
var url="http://www.siteexpeditor.com/php/example-login-data.php"
//var x=document.getElementById("input#email");
//str2 = (x.value);

str2 = $("input#email").val();
str3 = $("input#password").val();
url=url+"?xxx="+str1+"&email="+str2+"&password="+str3+"&errorno="+str4+"&transpass="+str5;
break;
default:
document.write("Unknown Transaction ID (" + str1 + ") - getData.js");
}

url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=getDataStateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}





function getDataStateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("dataArea").innerHTML=xmlHttp.responseText
}
}


function GetDataXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
// -->


Example of PHP Code called by above Javascript and injected into the division (id=dataArea) with example-index.php

<?php
session_start();
$trans_ID = $_GET['xxx'];
$transpass = $_GET['transpass'];

if ($transpass == 1) {
$message = "";
$emailerr = false;
$passworderr = false;

$email = trim($_GET['email']);
$length = strlen($email);
if ($length == 0) {
$emailerr = true;
$message = "MISSING EMAIL ADDRESS";
}
else {
//
//Password missing - return control to sign-on screen
//
$password = trim($_GET['password']);
$length = strlen($password);

if ($length == 0) {
$passworderr = true;
$message = "MISSING PASSWORD";
}
}



if (($emailerr == false) && ($passworderr == false)) {
//VALID SIGN ON - Pass control to client accounts
$_SESSION['client_ID'] = $client_data['ID'];
$_SESSION['account_ID'] = $client_data['default_account_ID'];
header("Location: http://www.siteexpeditor.com/example-client-page.php");
exit;
} //End if
} //End if

if ((((($emailerr == true) || ($passworderr == true)) && ($transpass == 1)) || ($transpass == 0 ))) {
echo "<div id='header'>";
echo " <table class='maxWidth'>";
echo " <tr>";
echo " <td>";
echo " <a href='index.php'><img src='images/logo.png' id='logo' alt='' /></a>";
echo " </td>";
echo " <td class='alignRight'>";
echo " <form id='login-form' method='post' action='javascript:getData(" . $trans_ID . "," . "0" . "," . "0" . "," . "0" . "," . "1" . ")'>";
echo " <table>";
echo " <tr>";
echo " <td><label>Email</label></td>";
echo " <td><label>Password</label></td>";
echo " <td></td>";
echo " </tr>";

echo " <tr>";
echo " <td><input type='text' id='email' name='email' value='$email' /></td>";
echo " <td><input type='password' id='password' value='$password' /></td>";
echo " <td><input type='submit' value='Log in' id='login-submit' /></td>";
echo " </tr>";

echo " <tr>";
echo " <td><input type='checkbox' name='keep' value='' />&nbsp;<label>Keep me logged in</label></td>";
echo " <td><label><a href='#'>Forgotten your password</a></label></td>";
echo " <td></td>";
echo " </tr>";

echo " <tr>";
echo " <td colspan='3' class='highlight'>" . $message . "</td>";
echo " </tr>";

echo " </table>";
echo " </form>";
echo " </td>";
echo " </tr>";
echo " </table>";
echo "</div>";
}
?>

:banghead:
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Neilos »

I see that the page is loading into the other element, like using a jframe. Could you cut and paste the section of code from your source where you all the header? I cannot see your php script from the browser. My knowledge of AJAX is nil but I may ne able to see it if I can see your source :) not all if it's lengthy just the part where you perform the redirect.
Designer
Forum Newbie
Posts: 12
Joined: Fri Nov 19, 2010 5:57 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Designer »

I believe the solution to this problem is likely to lie with using some additional headers?

:crazy:
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Neilos »

I don't think that you need additional headers. Notice how if you use the code;

Code: Select all

<?php
header("Location: http://www.siteexpeditor.com/example-client-page.php");
?>
On a new page, any page, try it and navigate to it. The page loads as you would expect, and as I would have expected. It is curious why it is loading inside the div element, so I think that it is to do with the positioning of header statement in your code. I'm having a play with it but I'm no expert so we may be here a while :)

Also sorry, you were posting the code as I was replying with my last message, ignore it :roll:
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Neilos »

Also just a thought have you tried using a meta refresh tag to redirect the page? To see if you get the same problem

<meta http-equiv="refresh" content="2;url=http://www.siteexpeditor.com/example-client-page.php/">

Not a long term fix but may help narrow the problem
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

Re: AJAX / PHP header("Location: http://new-url.php");

Post by s992 »

I think Neilos is probably right. I didn't have time to give your code a thorough look, but this jumps out at me:

Code: Select all

<div id="container">

<!-- i cut out the code in the middle -->

<!-- end #container -->
<?php
$trans_ID = "0";
echo "<script type='text/javascript' src='http://www.siteexpeditor.com/js/example.js#obj1/" . $trans_ID . "/obj2/" . "0" . "/obj3/" . "0" . "/obj4/" . "0" . "/obj5/" . "0" . "'></script>";
?>
</div>
You close the container div after running your Javascript..if you switch those two lines, does it work?
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: AJAX / PHP header("Location: http://new-url.php");

Post by Neilos »

Sounds like a likely culprit.

What does this do?

Code: Select all

<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats -->
<br class="clearfloat" />
force the #container div to contain all child floats hmmmmmmz

I can't get your code to run easily, and I don't have time to hack away at it all night so I can't investigate these bits but they seem suspicious.
Post Reply