44 users online (0 members and 44 guests)  


  Results 1 to 6 of 6

Related

  1. Passing PHP variable to Javascript    Forum: PHP Forum
    Replies: 5
  2. convert a perl variable to javascript variable    Forum: Javascript Forum
    Replies: 0
  3. insert vbscript into perl variable    Forum: HTML Forum
    Replies: 0
  4. Parsing javascript variable to PHP    Forum: PHP Forum
    Replies: 0
  1. #1
    wafasn's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2006
    Posts
    1

    Assign a javascript variable to a perl variable

    Hi Filks,
    I really need help for this:
    I have a <select > element, I need to pass the value of the seleceted element to a perl varaible.
    I mean when the user selects an element from the list, I want to assign that element to a perl variable.
    Thanks in advance.

  2. #2
    uthus's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Feb 2006
    Location
    Ft Hood
    Posts
    75

    Re: Assign a javascript variable to a perl variable

    Quote Originally Posted by wafasn
    Hi Filks,
    I really need help for this:
    I have a <select > element, I need to pass the value of the seleceted element to a perl varaible.
    I mean when the user selects an element from the list, I want to assign that element to a perl variable.
    Thanks in advance.
    In the html somewhere I'm assuming you have something like
    Code:
    <form action="/cgi-bin/whatever.pl" ...>
    The perl would look something like this:
    Code:
    #!/usr/bin/perl
    # scriptname.pl
    #
    
    &getFormInput;
    ...
    #do some stuff
    ...
    sub getFormInput {
      (*fval) = @_ if @_;
      local ($buf);
      if ($ENV{'REQUEST_METHOD'} eq 'POST') {
        read (STDIN, $buf, $ENV{'CONTENT_LENGTH'});
      } else {
        $buf = $ENV{'QUERY_STRING'};
      }
      if $buf eq "") {
        return 0;
      } else {
        @fval = split(/&/, $buf);
        foreach $i (0 .. $#fval) {
          ($name, $val) = split (/=/,$fval[$i],2);
          $val =~ tr/+/ /;
          $val =~ s/%(..)/pack("C",hex($1))/ge;
          $name =~ tr/+/ /;
          $name =~ s/%(..)/pack("C",hex($1)/ge;
    
          if (!defined($field{$name})) {
            $field{$name} = $val;
          } else {
            $field{$name} .= ",$val";
          }
        }
      }
      return 1;
    }
    This is the code snippet that I alway use when passing form data. It returns the select variable's name and data. It works, as far as I know, with all input types as long as they have a name.

    hth,

    Uthus

  3. #3
    sbryce's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2006
    Posts
    3

    Re: Assign a javascript variable to a perl variable

    This is much cleaner!

    ------------------------------------

    use strict;
    use warnings;
    use CGI;

    Code:
    my $query = new CGI;
     
    my $selection = $query->param('selection');
     
    print "Content-Type: text/html; charset=iso-8859-1\n\n";
     
    if ($selection)
    {
    print "<html><body><p>Selection = $selection</p></body></html>";
    }
    else
    {
    print "<html><body><p>Nothing Selected.</p></body></html>";
    }
    ----------

    OK, can someone explain how to maintain indentation in this forum?
    Last edited by HTML; 02-14-2006 at 05:12 PM.

  4. #4
    netroact's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2006
    Posts
    19

    Re: Assign a javascript variable to a perl variable

    Just in case the original poster does not realize this. It doesn't matter if it's a Javascript form you pass the field to the script with or not. Perl will parse all the fields from the form into variables by using the method above.

  5. #5
    uthus's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Feb 2006
    Location
    Ft Hood
    Posts
    75

    Re: Assign a javascript variable to a perl variable

    Quote Originally Posted by sbryce
    This is much cleaner!

    ------------------------------------

    use strict;
    use warnings;
    use CGI;

    my $query = new CGI;

    my $selection = $query->param('selection');

    print "Content-Type: text/html; charset=iso-8859-1\n\n";

    if ($selection)
    {
    print "<html><body><p>Selection = $selection</p></body></html>";
    }
    else
    {
    print "<html><body><p>Nothing Selected.</p></body></html>";
    }


    ----------

    OK, can someone explain how to maintain indentation in this forum?
    yep. yours looks better than mine. i just took mine from Perl Cookbook (i think).

    indenting? try the space bar. there may be some other way of doing it, but you know from my script that i do things the hard way.

    u

  6. #6
    HTML's Avatar
    Administrator

    Status
    Offline
    Join Date
    Aug 2000
    Posts
    3,445

    Follow HTML On Twitter Add HTML on Facebook Add HTML on Google+ Add HTML on Linkedin Visit HTML's Youtube Channel

    Re: Assign a javascript variable to a perl variable

    Quote Originally Posted by sbryce
    OK, can someone explain how to maintain indentation in this forum?
    Hello sbryce, when making the post there are 3 buttons which will help maintain the format
    http://www.ahfb2000.com/webmaster_he...ditor/code.gif http://www.ahfb2000.com/webmaster_he...ditor/html.gifhttp://www.ahfb2000.com/webmaster_he...editor/php.gif

    Hope this helps.

    Dave
    AHFBWEB Less customers per server, more power for you!

    Business Class Shared Hosting



Tags for this Thread