Forum Moderators: coopster

Message Too Old, No Replies

header with ajax and php

i don noe y u ppl are not answering me...this is a different question plz..

         

ayushchd

6:18 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



<?
session_start();
if (!isset($_COOKIE['user'])) {
header ("location : index.php"); exit ();
} else if (!session_is_registered("password")) {
header ("location : index.php"); exit ();
} else {
.............the rest of the code..............

this is the file (sell.php) which is requested by a javascript function and the output is inserted into a div with name "txthint" in a page (shares.php). The problem i m facing is that when the cookie is not set or the session is not registered, the page refreshes, is headed towards index.php and the contents of index.php are displayed inside the div "txthint" along with the contents of shares.php but what i want is that when the cookie is not set or session is not registered and sell.php is requested, it shud refresh the page and display index.php....

Psychopsia

7:54 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



I think in this solution
Instead of use header to redirect, print some text to send to ajax, and then redirect from javascript.

Example:

[php]
...
} else if (!session_is_registered("password")) {
echo 'redirect_index'; exit ();
...

[javascript]
response = ajax.responseText;
if (response == 'redirect_index')
{
window.location = 'index.php';
}

ayushchd

10:06 pm on Jul 30, 2007 (gmt 0)

10+ Year Member



didnt work instead the text is displayed, the common text...'Please Login Again' that is the response ...this is what is tried:

function showHint(id, company, squantity, tquantity){
var AdminRequest = window.ActiveXObject? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
AdminRequest.onreadystatechange = function()
{
if (AdminRequest.readyState == 4)
{
if (AdminRequest.status == 200)
{
document.getElementById(id).innerHTML = AdminRequest.responseText;
}
else {
alert('Error get.php File '+ AdminRequest.statusText);
}
}
}
var response = AdminRequest.responseText;
if (response == 'Please Login Again')
{
window.location = 'index.php';
}
var infoStr = "?id="+id+"&company="+company+"&squantity="+squantity+"&tquantity="+tquantity;
AdminRequest.open("GET", "sell.php"+infoStr, true);
AdminRequest.send(null);
}

Psychopsia

4:59 am on Jul 31, 2007 (gmt 0)

10+ Year Member



Move the response var inside "if (AdminRequest.status == 200)"

Should be:

if (AdminRequest.status == 200)
{
var response = AdminRequest.responseText;
if (response == 'Please Login Again')
{
window.location = 'index.php';
}
document.getElementById(id).innerHTML = AdminRequest.responseText;
}