Solved

Holding data between form posts

Posted on 2004-10-07
5
162 Views
Last Modified: 2013-12-25
Hey guys,

I have a page that contains a number of dropdowns and text boxes
Two of the dropdowns interact with each other i.e. Dropdown one controls the content of dropdown two. [This content is read from a Sybase database]

I have done this using javascript and here is a code snippet

#CGI bit
my $co = new CGI;
my $currenturl = $co->url;
..
$co->popup_menu( -name=>'project',
                 -default=>'$project',
                 -size=>1,
                 -width=>8,
                 -values=>['', 1,2,3],
                 -onchange=>"GetSubComponents('$currenturl',this.value);",                
                 ),

#javascript bit
function GetSubComponents(url,projectid){
var urlTo;
if(projectid < 1)
{
     urlTo = url;
}
else
{
     urlTo = url + "?ProjectID="+projectid;
}
window.location.href = urlTo;
}

when the page is reloaded it checks the ProjectID parameter and changes the content of the second dropdown.

this stuff works fine the only problem is I need to keep the data that was on the page prior to the page being redirected.
How do I do this?

Vinnie
0
Comment
Question by:VincentLawlor
5 Comments
 
LVL 1

Accepted Solution

by:
Levan_Kiladze earned 500 total points
ID: 12257164
you can do it using cookies..
in CGI bit edit this way:
#CGI bit
my $co = new CGI;
my $currenturl = $co->url;
..
$co->popup_menu( -name=>'project',
                 -default=>'$project',
                 -size=>1,
                 -width=>8,
                 -values=>['', 1,2,3],
                 -onchange=>"GetSubComponents('$currenturl',this.value); SaveData()",                
                 ),


now in JAVASCRIPT bit add function like this:
//----------------------------------------------------------
function SaveData()
{
       SetCookie(document.forms[0].project,document.forms[0].project.selectedIndex,1);
}


function SetCookie(cookieName,cookieValue,nDays)
 {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function GetCookie(cookiename)

 {
 var cookiestring=""+document.cookie;
 var index1=cookiestring.indexOf(cookiename);
 if (index1==-1 || cookiename=="") return "";
 var index2=cookiestring.indexOf(';',index1);
 if (index2==-1) index2=cookiestring.length;
 return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}



function getvalue()
{
      if(GetCookie(document.forms[0].project) != "")
     {
         document.forms[0].project.selectedIndex = GetCookie(document.forms[0].project);
     }

}

// ----------------------------------------------------


finally, add onload event in <body> TAG..
<body onload=getvalue()>







the second way of solving your problem is using hidden fields in your form, which stores selected index.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 12312862
> .. I need to keep the data that was on the page prior to the page being redirected.
which data?
wher do you need it?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now