CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> Tips & Solutions

 PHP Tip: Redirecting to a "previous" page.

Print topic Send  topic

Author Message
lvalverdeb

Posts: 299
Posted: 10/12/2004, 10:02 AM

In a project I am currently working on, I have several forms with grid lists that need to point to a single maintenance form. Basically that meant that I had to dynamically modify the $Redirect global variable in the maintenance form to return to the appropriate calling form.

I utilised the following 4-function solution and one session variable. These are the steps to implement this functionality;

1) Copy functions below to Common.php or a function library included in your project.

2) Set the "previous" or calling page. Call the following function in the AfterInitialize event of the calling page.

setAsPrevPage();


2) In the Unload Event (at least that what works for me) of the maintenance page (or whatever page you are calling), use the following function:

   
  
redirectToPrevPage();  
  

3) On some forms I needed to add this same functionality to a "Return to Prior Page" link. In the AfterInitialize event of the "called" form:

   
global $YourForm;  
setLinkToPrevPage($Path->To->Your->Return->Link);  
e.g. setLinkToPrevPage($Link1);  
        or setLinkToPrevPage($YourForm->Link1);  
  

And that is all there is to it.

I realise similar behaviour can be implemented using JavaScript but I prefer a PHP solution.

All the best

Luis

Here are the required functions:

function setAsPrevPage($PageName="")  
{  
	if (!strlen($PageName))  
		$PageName = getenv("SCRIPT_NAME");  
	$QueryString = CCGetQueryString("QueryString","");  
	if ($QueryString !== "")  
		$PageName.="?".$QueryString;  
	CCSetSession("sPrevPage",$PageName);  
}  
  
function redirectToPrevPage()	{  
	global $Redirect;  
	$Redirect = getPrevPage();		  
}   
  
function getPrevPage()  
{  
	$QueryString = CCGetQueryString("QueryString", "");  
	$retPage = CCGetSession("sPrevPage","");  
	return $retPage;  
}  
  
function setLinkToPrevPage(&$lnkRef)  
{  
	$prevPage = getPrevPage();  
	if ($prevPage == null)  
		$prevPage = getenv("HTTP_REFERER");  
	$lnkRef->SetLink($prevPage);  
	return $lnkRef;  
}  

_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4
View profile  Send private message
SteveG

Posts: 56
Posted: 02/06/2005, 9:40 PM

Thanks for the great help. I tried to do the redirecting the CCS way through passing/removing parameters; but after a few days of frustration, I'm using your session variables way. Just to add my own techniques to this post, I've got three forms on my page, Add_Record, Edit_Record and Delete_Record; of which I hide two. Each form has a cancel button, which calls my Redirect_to_Previous_Page() function as does the AfterExecuteUpdate and the AfterExecuteInsert. The AfterExecuteDelete needs a special condition, as I didn't want to go back to the page that called it if there was nothing left to show:

global $Record_Delete;
// If coming from the one-up page of this discussion, don't return to empty grid, but go to another page:
if (0 < strpos(Get_Previous_Page(), '&subjtype=2')) { // Indicates page I don't want to go back to.
global $Redirect;
$Redirect = 'Discussions.php?subjtype=1&areaid='.$Record_Delete->areaid->GetValue(); // Calls another page using hidden field values.
} else {
Redirect_to_Previous_Page();
}

... by the way, I took out the second " $QueryString = CCGetQueryString("QueryString", "");" line in your getPrevPage function, I wasn't sure that it needs to be there, and so far the function still works well.

- Steve
View profile  Send private message
SteveG

Posts: 56
Posted: 02/06/2005, 9:55 PM

Oh yeah, I forgot, here's the code I use when I add a new record (why return to previous page when there's a new record to mess around with):

global $Redirect;
$Redirect = 'Discussions.php?subjtype=2&discussionid='.mysql_insert_id();
View profile  Send private message
lvalverdeb

Posts: 299
Posted: 02/16/2005, 11:09 AM

Cheers Steve. I am glad the post gave you some pointers. Also, you are right about the second $QueryString = CCGetQueryString("QueryString", ""); in the getPrevPage() function. It is not needed at all. Hence, the function would just be:
  
function getPrevPage()  {    
 return CCGetSession("sPrevPage","");    
}    

Luis
_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.