On Thursday 04 Feb 2010 14:09:11 salmia wrote:
> hi all,
> i'm newbie in perl.i have face problem how to add function ajax
> (onchange) on my file.
> 

Hi salmia!

Have you visited http://perl-begin.org/ yet and read a good introductory book 
or tutorial? Your code has many anti-patterns.

> this is my code...
> 
> 
> $ajx_ctrl         = $query->param('ajx_ctrl');
> $co_id        = $query -> param('select_co');

You should add "use strict;" and "use warnings;" at the top of your code, and 
fix all relevant errors and warnings. Also, please don't use too terse 
abbreviations like "ajx" instead of AJAX. I also think you should call 
$ajax_ctrl "$ajax_control_value".

> 
> my $exported_fx = sub {

Why are you declaring a subroutine like that as a closure assigned to a "my" 
variable? A plain "sub exported_fx {" would be better.

>       my $searchterm = shift;
>       my $sql = qq< select file_name from file_name where id_module = ? >;
>       my $dbh_label = $dbh_label;

What is the intention of the above line?

>       my $sth = $dbh_label->prepare( $sql );
>       $sth->execute( $searchterm . '%' );

Do you realise that doing «id_module = "mysearch%"» in the SQL won't actually 
do a search? You need LIKE. And beware of SQL wildcards injection:

http://phroggy.com/weblog/63.html

Also it is customary to highlight the SQL keywords using "ALL-CAPS".

> 
>       my $html1 = "";
>       $html1 = qq!<select name="file_name" id="id_module" $input_style >
>       #OnChange="details( ['users'],['ddiv'] ); return true;">\n!;

1. Use here-documents for multi-line strings:

http://www.shlomifish.org/lecture/Perl/Newbies/lecture4/string-
forms/here_doc.html

(short URL - http://xrl.us/bgukac ).

2. make sure $input_style does not have special HTML characters, or escape it.

> 
>       my $firstrow = $sth->fetch();
>       if ( defined $firstrow ) {
>               $html1 .= qq!<option >! .qq!- Select One -! . qq!</option>\n!;

1. This should be one big string.

2. Please consider using a templating system such as:

http://template-toolkit.org/

>               $html1.= qq!<option>! . $firstrow->[0] . qq!</option>\n!;
> 

Now you have a potential cross-site scripting vulnerability here:

http://en.wikipedia.org/wiki/Cross-site_scripting

>               # dot on each option from the db
>               while ( my $row = $sth->fetch() ) {
>                       $html1.= qq!<option>! . $row->[0] . qq!</option>\n!;
>               }
> 
>       }
>       else
>       {$html1 .= qq!<option >! .qq!- Select One -! . qq!</option>\n!;}
> 
>       # close off the select and return
>       $html1 .= qq!</select>\n!;
>       #&common_error("$html1");

Don't use ampersand-subroutine.

>       return($html1);
> };
> 
> 
> 
> my $dbh_label = $dbh_label;

Again - why?

>       $file_base .=qq~ <select name="file_name" $input_style>
>       <option>- Select One -</option>
>       </select>
>       ~;

use here-docs.

> 
> my $exported_form = sub {
>       my $searchterm2 = shift;
>       my $sql2 = qq< select form_action from form where id_file = ? >;
>       my $dbh_label = $dbh_label;
>       my $sth2 = $dbh_label->prepare( $sql2 );
>       $sth2->execute( $searchterm2 . '%' );
> 
>       my $html2 = "";
>       $html2 = qq!<select name="form_action" id="id_file" $input_style >
>       #OnChange="details( ['users'],['ddiv'] ); return true;">\n!;
> 
>       my $firstrow2 = $sth2->fetch();
>       if ( defined $firstrow2 ) {
>               $html2 .= qq!<option >! .qq!- Select One -! . qq!</option>\n!;
>               $html2.= qq!<option>! . $firstrow2->[0] . qq!</option>\n!;
> 
>               # dot on each option from the db
>               while ( my $row = $sth2->fetch() ) {

Is there such a method of $sth called ->fetch()? You should be more explicit - 
fetchrow_arrayref , etc.

>                       $html2.= qq!<option>! . $row->[0] . qq!</option>\n!;
>               }
> 
>       }
>       else
>       {$html2 .= qq!<option >! .qq!- Select One -! . qq!</option>\n!;}
> 
>       # close off the select and return
>       $html2 .= qq!</select>\n!;
>       return($html2);
> };
> 

This seems nearly identical to the previous function - please extract the 
common functionality to a subroutine.

Regards

>  my $dbh_label = $dbh_label;
>       $form_base .=qq~ <select name="form_name" $input_style>
>       <option>- Select One -</option>
>       </select>
>       ~;
> 
>       #--- get module ---#
>       $sql = qq~
>       SELECT id, module
>       FROM   module
>       ~;
>       $sth = $dbh_label->prepare($sql);
>       $sth->execute() or &common_error("SQL","Couldn't execute statement :
> $DBI::errstr",1);
>       $sel_module .=qq~
>       <option value="">- Select One -</option>
>       ~;
>       while (($id, $module) = $sth->fetchrow_array())
>       {
> 
>       $sel_module .=qq~
>       <option value="$id" >$module</option>
>       ~;
>       }
>       $sth->finish();
>       #----------------------#
> 
> 
> 
> 
> 
> ### start add company form ##
> my $Show_Form = sub {
> 
> my $html = "";

Please use proper indentation inside of subroutines.

> $html .= <<EOT;

Make sure here-docs are either «<<"EOT"» or «<<'EOT'» and not just <<EOT - see 
Perl Best Practices.

Please fix all the problems I've mentioned in all of your code.

Regards,

        Shlomi Fish

> 
> my $pjx = CGI::Ajax->new(
>                                       search  => $exported_fx,
>                                       search2  => $exported_form,
> 
> );
> ---------------------------------------------------------------------------
> -------------------------------------------------------------------------
> my problem is i want to create 3 dropdown list but 2nd list is based on
> 1st list and the 3rd list is based in 2nd list. if we not choose anything
> only word -select one- is appear..plz help me...thanks..

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What does "Zionism" mean? - http://shlom.in/def-zionism

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to