473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

submitting a variable to a subroutine

76
hi all,
i've been trying, almost for the whole day, to submit the value of a variable in my perl script (eg, $sequence) to a subroutine in the same script when i click on a submit button. how can i do that? is there any other way execute a second perl script with a click of a button on the first one, submitting the same values of variables in first one????
your help would be greatly appreciated.
Oct 12 '07 #1
9 2159
numberwhun
3,509 Expert Mod 2GB
hi all,
i've been trying, almost for the whole day, to submit the value of a variable in my perl script (eg, $sequence) to a subroutine in the same script when i click on a submit button. how can i do that? is there any other way execute a second perl script with a click of a button on the first one, submitting the same values of variables in first one????
your help would be greatly appreciated.
If we are to help you, we will definitely need to see the code that you have been working with. Please be sure and surround the code with the proper [code] [/code] tags when posting it.

Regards,

Jeff
Oct 12 '07 #2
idorjee
76
Thanks for reminding me of the CODE tags, Jeff. I always forget about that. Sorry, if I've caused you trouble with that in the past.
OK, so here is what I want my code to do. The following code which is a part of my perl script displays the info as an html table, and that part is fine.

Expand|Select|Wrap|Line Numbers
  1. print '<TABLE border="1" cellspacing="0" cellpadding="3" width="80%">' . "\n";
  2. print "<tr><th width='15%'>Entry one</th><td>" . $one . "</td></tr>\n"; 
  3. print "<tr><th>Entry two</th><td>" . $two . "</td></tr>\n";
  4. print "<tr><th>Entry three</th><td>" . $three . "</td></tr>\n";
  5. print "</TABLE>\n";
  6. print "<br>";
  7.  
  8. print '<form name="myForm" method="post">'."\n";
  9. print '<input type="hidden" name="insert1" value="$one">'."\n";
  10. print '<input type="hidden" name="insert2" value="$two">'."\n";
  11. print '<input type="hidden" name="insert3" value="$three">'."\n";
  12. print '<input type="submit" name="submit_myForm" value="Submit">'."\n";
  13. print '</form>'."\n"; 
  14.  
But, now when I click on the Submit button, I want the following part of the script to be executed.

Expand|Select|Wrap|Line Numbers
  1. my $dbh = DBI->connect ("DBI:mysql:database:123.456.789.1","user","pass") or die "Error: $DBI::errstr\n";
  2. my $sql = "INSERT INTO protein (first, second, third) VALUES ('$one', '$two', '$three')";
  3. my $in = $dbh->prepare($sql);
  4. $in->execute; 
  5.  
How can I do that? I know it sounds easy, and may be it is, but I am so dump that I can't figure it out. Please, help.

Thanks a lot.
Oct 12 '07 #3
eWish
971 Expert 512MB
Assuming that you are using the CGI module. You can do it like so.

Expand|Select|Wrap|Line Numbers
  1. my $q = CGI->new;
  2. my $from_data_name = $q->param('field_name_here');
Then use it later in your script use that variable where you want. But there are some security issues here, When ever you allow the user to submit data you always need to check the data to make sure that it is what you are escpecting.
Expand|Select|Wrap|Line Numbers
  1. sub process_form (
  2.  
  3. my $not_valid = 0;
  4.  
  5. #Check the incoming data to make sure it is safe to use.  If not display the form again.    
  6.     unless ($q->param('field_name_here')  =~ /^(?:([\w\s+\d\']+))$/) {
  7.                 $not_valid = 1;
  8.     }
  9.     #Check to see if the form needs to be displayed again.    
  10.     if ($not_valid) { &display_form(); } 
  11.     else { &next_sub(); }
  12.  
  13. }
  14.  
  15. sub next_sub (
  16.  
  17. my ($value1, $value2, $value3) = @_;
  18.  
  19. my $insert = $dbh->prepare('INSERT INTO protein (first, second, third) VALUES (?,?,?);
  20. my $insert->execute($value1,$value2,$value3);
  21.  
  22. #rest of code here......
  23.  
  24. )
Use placeholders for your data when inserting and selecting from the database. It is safer and will cause you less headaches later.
Oct 13 '07 #4
Kelicula
176 Expert 100+
Assuming that you are using the CGI module. You can do it like so.

Expand|Select|Wrap|Line Numbers
  1. my $q = CGI->new;
  2. my $from_data_name = $q->param('field_name_here');
Then use it later in your script use that variable where you want. But there are some security issues here, When ever you allow the user to submit data you always need to check the data to make sure that it is what you are escpecting.
Expand|Select|Wrap|Line Numbers
  1. sub process_form (
  2.  
  3. my $not_valid = 0;
  4.  
  5. #Check the incoming data to make sure it is safe to use.  If not display the form again.    
  6.     unless ($q->param('field_name_here')  =~ /^(?:([\w\s+\d\']+))$/) {
  7.                 $not_valid = 1;
  8.     }
  9.     #Check to see if the form needs to be displayed again.    
  10.     if ($not_valid) { &display_form(); } 
  11.     else { &next_sub(); }
  12.  
  13. }
  14.  
  15. sub next_sub (
  16.  
  17. my ($value1, $value2, $value3) = @_;
  18.  
  19. my $insert = $dbh->prepare('INSERT INTO protein (first, second, third) VALUES (?,?,?);
  20. my $insert->execute($value1,$value2,$value3);
  21.  
  22. #rest of code here......
  23.  
  24. )
Use placeholders for your data when inserting and selecting from the database. It is safer and will cause you less headaches later.

You can run anything you want after form submission by including a if(param()) clause.
When the page first loads, the param is "undefined" and will return false.
Once the form is submitted it will return true, and the code will be executed.

Also a very good way to check that input submitted by users is safe is by using the "-T" switch. "taint mode". It's not fail safe, but it's easy, and a lot better than nothing. The main problem is that you don't have as much control over what happens after an erroneous entry is made, for that reason eWish's code is useful. But just in general it's good for stopping bad entries, what happens next is ??....


example:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -T
  2.  
  3. use strict;
  4. use CGI qw(:standard);
  5.  
  6. print header;
  7. print start_html();
  8.  
  9. my $one = "hey";
  10. my $two = "you";
  11. my $three = "there";
  12.  
  13.  
  14. print start_form(-enctype=>"application/x-www-form-urlencoded"),
  15.       textfield(-name=>'insert1', value=>$one),
  16.       textfield(-name=>'insert2', value=>$two),
  17.       hidden('insert3',$three), # if you want hidden, they default to name, value.
  18.       submit();
  19.  
  20.  
  21. if(param()){
  22.  
  23. my $on = param('insert1'); # remember to grab values from param
  24. my $tw = param('insert2');
  25. my $th = param('insert3');
  26.  
  27. # It's not necessary to create unique scalars, but I play it safe.
  28.  
  29. #do whatever with $on, $tw and $th...
  30. # they will contain what the user or script inserted into
  31. # the form inputs named accordingly. (see the param calls)
  32.  
  33. }
  34.  
You see, by not specifying, perl inserts the current %ENV{SCRIPT_NAME} into the action value in the form tag. The script will call on itself to process the input.

This does work.
See HERE

PS- It's looks like you are need of a here document also.
Instead of repeating so many lines of
print
print
print
etc...

You can create an area where perl will interpret as a separate field.
Most symbols that need to be escaped are ignored by perl and can be used, like
%, and " , or -, and =.
example:
Expand|Select|Wrap|Line Numbers
  1. print <<EOH;
  2.  
  3. you can write regular html code in here, without much consideration for perl constraints. Perl variables however, will be interpolated to their values.
  4.  
  5. EOH
  6.  
It's very useful.
You can also assign it to a variable:

Expand|Select|Wrap|Line Numbers
  1.  my $stuff = <<EOF;
  2.  
  3. write stuff here...
  4.  
  5.  
  6. EOF
  7.  
Now all that stuff will be stored in $stuff.

Notice that there is NOT a semi-colon after the last identifier.

<<EOF;

than

EOF

Anyway have fun!!
Oct 13 '07 #5
eWish
971 Expert 512MB
PS- It's looks like you are need of a here document also.
Instead of repeating so many lines of
print
print
print
etc...
You can also use the qq operator.

Expand|Select|Wrap|Line Numbers
  1. print qq~
  2. <table border="1" cellspacing="0" cellpadding="3" width="80%">
  3.   <tr> 
  4.     <th width='15%'>Entry one</th>
  5.     <td>$one</td>
  6.   </tr>
  7.   <tr>
  8.     <th>Entry two</th>
  9.     <td>$two</td>
  10.   </tr>
  11.   <tr>
  12.     <th>Entry three</th>
  13.     <td>$three</td>
  14.   </tr>
  15. </table>
  16. <br>
  17. <form name="myForm" method="post">
  18.   <input type="hidden" name="insert12" value="$one">
  19.   <input type="hidden" name="insert22" value="$two">
  20.   <input type="hidden" name="insert32" value="$three">
  21.   <input type="submit" name="submit_myForm2" value="Submit">
  22. </form>
  23. ~;
But I like keeping my HMTL seperate from my code. To me it makes it cleaner.

HTML::Template
Oct 13 '07 #6
idorjee
76
Thanks a lot eWish and Kelicula. I'm still a bit unsure as to how I should write it all. My initial input comes from an html form, which submits it to the perl script that I mentioned earlier. Here is how it actually looks like:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use CGI;
  4. use Bio::DB::SwissProt;
  5. my $database = new Bio::DB::SwissProt;
  6. my $cgi = new CGI;
  7. print $cgi->header() .
  8. $cgi->start_html(-title => 'Results', -bgcolor => '#6699CC').
  9. $cgi->h1('Results from the DB') . "\n";
  10.  
  11. my @params = $cgi->param();
  12. foreach my $parameter (@params) {
  13.     my $value=$cgi->param($parameter);
  14.     my @values=split(/\s+/,$value);
  15.     foreach my $acc (@values){
  16.         chomp($acc);
  17.  
  18. # gets info. using the $acc with BioPerl module....
  19. my $one = $seq->seq();
  20. #and so on ...
  21.  
  22. #the variables are then displayed as follows 
  23. print '<TABLE border="1" cellspacing="0" cellpadding="3" width="80%">' . "\n";
  24. print "<tr><th width='15%'>Entry one</th><td>" . $one . "</td></tr>\n";
  25. print "<tr><th>Entry two</th><td>" . $two . "</td></tr>\n";
  26. print "<tr><th>Entry three</th><td>" . $three . "</td></tr>\n";
  27. print "</TABLE>\n";
  28. print "<br>";
  29. print '<form name="myForm" method="post">'."\n";
  30. print '<input type="hidden" name="insert1" value="$one">'."\n";
  31. print '<input type="hidden" name="insert2" value="$two">'."\n";
  32. print '<input type="hidden" name="insert3" value="$three">'."\n";
  33. print '<input type="submit" name="submit_myForm" value="Submit">'."\n";
  34. print '</form>'."\n"; 
  35.  
Now with the the submit I wanted to insert the same displayed variables that is in the table ($one, $two and $three) to the MySQL db.

Cheers!!!
Oct 13 '07 #7
KevinADC
4,059 Expert 2GB
regardless of other problems you are having, this will not work:

Expand|Select|Wrap|Line Numbers
  1. print '<input type="hidden" name="insert1" value="$one">'."\n";
You have a scalar inside a single-quoted string construct: $one.
$one will not be expanded, it will be treated as the literal string $one.

You have to use a double-quoted string or a quote operator that interpolates scalars and arrays:

Expand|Select|Wrap|Line Numbers
  1. print "this is \"$foo\"";
  2. print qq{this is "$foo"};
  3.  
qq escapes the double-quotes for you making it very convenient to use. And if you use paired brackets as the delimiters, like {} or (), the qq operator keeps track of them so you don't have to escape them if you use them in the string:

Expand|Select|Wrap|Line Numbers
  1. $foo = 'a test';
  2. print qq{this {is} "$foo"};
  3.  
but if you use a symbol, like '~' or '!' as the delimiter you do have to escape it:

Expand|Select|Wrap|Line Numbers
  1. $foo = 'a test';
  2. print qq~this \~is\~ "$foo"~;
  3.  
Oct 13 '07 #8
idorjee
76
hi KevinADC,
you were absolutely correct. i was overlooking the "single-quoted" thing. i think it should work now. let's see.
thanks a lot.
cheers!!!
Oct 13 '07 #9
idorjee
76
it works fine now. thanks to all you guys.
^ ^*
Oct 13 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tracer | last post by:
I have a variable named blnAnswer that is a flag in a subprocedure called ValidateEntries() set to true and tripped to false if something fails. When the variable comes out of the subprocedure and...
10
by: Blaxer | last post by:
There is probably a really easy way to do this, so please forgive me but I would like to set the value of a variable from a variable, an example would be... function Calculate_Something(ByVal...
3
by: Richard Hollenbeck | last post by:
I've marked the line in this subroutine where I've been getting this error. It may be something stupid but I've been staring at this error trying to fix it for over an hour. I'm pretty sure the...
2
by: Tom_Slycke | last post by:
In a VB.Net class and have pointed out a discrepancy to my teacher. Can anyone here answer this? Very simple program used to demonstrate the "ByRef" parameter passing. If the ByRef truly...
5
by: gabba | last post by:
Hi, is it possible to call a subroutine (or a function) using variable name? Sub a() Response.write("sub a") End sub Sub b() Response.write("sub b") End sub
2
by: shil | last post by:
Hi all, I am using javascript confirm() function to get user's confirmation from codebehind after a condition is met. I don't want to attach the function to the button on page load. When I...
3
by: Looch | last post by:
Something simple here (hopefully)...how can I append the text in a text box to a hyperlink? For example, I enter the string 'Printer223'. There is an Excel file with the same name that I want to...
3
by: zensunni | last post by:
In a perfect world, I wouldn't have to do this, but I'm at the mercy of event scripting for a program. there's an event that runs every so often that triggers an subroutine. I'd like to declare a...
2
by: jim190 | last post by:
I am relatively new to using VBA and am have been trying for two days to pass a user entered value to my report for filtering when opened. I have the following subroutine in the open event of my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.