473,699 Members | 2,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

submitting a variable to a subroutine

76 New Member
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 2184
numberwhun
3,509 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Contributor
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 Recognized Expert New Member
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_NAM E} 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 Recognized Expert Contributor
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 New Member
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 Recognized Expert Specialist
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 New Member
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 New Member
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
1191
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 everything has passed it is true, but when it gets passed to the calc function it turns to false on the DimblnAnswer as Boolean = ValidateEntries() although the subprocedure is true. We bypassed the assignment and ran the code with just the...
10
2248
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 multiplyer as integer, ByVal variable as ___?) variable = 5 * multiplyer end function What I would like this function to do is take the name of the incoming variable and assign a calculated value to it. Any help would be greatly appreciated, TIA!!
3
35506
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 table and field names and controls are all named correctly, and the control referred to in the errant code is open, and it has data in it. Private Sub cmdAddIngredientToRecipe_Click() ' Get RecipeID for future action query Dim recipeID As Long
2
1573
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 refers to the variable location, I would have expected the textbox1.text display to update right when the value change when NAME is set to "Samantha" but the textbox1.text field does not reflect the change until the subroutine completes.
5
5852
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
2915
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 submit the page, I want to check some business rules, and based on the response from the database, if the response is Yes, I need to display a confirmation alert to user. When the user clicks ok, then I should take an action, if not, exit out of the...
3
2306
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 open (Printer223.xls). Basically http://localhost/printers/whatever I put in the textbox.xls is what I'm after. Thanks.
3
4314
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 variable in that subroutine, but not have to worry about it when an event triggers it again. If I just go: Public variable As String .. it will give an error. I need to check if the variable exists... can that actually be done? I've yet to see...
2
2628
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 report: Private Sub Report_Open(Cancel As Integer) 'Prompts for business unit when run educational assistance reports. Dim strBusUnitName As String strBusUnitName = UCase(InputBox("Enter Business Unit or click OK for all.", "Business Unit",...
0
8692
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9040
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8924
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8889
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2353
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2012
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.