Forum Moderators: coopster & phranque

Message Too Old, No Replies

Script not passing variables to 2nd subroutine

         

typomaniac

10:51 pm on Mar 14, 2024 (gmt 0)

10+ Year Member Top Contributors Of The Month



Using MySQL to populate the form. It loads data one dabase table row per <tr> row. Please don't mind the crude use of the html table (I'll add the CSS later). So, when I open the script it shows all the rows of the table but, If I select one and it goes
to edit_members_2 it only displays data from the first db table row regardless of the one I select in the form. I also tried using a <select> to put the SQL data in the form rather than the way I did this one and added a submit button but the results were
the same. All the link info, subroutine names and variable names etc is purely ficticious (given other names). The reason for the same db table in part two--update with the info in part 1.

my $query;
my $sth;
my $ref;
my $query=new CGI;
my $sub_selecter;
my $id_number=$query->param("id_number");
my $name=$query->param("name");
my $dbh = DBI->connect('connection parameters') or die("Couldn't connect");
#sub re-direct station I have quite a few subroutines in the main file.
if($sub_selecter==1){&edit_something_2($id_number, $name);exit;}

sub edit_something{
print"<h1 align=\"center\">Select a Name to Edit $user_id</h1>";
&htm_header;
print" <table align=\"center\"border=\"2\"width=\"30%\">\n";
print" <form id=\"EP\"method=\"post\"action=\"$ENV{'SCRIPT_NAME'}\">\n";
print" <input type=\"hidden\"name=\"sub_selecter\"value=1>\n";
$sth=$dbh->prepare("SELECT * FROM table_1");
$sth->execute();
while($ref=$sth->fetchrow_hashref()){
print" <tr>\n";
print" <td width=\"100%\"><h2>\n";
print" <input type=\"hidden\"name=\"id_number\"value=\"$ref->{'id_number'}\">\n";
print" <input type=\"hidden\"name=\"name\"value=\"$ref->{'name'}\">\n";
print" <a href=\"javascript:{}\" onclick=\"document.getElementById('EP').submit(); return false;\">$ref->{'name'}</a>\n";
print" </td></h2>\n";
print" </tr>\n";}
print" </form>\n";
print" </table>\n";
}



sub edit_something_2{

print"<h1>Part Two $_[0] $_[1]</h1>\n";
my $dbh = DBI->connect('connection parameters where id_number=$_[0]') or die("Couldn't connect");
$sth=$dbh->prepare("SELECT * FROM table_2");
$sth->execute();
while($ref=$sth->fetchrow_hashref()){
print"$_[0] $_[1]";

}
}

explorador

6:43 pm on May 16, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi, did you solve it?

A few observations here.

When you send parameters/data to a perl function/routine, like you did on your code, you have to receive it and parse it using shift, this will put you on the right course of searching documentation. Otherwise, you can use the variables, as long as the data is shared, the problem I see is you are using "my" to define the variables, this makes them local, not global.

As for the rest, you can use this:

print "abc abc
abc abc
abc
abc \n
abc \n
";

instead of using an individual print for each line, this will help you to have a readable code. Hope it helps.

typomaniac

12:40 am on May 18, 2024 (gmt 0)

10+ Year Member Top Contributors Of The Month



I got this working but not in a way most would have probably done it. It demonstrates pretty much how I'm self taught and never really had to work with arrays/hashes. I used the file writing option (open(file,">solution.pl"), I'm preparing to make an emergency trip overseas for a month but when I get back I'll try to explain the method to my madness (might be useful or at the least a good laugh) but it works. Thanks for responding. I had pretty much forgot I even posted this until I saw the email. I used the same process for approving user input before going onto a prayer board about 15 years ago and the same code still runs quick and flawlessly.