Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help in debugging this CGI::Ajax code

Status
Not open for further replies.

sunw

Technical User
Sep 24, 2007
32
US
I wrote a small piece of perl code using CGI::Ajax.

Code:
#! /usr/bin/perl

use strict;
use CGI::Pretty qw( :html3 );
use CGI::Ajax;

local $| = 1;

my $q = new CGI::Pretty;
my $p = new CGI::Ajax('js_func' => \&pl_func);
my $flag = 1; [b]# if $flag != 1, then the Ajax codes would not work!![/b]
[COLOR=blue]
if($flag == 1) {
  if($q->param('switch') == 1) {
    print $q->redirect('[URL unfurl="true"]http://www.cnn.com');[/URL]
  }
  else {
    print $p->build_html($q, \&Show_HTML);
  }
}[/color][COLOR=red]
else {
  if($q->param('switch') == 1) {
    print $p->build_html($q, \&Show_HTML);
  }
  else {
    print $q->redirect('[URL unfurl="true"]http://www.cnn.com');[/URL]
  }
}[/color]

exit 0;

sub Show_HTML {
  my $form = &getFormInfo;
  my $html = <<HTML;
<html>
<body>
<form action="#" name="theFrom">
  $form
</form>
</body>
</html>
HTML
  return $html;
} 

sub pl_func {
  my $fn = shift;
  my $ln = shift;
  # In reality, will massage $fn & $ln based on some business rule
  return $fn.'-'.$ln;
}

sub getFormInfo {
  my $info = div(
    div($q->label('First Name: '),$q->input({-name=>'fname',-id=>'fnameID',-type=>'text'})),
    div($q->label('Last Name: '), $q->input({-name=>'lname',-id=>'lnameID',-type=>'text'})),
    div($q->label('Login ID: '), $q->input({-name=>'id',-id=>'idID',-type=>'text', -onfocus=>"js_func(['fnameID', 'lnameID'], ['idID']);"})),
    div($q->submit({-value=>"Submit"}), $q->reset({-value=>"Cancel"})));
  return $info;
}

I named the above code as mytest.pl. If I construct a url like this:


The blue section works, but the red section does not work. Could someone tell what I have missed here?

Thanks!
 
thats because of the if/else conditions. If flagg == 1 will execute the first "if" block only.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Hi KevinADC,

I have read through his script and I don't understand what you mean by only the first if block will be executed. Is it because $flag = 1; Therefore the red section will never have to be executed? Sorry

Chris
 
This is the essence of the problem:

Code:
$flag = 1;
$switch = 1;
if ($flag==1) {
   print "the 'if' condition is true";
}
else {
   if ($switch == 1) {
      print "the 'else' condition is true";
   }
}

the "if" block represents the blue code. It will always be true because $flag is assigned the static value of 1 (one). The "else" block will never be true so any code in that block will never be executed. It does not matter what the value of $switch is because the first "if" condition is determining the flow of the program.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I guess I did not make myself clear enough.

The code above only works when $flag == 1. If you change it to zero through hard code, in theory, it should still work, right? But, unfortunately, it would not work!!! That's why I said in bold font: if $flag != 1, then the Ajax codes would not work!!.

The reason I was using $flag in my code was trying to make you guys testing easier. All you need to do is set $flag = 0, then you'll see an odd behavior of CGI::Ajax.

My impression is that $p->build_html($q, \&Show_HTML) HAS TO be in the 'else' block. It does not make sense, I know.
 
Thanks Kevin

sunw, have you tried putting the red section in another if tag rather than an else? so you then have two ifs

if {
blue section
}
if {
red section
}
 
what does switch equal?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
To 1DMF,

$switch can be 1 or anything else.

To chrismassey,

Yes, I tried. As along as $p->build_html() is in 'if' block, then it would NOT work!!

=======================

And to all, if I confused you with my original question, I am sorry. I thught that $flag I used might have make your testing a bit easier.

Now, forget $flag, all I want to ask is this:

Code:
[COLOR=blue]
  if($q->param('switch') == 1) {
    print $q->redirect('[URL unfurl="true"]http://www.cnn.com');[/URL]
  }
  else {
    print $p->build_html($q, \&Show_HTML);
  }
[/color]

The above code works, because $p->build_html() is in 'else' block.

Code:
[COLOR=red]
  if($q->param('switch') == 1) {
    print $p->build_html($q, \&Show_HTML);
  }
  else {
    print $q->redirect('[URL unfurl="true"]http://www.cnn.com');[/URL]
  }
[/color]

The above code does not work, because it is in 'if' block. Isn't this strange?
 
it's only strange depending on what switch equals.

if for both blocks switch equals 1 , then you are not executing the same code for each block as you have swapped round which runs when switch equal 1.

if it works in the first (blue) code and switch equals 1 well then the redirect gets executed NOT the build_html.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
In red code, $switch == 1, $p->build_html() would NOT work.

In blue code, $switch == 0, $p->build_html() works just fine.

If you do a test run then you'll see what I mean.
 
seems a bit odd if that's the case, but if you need to swap the way it works, the easy solution would be make it
Code:
[b]if($q->param('switch') != 1) {[/b]
    print $q->redirect('[URL unfurl="true"]http://www.cnn.com');[/URL]
  }
  else {
    print $p->build_html($q, \&Show_HTML);
  }

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
I have a way to work around. But I just can not understand why it behaves so strangely!! I'd guess a cgi variable passed in from url could interfere CGI::Ajax? I hope some experts here could give me a clear explanation.
 
maybe you need to load the CGI module:

Code:
use CGI;

then:

Code:
my $q = CGI->new;
if($q->param('switch') == 1) {
   .......

You are using CGI::pretty and CGI::Ajax but in the code you posted you have not loaded CGI. Thats the module that will get the value of the switch paramater into your script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thank you, Kevin.

I tried your suggestion before and actually that was my another question. I thought once I had the question in my original post answered, I might be able to figure out how to fix the problem caused by 'use CGI;'.

Ok, here is the error I got while using CGI instead of CGI::pretty:

Problems
with the html-generating function sent to CGI::Ajax object


This error is actually from Ajax.pm..sub build_html.

 
please post you most current code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I have made a slight change in my code and this time, the error is more informative.

Here is the new code:

Code:
use strict;
use CGI::Pretty qw( :html3 );
use CGI::Ajax;

local $| = 1;

my $q = new CGI::Pretty;
my $p = new CGI::Ajax('js_func' => \&pl_func);

=[b]The following blue code works fine. It's listed here for reference only.[/b][COLOR=blue]
if($q->param('switch') == 1) {
	print $q->header;
	print "In if";
}
else {
	print $p->build_html($q, \&Show_HTML);
}[/color]
=cut
[COLOR=red]
if($q->param('switch') == 1) {
	print $p->build_html($q, \&Show_HTML);
}
else {
	print $q->header;
	[b]print "In else";[/b]
}
[/color]
exit 0;

sub Show_HTML {
# The implementation is the same as the one in my original post. 
}

sub pl_func {
	my $fn = shift;
	my $ln = shift;
	return $fn.'-'.$ln;
}

sub getFormInfo {
# The implementation is the same as the one in my original post. 
}

Again, assume the url is .

If you enter fn for first name and ln for last name. By design, once you put the cursor in 'Login ID' area, the value 'fn-ln' will be populated automatically by the Ajax code. And this is TRUE when the blue code is in place.

However, if the red code is in place, the Ajax code would put 'In else' in the 'Login ID' area!!
 
to Kevin,

Replace 'use CGI::pretty;' with 'use CGI;' and replace 'my $q = new CGI::pretty;' with 'my $q = new CGI;', you'll get this error:

Problems
with the html-generating function sent to CGI::Ajax object
 
add a print header line to the second "if" condition.

if($q->param('switch') == 1) {
print $q->header;
print $p->build_html($q, \&Show_HTML);
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I tried. Would not work, 'cause build_html() has already set the header. If you added 'print $q->header;', you'd see the following line on your page:

Content-Type: text/html; charset=ISO-8859-1

 
in the last code you posted you still have not loaded the CGI module:

use strict;
use CGI::pretty qw( :html3 );
use CGI::Ajax;

local $| = 1;

my $q = new CGI::pretty;
my $p = new CGI::Ajax('js_func' => \&pl_func);



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top