Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 59
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help with Perl Syntax PLEASEE

    Hi guys I have a lab to complete where I need to be able to access, pull and add data from a database. I wrote this scrip in perl but I dont know how to call functions using a button or an anchor tag for example. The mySQL statements work fine as I have tested them from command line. Can someone please tell me what the proper syntax is? as you can see I the only thing we have learned is how to use param(). Im refereing to the proper syntax in the startHtml() =>script and the buttons in the subHtmlForm().. I also dont know if its legal that im using cgi.pm and regular xhtml at the same time.. dont think it is. PLease helpP.. thanks in advanced. that is when i press the button once the page is generated nothing happens!!


    Code:
    #!/usr/bin/perl
    use strict; use warnings;
    use CGI qw/:all/;
    use CGI::Pretty;
    use CGI::Carp "fatalsToBrowser";
    use DBI;
    my $serverName=;
    my $serverPort=;
    my $serverUser=;
    my $serverPass=;
    my $serverDb=;
    my $serverTabl=;
    
    if(!param()){
      htmlForm();
    }else{
      my $error=insert_entry();
      show_entries($error);
    }
    
    sub htmlForm{
      print header();
      print start_html(-title=>'Lab 6',-scrip=>{-language=>'PerlScript', src=>'mySqlProg.cgi'});
      print h1("Enter data in to mysql table(lab 6)");
      print startform({-action=>"",-method=>"POST"}); 
      print table(TR(td("First Name:"),td(textfield({-name=>"fname",-size=>"11",-maxlength=>"10"}))),
                  TR(td("Last Name:"),td(textfield({-name=>"lname",-size=>"11",-maxlength=>"10"}))),
                  TR(td("Student ID:"),td(textfield({-name=>"id",-size=>"10",-maxlength=>"9"}))),
                  TR(td("Logins:"),td(textfield({-name=>"logins",-size=>"4",-maxlength=>"3"}))));
      print submit(-name=>'submit_form', -value=>'Submit form');
      print end_form();
      print "<button type='button' value='view' onclick='show_entries();'>Show Table Values</button>";
      print "<button type='button' value='increment' onclick='increment();'>Increment logins by 1</button>";
      print "<button type='button' value='change' onclick='change();'>Any field, change the # 6 to 4</button>";
      print end_html();
    }
    
    sub insert_entry {
        my ($dbh,$sth,$Fname,$Lname,$studentID,$Logins,$success);                      
        $dbh=DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass)
            or die ("Cant connect to the database: ". DBI->errstr);
        $Fname=param("fname");
        $Lname=param("lname");
        $studentID=param("id");
        $Logins=param("logins");
        $success=$dbh->do("INSERT INTO $serverTabl(firstname,lastname,SID,logins) VALUES(?,?,?,?)", 
                 undef,$Fname,$Lname,$studentID,$Logins);    
        $dbh->disconnect;
        if($success != 1){return "Sorry, the database was unable to add your entry.";}
    }
    
    sub show_entries {
        my($dbh, $sth, @row);
        $dbh=DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
        $sth=$dbh->prepare("SELECT firstname,lastname,SID,logins FROM $serverTabl");
        $sth->execute;
        print header();
        print start_html(-title=>'Lab 6');
        if($_){print "$_";}
        print "Existing Entries",hr;
        while(@row=$sth->fetchrow_array){
              print "First Name: ", $row[0], br;
              print "Last Name: ", $row[1], br;
              print "Student ID: ", $row[2], br;
              print "Logins: ", $row[3], br,br;
        }
        print end_html();
        $sth->finish;
        $dbh->disconnect;
    }
    
    sub increment{
       my($dbh);
       $dbh=DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
       $dbh->do("UPDATE $serverTabl SET logins=logins+1");
       $dbh->disconnect;
    }
    
    sub change{
       my($dbh, $sth, @row);
       $dbh=DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
       $sth=$dbh->prepare("SELECT firstname,lastname,SID,logins FROM $serverTabl");
       while(@row=$sth->fetchrow_array){
          for(my $i=0;$i<=3;$i++){
            if($row[$i]){
              $row[$i]=~s/6/4/g;
            }
          }
          $dbh->do("UPDATE $serverTabl SET firstname=$row[0],lastname=$row[1],SID=$row[2],logins=$row[3]
                   WHERE firstame=$row[0]");
       }
       $sth->finish;
       $dbh->disconnect; 
    }
    Last edited by hackYourBrain; 07-29-2009 at 04:18 PM.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    The buttons need to be inside the form definition and their type needs to be 'submit' and you should also set the name attribute. Then you can use the param() method to retrieve the value and call the proper subroutine.

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok thanks for the reply.. I need to be able to call my subs from another sub..as you can see from my cgi.. and I need to be able to call them from outside the form definition..they are not part of the form.. for example if I want to call a sub from an anchor tag in one of my other subs How do I do that??

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    If you're referring to the sub being called in the onclick handler, then you need to ask your question in the JavaScript area.

    If you want to execute a Perl script/subroutine without a form submit button, then you need to ask your question in the Ajax area.

  • #5
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Fish, it looks like he is using PerlScript, which I know next to nothing about myself.

    print start_html(-title=>'Lab 6',-scrip=>{-language=>'PerlScript', src=>'mySqlProg.cgi'});


    but -scrip should be -script I would think.

    These lines look like syntax errors to me:

    my $serverName=;
    my $serverPort=;
    my $serverUser=;
    my $serverPass=;
    my $serverDb=;
    my $serverTabl=;


    should be:

    my $serverName;
    my $serverPort;
    my $serverUser;
    my $serverPass;
    my $serverDb;
    my $serverTabl;

    That at least makes the script run when fixed.

  • #6
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    He's using both. The posted script is Perl, and it loads an external PerlScript file, just like what would be done with an external javascript or css file.

    Based on how the question was posed, this issue does not fall under Perl. It should go to a forum dealing with Perlscript or Ajax, or JavaScript (all of which are client side scripting).

  • #7
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Quote Originally Posted by FishMonger View Post
    He's using both. The posted script is Perl, and it loads an external PerlScript file, just like what would be done with an external javascript or css file.

    Based on how the question was posed, this issue does not fall under Perl. It should go to a forum dealing with Perlscript or Ajax, or JavaScript (all of which are client side scripting).
    I owe you a margarita.

  • #8
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is perl scrip posted in the perl section.. if you dont know perl why are u even replying...

    These lines look like syntax errors to me:

    my $serverName=;
    my $serverPort=;
    my $serverUser=;
    my $serverPass=;
    my $serverDb=;
    my $serverTabl=;


    should be:

    my $serverName;
    my $serverPort;
    my $serverUser;
    my $serverPass;
    my $serverDb;
    my $serverTabl;
    no it shouldnt... was initializing those variables to their appropriate values.. I took the values out for obvious reasons.. [comment removed by FishMonger]

    and this scrip is not external.. its not called from some html page.. it generates them as you can clearly seeeee

    hollyyy
    Last edited by FishMonger; 07-30-2009 at 12:41 AM. Reason: Rude/insulting comment removed

  • #9
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    hehehe.... best laugh so far today, Thanks, good luck with your lab.

  • #10
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    hackYourBrain,

    The answers I gave are the proper solutions for the problem as you described it. If they aren't what you're looking for, then please provide more details of what you need.

    All parameters passed/submitted to the script can be accessed via CGI's param() method.

    Do you need help in the Perl logic required to take a parameter passed to the script and decide which subroutine to execute?

    If not, then please provide more details regarding the server side portions of what you need i.e., the Perl requirements, not the html requirements.

  • #11
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok guys thanks for your help I will take your advice and post in another section .. although I dont see an Ajax section...

    and sorry for not being civil.. just a bit frustrated with my Prof.. danm dude wont help me. didnt mean to take it out on you.

    Daneil

  • #12
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,916
    Thanks
    2
    Thanked 164 Times in 159 Posts
    You can post ajax questions here: http://codingforums.com/ajax-design/

  • #13
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok since I am having trouble getting some help with this..and I mean some serious trouble.. guess perl is not as popular as I thought.. my Prof wont help me.. tutor couldnt help me.. you guys cant help me.. ok ok sorry there I go again.. it looks like I will have to do it myself I will post my progress here.. so maybe some of you will learn something you can use.. my lab is to code a hockey or soccer or nfl online pool.. with some specs..part of which are to use a database to store and retrieve data.. and use linux commands and regular espressions to work with the system and info.. should be fun considering Im on my own.. but here is what I Have soo far.. My main coner with asking the previouse questions was that I didnt understand what the mechanics were for moving around with perl cgi.. by moving around I mean running webpages base on input ofcourse.. but since we are to use a form for everything It looks like I can do whatever I need with param()... so here it goes.. Will keep you updated..ohh and before anyone asks.. the variables near the top are not initialized for security reasons.. if you want to use this script.. then you will have to plug in your own values.... and dont tell me the script dont work.. of course it dont its under development.. I will say when its completely done.. annd I wil not comment the code in till Im finished.. cause Im on a tight deadline so I cant do it til the end.. cheers

    Code:
    #!/usr/bin/perl
    use strict; 
    use warnings;
    use CGI qw/:all/;
    use CGI::Pretty;
    use CGI::Carp "fatalsToBrowser";
    use DBI;
    #use LWP::TreeBuilder;
    #use LWP::FormatText;
    use LWP::Simple;
    
    my $serverName="";
    my $serverPort="";
    my $serverUser="";
    my $serverPass="";
    my $serverDb="";
    my $serverTabl="mysql";
    my($checkBoxValue,$flag,$randomNum,$accountExists,$specialError,$CSV);
    my(@errors,@formValues,@accountsDir);
    
    if(!param()){
       firstPage();
    }
    if(param('firstPage') eq "2"){
    #   do{
    #     $flag=0;
    #     regForm(@formValues,@errors,$specialError);
          regForm();
    #     if(param()){
    #          $formValues[0]=param("fname");
    #          $formValues[1]=param("lname");
    #          $formValues[2]=param("email");
    #          @errors=validate(@formValues);
    #          if($errors[0]ne""||$errors[1]ne""||$errors[2]ne""||$errors[3]ne""){
    #             $flag=1;
    #          }else{
    #             $randomNum=int(rand(100000));
    #             @accountsDir=`ls -l ../accounts`;
    #             foreach(@accountsDir){
    #               if($_=~/$formValues[1]$randomNum/){
    #                 $specialError=2;
    #                 $accountExists=1;
    #                 $flag=1;
    #               }
    #             }
    #             if($accountExists!=1){
    #               system("touch ../accounts/$formValues[1]$randomNum.txt");
    #               $"=",";
    #               $CSV="@formValues,$randomNum";
    #               open MYFILE, ">$formValues[1]$randomNum.txt" or die "There was a problem opening the file\n";
    #               print MYFILE "$CSV";
    #               close MYFILE;
    #             }
    #          }
    #     }else{
    #          $specialError=1;
    #          $flag=1;
    #     }
    #   }while($flag==1);      
    }
    if(param('firstPage') eq "1"){
          print header();
          print start_html();
          print "this will be the sign in page";
          print end_html();
    }
    
    
    ####################################################################
    #mirror($URL, $local_filename); #will be using this to mirror scoreboard
    #system(mkdir $folder); #syntax for running linux commands from perl cgi
    #my randomNum=rand(999); #should be a number between 0 and 998, right?
    #$content = get("http://www.sn.no/"); #uses LWP::Simple; #not sure about this still have to check it
    ######################################################################
    
    
    sub validate{
      my @errors=("","","","");
       if(!($_[0]=~/^[a-z]{2,}[ ]*[a-z]$/i)){
          $errors[0]="Only alphabetic characters, minimum two characters long";}
       if($_[1]!~/^[a-z]{2,}[ ]*[a-z]$/i){
          $errors[1]="Only alphabetic characters, minimum two characters long";}
       if($_[2]!~/^[a-z:.~?=+&]{1,}@[a-z.]{1,}$/i){   #:/.~?=+&@ 
          $errors[2]="E-mail field only allows the folloing characters: :/.~?=+&@";}
       if($_[2]=~/\.\./){
          $errors[3]="Two consecutive periods are not allowed!";}
       return @errors;
    }
    
    
    
    sub regForm{
        startHTML();
        print h1("Registration Form, fill out the stuff!");
        if($_[7]==1){
          print span({-style=>'Color: red;'},h3('You did not enter anything! Try again.'));
          $_[7]="";
        }elsif($_[7]==2){
          print span({-style=>'Color: red;'},
                h3('Its a miracle! your randomly generated Id number and last name already exist! try again.'));
          $_[7]="";
        }
        print startform({-action=>"",-method=>"POST"});
        print table(TR(td("First Name:"),td(textfield({-name=>"fname",-size=>"12",-maxlength=>"12",-value=>"$_[0]"})),td($_[3])),
                    TR(td("Last Name:"),td(textfield({-name=>"lname",-size=>"12",-maxlength=>"12",-value=>"$_[1]"})),td($_[4])),
                    TR(td("E-Mail:"),td(textfield({-name=>"email",-size=>"24",-maxlength=>"24",-value=>"$_[2]"})),td($_[5],$_[6])));
        print submit(-name=>'Submit', -value=>'Submit');
        print end_form();
        endHTML();
    }
    
    sub firstPage{
      my %labels = ('1'=>'I am a member. Sign In.','2'=>'Register');
      startHTML();
      print h1('Welcome to the Assign2 NFL Pool');
      print startform({-action=>"",-method=>"POST"});
      print checkbox_group(-name=>'firstPage',-values=>['1','2'],-labels=> \%labels,-linebreak=>'true');
      print submit(-name=>'submit', -value=>'Submit');
      print end_form();
      endHTML();
    }
    
    sub endHTML{
      print end_html();
    }
    
    sub startHTML{
      print header();
      print start_html(-title=>'Assign2 Daniel Nowocien');
    }


    I
    Last edited by hackYourBrain; 08-03-2009 at 05:57 AM.

  • #14
    New Coder
    Join Date
    Jul 2009
    Posts
    43
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry also I will be using server side includes.. anyone tell me if they are interested in comments regarind security considerations? its one of my other chapters.

    Me

  • #15
    Senior Coder
    Join Date
    Mar 2006
    Posts
    1,274
    Thanks
    2
    Thanked 39 Times in 38 Posts
    Your script doesn't work and I think those variables need to be initialized.


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •