0

Hi All,

I have a PHP code which do some changes to the POST variables it receives and redirect the page to some other page. This PHP code was initially designed to work without Ajax. So there wasn't any problem with the redirect function and it perfectly handles the POSt variables and redirect the user to another page.

So now I want to use the same PHP code with Ajax... :)
I tried it and and it did not gave me the results I was expecting. Problem is with the redirect function. Other PHP lines execute perfectly fine( it stores some data in the database).

So is there any way to ignore the redirect call or just to filter out the redirecting url into a javascript variable ?

Thanks in advance for taking some time to read this

3
Contributors
7
Replies
14
Views
6 Years
Discussion Span
Last Post by Stefano Mtangoo
0

If you do not want any redirection to take place when viewing the page via ajax, what you can do is to send a parameter that identifies the request as being ajax. In your php code, immediately before the redirect code see if you detect that parameter and if you do, then do NOT redirect.

To clarify, if the url you are sending the requests to is http://site.com/file.php, then simply make the requests to http://site.com/file.php?rt=ajax (rt=requestType).

Then, assuming you currently are redirecting using header("Location: ..."); , then it would now be:

if( !isset($_GET['rt'])  || $_GET['rt']!='ajax' )
{
  header("Location: ...");
  exit();
}

That way if the page is NOT accessed via ajax, it would still redirect.

0

Hi All,

I have a PHP code which do some changes to the POST variables it receives and redirect the page to some other page. This PHP code was initially designed to work without Ajax. So there wasn't any problem with the redirect function and it perfectly handles the POSt variables and redirect the user to another page.

So now I want to use the same PHP code with Ajax... :)
I tried it and and it did not gave me the results I was expecting. Problem is with the redirect function. Other PHP lines execute perfectly fine( it stores some data in the database).

So is there any way to ignore the redirect call or just to filter out the redirecting url into a javascript variable ?

Thanks in advance for taking some time to read this

I would recommend JQuery library for easy AJAX calls. With JQuery, you just select the element (Let say a Div) and clear it. then AJAXically get the content of page you want to redirect using load() function. Read the $.post, ajax() and load as well as selectors

0

Hi hielo & evstevemd,

Thanks a lot for your replies :).

@hielo:
Actually I am having a huge number of PHP files that should be accessed using Ajax. So I have to change the code of each an every PHP file to make it compatible with Ajax requests :(. I was wondering whether there is a solution without touching the PHP files :(. Anyway thanks a lot for taking some time to reply me.

@evstevemd:
I am using jQuery $.post to send Ajax requests. It works fine for non redirecting PHP scripts, but when PHP script calls the redirect function, it works in an unpredictable manner :(. May be I am using it incorrectly :/. I will go through the jQuery documentation and try it again. Thanks a lot for your reply.

If anyone knows how to handle it using jQuery $.post please be kind enough to share it with me :)

Thanks a lot

1
I am using jQuery $.post to send Ajax requests. It works fine for non redirecting PHP scripts,

There's your problem. From the jQuery manual

jQuery.post( url, [ data ], [ success(data, textStatus, XMLHttpRequest) ], [ dataType ] )

the third argument is a reference to a function which is to be called whenever the request completes "successfully". But this "successful" request equates to a http response status=200. A redirect gives you a 301 request, NOT 200. So you cannot use $.post(). Instead use $.ajax(). Currently you probably have something SIMILAR to:

$.post('page.php',params, function(){
  //here is whatever you are doing on a successful request
  ...
});

change that to:

$.post('page.php',params, function(){
  //here is whatever you are doing on a successful request
  ...
});
$.ajax({
  type: 'POST'
  ,url: 'page.php'
  ,data: params
  ,success: function(){
    //here is whatever you are doing on a successful request
    ...
  }
  ,complete: function(httpObj, textStatus){
     switch( 1*httpObj.status )
     {
          case 301: //here you do whatever you need to do
                    //when your php does a redirection
                    alert("Redirection");
                    break;

          case 404: //here you handle the calls to dead pages
                    alert("Page Not Found");
                    break;
     }
  }
});
0

Hi hielo & evstevemd,

Thanks a lot for your replies :).

@hielo:
Actually I am having a huge number of PHP files that should be accessed using Ajax. So I have to change the code of each an every PHP file to make it compatible with Ajax requests :(. I was wondering whether there is a solution without touching the PHP files :(. Anyway thanks a lot for taking some time to reply me.

@evstevemd:
I am using jQuery $.post to send Ajax requests. It works fine for non redirecting PHP scripts, but when PHP script calls the redirect function, it works in an unpredictable manner :(. May be I am using it incorrectly :/. I will go through the jQuery documentation and try it again. Thanks a lot for your reply.

If anyone knows how to handle it using jQuery $.post please be kind enough to share it with me :)

Thanks a lot

Hielo have said it well, but I would avoid that code complexity by removing the redirection instruction in my php and just *load* the php file to be redirected to in post callback function; roughly something like below. But for that, you have to "touch" PHP files. If you don't want to touch them, then check Hielo's method. Again, we don't know what the pages does exactly but it is worth to check JQuery append if page.php and redirect.php are to *not* overwrite the content of a div

$.post('page.php',params, function(){ 
   	$('#yourDisplayDiv').load('redirect.php');
  
      });

Edited by Stefano Mtangoo: n/a

0

Hi hielo & evstevemd,

Wow..!! I didn't knew taht $.ajax has those attributes..!!!:-O Great..!!! I will definitely try it..!!! :)

Thanks a lot. I really appreciate the help you gave me..!!!
Thanks a again..!!!


Cheers
Niranga

0

Hi hielo & evstevemd,

Wow..!! I didn't knew taht $.ajax has those attributes..!!!:-O Great..!!! I will definitely try it..!!! :)

Thanks a lot. I really appreciate the help you gave me..!!!
Thanks a again..!!!


Cheers
Niranga

Glad I was of help,
I will be happy to see you resolve your issues.
Welcome!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.