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!
 
That's because Ajax.pm would not work if CGI.pm was used. I told you this in my previous post.
 
what use is this CGI::Ajax ?

AJAX is Active JavaScript And XML , all calls are normally made from client side with JavaScript and the HTTP request.

Why on earth would you want to use a perl module to do this, isn't it better to have a JavaScript file for this?

Maybe i'm wrong, dunno, but I've written an entire site based on AJAX and none of it was done with perl or a perl module.

Just wondered what it did and how it helps?

"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!
 
That's because Ajax.pm would not work if CGI.pm was used. I told you this in my previous post.

Then you can't do it. You need to use the CGI module to import the switch paramater into your script.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
what use is this CGI::Ajax ?

See the module documentation.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Then you can't do it. You need to use the CGI module to import the switch paramater into your script.

Really? Did you mean CGI::pretty does not support importing parameters from url? I must have missed something.

Would you please explain a bit more, Kevin? Thank you.
 
As far as I know, CGI::pretty does not do anything except format html code. CGI imports the data/query string paramaters.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
As far as I know, CGI::pretty does not do anything except format html code. CGI imports the data/query string paramaters.

It seems to me what you said might not be true. My impression is that CGI::pretty can do everything that CGI.pm does, plus formating html tags.
 
I could be wrong. Run some tests.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I did. Many many times.

After so many discussions, now I tend to think this is a CGI::Ajax's bug. Is this a reasonable conclusion? Kevin?

Actually, I wrote an email to the author two days ago. But I have not heard anything from him, yet.

Thank you all very much for your inputs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top