Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

HTML/CGI for loop problem

by Jabox (Sexton)
on Jul 14, 2014 at 15:38 UTC ( [id://1093568]=perlquestion: print w/replies, xml ) Need Help??

Jabox has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!

Back again with another problem I'm stuck on.. I couldn't find my answer or know how to narrow down my search on google to find my answer so I'm asking here again.

So I've had some help with using ajax in a .cgi code which was what I needed. But the original code was an <input> type and I wanted it to be a drop down list.

The problem is that I do not know how to squeeze the commented out for into the html where the dropdown list would be.

Here's the code

my $cgi = new CGI; my $pjx = new CGI::Ajax( 'exported_func' => \&perl_func ); my $dirname = ''; opendir my($dh), $dirname or die "Couldn't open dir'$dirname':$!"; my @files = readdir $dh; my $files; closedir $dh; #for my $i ( 2 .. $#files ) { # print "<option value=\"$files[$i]\">$files[$i]</option>\n"; # }; sub perl_func { my $input = shift; my $output = $input . "was the input!"; return ($output); } print $pjx->build_html( $cgi, \&Show_HTML); sub Show_HTML { my $html = <<__EOI__; <!DOCTYPE html> <html> <head> <script type="text/javascript" src="\jquery.js"></scri +pt> <script type="text/javascript" src="script.js"></scrip +t> <script type="text/javascript" src="stats.js"></script +> <title> Graph </title> </head> <body> <form action=''> <select name="users" id="users" onChange="expo +rted_func( ['users'], ['files'] );"> </select> </form> <div id="files"></div> </body> </html> __EOI__ return $html;

Thanks in advance!

Replies are listed 'Best First'.
Re: HTML/CGI for loop problem
by blue_cowdawg (Monsignor) on Jul 14, 2014 at 15:51 UTC

    There are multiple ways you can do this. You might want to investigate using HTML::Template and friends instead of embedding your HTML inside your code. If you still want to embed your HTML inside your code one way to embed your for loop is to use two print statements instead of one. The first prints everything up to and just shy of what the for loop is doing and the other everything after the for loop.

    sub buildHTML { my $html = <<!firsthalf something something !firsthalf foreach my $iter(@whatever) { $html = sprintf "%s %s",$html, $something else; } my $html2 =<<!secondhalf; other stuff !secnodhalf return $html . $html2 }


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: HTML/CGI for loop problem
by Anonymous Monk on Jul 14, 2014 at 19:32 UTC

    ... I'm stuck ...

    Write more functions, determine the common parts and turn them into functions, then turn the non-common parts into functions , so you have small functions you can understand

    #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); sub Show_Red_HTML { my( $query ) = @_; my $dirname = '.'; my $js_includes = js_includes(); my $css_includes = stylesheet_includes(); my $title = "This is the redness"; my $drops = red_dropdown_files( $dirname ); my $body = red_form_html( $query, $drops ); return Show_HTML( "$css_includes $js_includes", $title, $body ); } ## end sub Show_Red_HTML sub Show_HTML { my( $head, $title, $body ) = @_; my $header = common_body_header_html(); my $footer = common_body_footer_html(); return qq{<!DOCTYPE html> <html> <head> <title> $title </title> $head </head> <body> $header $body $footer </body> </html>}; } ## end sub Show_HTML sub red_dropdown_files { my( $dirname ) = @_; use Path::Tiny qw/ path cwd /; use CGI qw/ escapeHTML /; my @files = path( $dirname )->children; my $ret = ""; for my $file ( @files ) { $ret .= sprintf qq{<option value="%s">%s</option>\n}, escapeHTML( $file ), escapeHTML( $file ); } return $ret; } ## end sub red_dropdown_files

    Why write like this? Its easier to test/debug each small part seperately , for example

    sub Main { Tests(); # RealProgram(); } ## end sub Main sub Tests { require Test::More; Test::More->import( 'no_plan' ); ## and ok()... test_drops(); } ## end sub Tests sub test_drops { chdir 'testdir'; my $drops = red_dropdown_files( '.' ); ok( int $drops =~ m{\Q<option value="chapter-8.pdf">\E}, 'drops wo +rks' ); } ## end sub test_drops

    Its important not to forget to escapeHTML

Re: HTML/CGI for loop problem
by perlfan (Parson) on Jul 15, 2014 at 15:45 UTC
    Please, for the sake of sweet Baby Jesus, use a templating system like Template.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1093568]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-10-03 13:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (42 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.