CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
2 Forms on 1 page - session issue?

 



hybride
New User

Jul 26, 2009, 6:36 PM

Post #1 of 3 (520 views)
2 Forms on 1 page - session issue? Can't Post

Hello everyone,

This is my first post here, and I have been scratching my head over this for the past few weeks. I am currently teaching myself Perl and Prolog at the same time, and the code am working on requires both.

What it's supposed to do is in the first query (the input), it checks whether or not the query exists in the file (that works). If it does, it continues on (works); if it does not, the second form with the yes/no radio buttons comes up (that also works). However, after pressing the radio button submit, the original query is lost, so somewhere between submit 1 and submit 2, the original query disappears. I have tried using sessions, but I only figured out how to make it on static variables, not with the dynamic param. I know this form requires sessions, but I have no idea how to do it with the second form.

Any help would be greatly appreciated. This is the code:

Code
#!/usr/bin/perl 

use strict;
use warnings;
use AI::Prolog;
use AI::Prolog::Engine;
use AI::Prolog::Term;
use CGI;
use CGI qw(:standard);
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session;
use Data::Dumper;
use Tie::File;
use Template; # for producing output.

my $q = new CGI; #query, handle common gateway interface
my $pq = CGI->new(); #query spiderform
my $session = new CGI::Session();
my $CGISESSID = $session->id();

# These are the Prolog files, creating new prolog ruleset
my $file = "ancestry.pl";
open(PROLOGFILE, $file ) or die "$! \n";
local $/;
my $prologRules = <PROLOGFILE>;

my $prolog = AI::Prolog->new( $prologRules );

print "Content-type: text/html\n\n";

firstQuery();
my $firstq = $q->param('query1');

#$session->save_param('que',$q->param('query1'));
$session->save_param($q, ["query1","query2"]);

my $que = $session->param('query1');
my $que2 = $session->param('query2');
#$session->param('query1');
#$session->param(-name=>'l_name', -value=>$firstq);

if(param()) {
my $t = $session->param("query1", $q->param("query1"));


secondQuery();
if($q->param('submit2')) {
print $que2;
print 'es';

}
}

sub firstQuery {

print <<PROG;

<form method="post" name="prol1">
<input type="text" name="query1" maxlength="150">
<input type="submit" value="submit" name="submit1">
<input type="reset" value="erase">
</form>

PROG
}

sub secondQuery {

print <<PROG;

<form method="post" name="prol2">
<input type="radio" name="query2" value="yes">Yes
<input type="radio" name="query2" value="no">No<br />
<input type="submit" value="submit" name="submit2">
<input type="reset" value="erase">
</form>

PROG
}



FishMonger
Veteran / Moderator

Jul 26, 2009, 8:51 PM

Post #2 of 3 (516 views)
Re: [hybride] 2 Forms on 1 page - session issue? [In reply to] Can't Post


Quote
my $q = new CGI; #query, handle common gateway interface
my $pq = CGI->new(); #query spiderform


Why are you creating 2 cgi objects?

You only need 1 and the second form of creating the object (i.ie, my $pq = CGI->new;) is preferred.

Have you tried associating the cgi object with the session object?
I think it is supposed to be done by default, but maybe it isn't.

Code
$session = CGI::Session->new( $pg );

You may need to use load instead of new when creating the session object.


hybride
New User

Jul 27, 2009, 8:03 PM

Post #3 of 3 (509 views)
Re: [FishMonger] 2 Forms on 1 page - session issue? [In reply to] Can't Post

Thank you for your help! I've cleaned up the file quite a bit, and was wondering if you can take a look at it again? It's still the same problem, session loading. Here is a sample of the code live: http://cabal.anekyu.com/loadanc2.pl if you wanted to test it out.

Code
#!/usr/bin/perl 


use strict;
use warnings;
use AI::Prolog;
use AI::Prolog::Engine;
use AI::Prolog::Term;
use CGI;
use CGI qw(:standard);
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session;
use Data::Dumper;
use Tie::File;
use Template; # for producing output.

my $q = CGI->new(); #query, handle common gateway interface
my $session = CGI::Session->load($q) or die CGI::Session->errstr;

# These are the Prolog files, creating new prolog ruleset
my $file = "ancestry.pl";
open(PROLOGFILE, $file ) or die "$! \n";
local $/;
my $prologRules = <PROLOGFILE>;

my $prolog = AI::Prolog->new( $prologRules );

print "Content-type: text/html\n\n";

firstQuery();
my $first = $q->param('query1');
$session->param("query1",$first);
my $sessfir = $session->param('query1');
if($q->param('submit1')) {
print $sessfir;
print $first;
secondQuery();

if($q->param('submit2')) {
my $q2= $q->param('query2');
print $sessfir;
print '<BR><BR>SEC: $q2';
}

}

sub firstQuery {

print <<PROG;

<form method="post" name="prol1">
<input type="text" name="query1" maxlength="150">
<input type="submit" value="submit" name="submit1">
<input type="reset" value="erase">
</form>

PROG
}

sub secondQuery {

print <<PROG;

<form method="post" name="prol2">
<input type="radio" name="query2" value="yes">Yes
<input type="radio" name="query2" value="no">No<br />
<input type="submit" value="submit" name="submit2">
<input type="reset" value="erase">
</form>

PROG
}


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives