Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Perl in HTML

by Appy16 (Sexton)
on Apr 15, 2010 at 07:27 UTC ( [id://834837]=perlquestion: print w/replies, xml ) Need Help??

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

I have a project in which i have an html page.There I have a Submit Button using which i have to call a perl subroutine, carry out some function and return the value. The problem is that i am not able to pass the value to the subroutine. Also is there a way to use $_Get or $_Post i n perl??

I am posting my script below

#!/usr/bin/perl -w use CGI::Ajax; use CGI; sub functions { my $Fn = $_Get['Function']; my $input = shift; if(1) { return ("True"); } if(0) { return ("False"); } } sub Show_HTML { my $html = <<EOT; <HTML> <HEAD><title>Perl and HTML</title> </HEAD> <BODY> <script type="text/javascript"> function CheckIsNumeric() { var AsciiCode = event.keyCode; if ((AsciiCode < 48) || (AsciiCode > 57)) { alert('Please enter only numbers.'); event.cancelBubble = true; event.returnValue = false; } } </script> <Form method="Get"> <select name="Function"> <option value="0" selected>(please select:)</option> <option value="1">Func1</option> <option value="2">Func2</option> </select> &nbsp; &nbsp; Enter a number:&nbsp; <input type="text" name="Number" id="myText" size="6"onkeypress =" +CheckIsNumeric()"/> &nbsp; &nbsp; Enter a function:&nbsp; <input type="text" name="fns" id="URI" size="25"> <input type='Submit' onsubmit="functions('fns');" value='Submit' / +> </Form> <hr> <div id="resultdiv"> </div> <br> </BODY> </HTML> EOT return $html; } my $cgi = new CGI(); my $pjx = new CGI::Ajax( 'functions' => \&functions ); print $pjx->build_html($cgi,\&Show_HTML);
what i want to do is pass the fns's value to the subroutine and return a value.

Kindly Help. Thanx in advance.

Replies are listed 'Best First'.
Re: Perl in HTML
by moritz (Cardinal) on Apr 15, 2010 at 08:03 UTC
    See CGI: you use $cgi->param('name') and $cgi->url_param('name') for obtaining POST and GET values.
    Perl 6 - links to (nearly) everything that is Perl 6.

      op, are you aware that javascript is not perl? Do you know what language the onsubmit event handler comes from? You seem to have skimmed over a few steps in your web programming education:

      1) Your html is atrocious, so you need to read a modern text book on proper html.

      2) You don't have the skill yet to include any javascript anywhere on your html pages. So for the next year, forget you ever heard the word javascript. If you want to learn server side programming, then concentrate on that. If you want to learn client side programming, then concentrate on javascript. But whatever you do, don't try to learn server side programming and javascript at the same time.

      3) You don't call perl scripts with javascript onsubmit event handlers.

      4) For practice, you need to create a basic html page with one form field and a submit button. When the user clicks on the submit button, the form should request your perl script. Your perl script should return an html page that says, "You entered: ...." How is that done? Read five beginning CGI tutorials, and then come back if you still have any questions.

Re: Perl in HTML
by almut (Canon) on Apr 15, 2010 at 08:34 UTC

    According to CGI::Ajax, the JS-side syntax for calling the routine seems to always be something like

    function(['source', ...], ['dest', ...]);

    where 'source' and 'dest' are the IDs of the respective HTML elements where the arguments to the function are read from, and where its result is written to.

Re: Perl in HTML
by Corion (Patriarch) on Apr 15, 2010 at 08:04 UTC

    I think you are translating PHP to Perl. @_Get and @_Post likely are the parameters passed with the request, so the Perl equivalent would be $cgi->param().

    Also, you should use strict; at the top of your program. This forces you to predeclare all variables you need and will prevent you from misspelling variables or using variables that don't exist, like @_Get.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-26 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found