Gossamer Forum
Home : Products : DBMan : Customization :

Passing values in the URL

(Page 2 of 2)
> >
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Oh, did you change $in{my} to $data{my} in those examples I gave?
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
yep :)



There is something strange going on, cant figure out what. Cause, I manage to split the values up properly, but cant get them to function...
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Ok,

Could you try getting rid of the local $, bit and change the split part to split by \0 rather than |

If that doesn't work I'd need to make some changes to the get_data routine as I'm not sure it will allow you to do this.

Last edited by:

RedRum: Feb 8, 2002, 4:25 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Thanks

@fields = split(\0, $data{'mine'}); ?
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Yeah, I think that should work.
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Didnt work. I rolled back to my previous solution:

In the form:

<input type=checkbox name=my value="$id,">sdafsdfsdf

Splitting up:

@fields = split(/\,/, $data{'my'});

Checking against db file:

for ($count=0;$fields[$count]>0 ;++$count) {
if ($fields[$count] eq $id) {
push (@result_list, $line)
}
}





This results in the passed values being split correctly into $fields[x] - but only the first $field[0] to be recognised in the [for] loop. Even thought I can see the other matches being correct...

I think this must be a prob with the handling code, ie:

sub get_data {
#######################################################################################
local($string);
# get data
if ($ENV{'REQUEST_METHOD'} eq 'GET') {
$string = $ENV{'QUERY_STRING'};
}
else { read(STDIN, $string, $ENV{'CONTENT_LENGTH'}); }
# split data into name=value pairs
@data = split(/&/, $string);
# split into name=value pairs in associative array
foreach (@data) {
split(/=/, $_);
$_[0] =~ s/\+/ /g; # plus to space
$_[0] =~ s/%0D%0A%0D%0A/\n\n/g; #added by kristina make newlines?
$_[0] =~ s/%0a/newline/g;
$_[0] =~ s/\%00//g;
$_[0] =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
if(defined($data{$_[0]})){
$data{$_[0]} .= "\0";
$data{$_[0]} .= "$_[1]";
}
else {
$data{"$_[0]"} = $_[1];
}
}
# translate special characters
foreach (keys %data) {
$data{"$_"} =~ s/\+/ /g; # plus to space
$data{"$_"} =~ s/%(..)/pack("c", hex($1))/ge; # hex to alphanumeric
$data{"$_"} =~ s/\%00//g;
}
%data; # return associative array of name=value
#######################################################################################
}

Last edited by:

eric74: Feb 8, 2002, 5:25 AM
Quote Reply
Re: [eric74] Passing values in the URL In reply to
:(

Here's what I use in my scripts to grab form input if it is any help:

Code:
sub get_data {
#----------------------------------------------------------
# Return a hashref of all params and their values.

my ($INPUT) = {};
my (@INPUT) = ();

for (param()) {
@INPUT = param($_);

# Decide whether it is a single value or multiple.
if (@INPUT > 1) {
@{$INPUT->{$_}} = @INPUT
}
else {
$INPUT->{$_} = param($_)
}
}

return $INPUT;

}

I changed the sub name to get_data incase you want to try it.

Basically it detects whether the input is one value or multiple for each field and handles it appropriately.

So for your $data{my} field it would automatically put the data into an array ready for use.

The only thing you'd need to do to use it would be to add:

use CGI qw(:standard);

...to the top of the cgi script. The original code uses objects but I removed that so you don't need to bother creating one.

So basically you'd do:

my $input = get_data();

...and then hopefully you'll have access to the my field using @{$input->{my}}

Last edited by:

RedRum: Feb 8, 2002, 5:57 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Thanks!

So:

@{$input->{whateverfieldname}}


would be the same as writing:

$data{'whateverfieldname'}

?
Quote Reply
Re: [eric74] Passing values in the URL In reply to
If the field has multiple values then they are accessible using

@{$input->{fielname}}

...or ${$input->{fieldname}}[x] ...for individual fields where x is the element.

...otherwise it will be just $input->{fieldname} for single values.

You may need to have a play to see if/how it works by just printing values.

Just submit your form and on the success page add:

print join("<BR>", @{$input->{my}});

...and see if it print all the values you selected.

Last edited by:

RedRum: Feb 8, 2002, 6:14 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
works brilliantly. I can extract the values correctly.... but .... the below still doesnt work... :( even if I can see that

${$input->{my}}[0];
${$input->{my}}[1];
${$input->{my}}[2]; - are correct




for ($count=0;$fields[$count]>0 ;++$count) {
$this=${$input->{my}}[$count];
if ($this eq $id) {
push...
}

}
Quote Reply
Re: [eric74] Passing values in the URL In reply to
>>
for ($count=0;$fields[$count]>0 ;++$count) {
$this=${$input->{my}}[$count];
if ($this eq $id) {
push...
}
<<

I wouldn't use that. You need to do a while loop through the file like I gave above otherwise you are looping through the input rather than the file and so you can only do as many loops as there are elements in the array which won't give the correct matches.

Last edited by:

RedRum: Feb 8, 2002, 8:36 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
sorry, I have "foreach line in the file" before that. so it does the for loop I just send for each line in the file
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Hmm...

How about...
Code:
open FH, "bla.....".....
while (<FH>) {
if (/^(\d+)/ and (grep { /^\Q$1\E$/ } @{$input->{my}})) {
push @result_list, $_;
}
}
close FH;

Im sure that should work Unsure

Last edited by:

RedRum: Feb 8, 2002, 8:47 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
U BET IT DID!!

Thanks a lot for all this, and for baring with me...

Have a nice weekend!
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Phew...I was about to start plucking hairs from my skull.
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Again, thanks a lot for all the help! I have another question I know you could answer:

I want to pick out the second value in every line of the .db file ("|" separeted), whats the smoothest way (tried quite a few and messed up...)?



Thanks tons!
Quote Reply
Re: [eric74] Passing values in the URL In reply to
/^\d+\|([^|]+)/ and my $second_field = $1;


Thats one way at least :)

Let me know if it works.

Last edited by:

RedRum: Feb 22, 2002, 10:35 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Didnt totally get that... Crazy



I use this:

open (DB, "<...");
flock(DB, 1);
@lines = <DB>;
close DB;
foreach $line (@lines) {
next if ($line =~ /^$/);
next if ($line =~ /^#/);

klsksksksjsjjhd

}



Should I put that reg expr in that loop?

Thanks!
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Yep, under the next ...'s

Then $second_field will store the value for each loop.
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
now I just put:

/^\d+\|([^|]+)/ and my $second_field = $1;

exactly as is within the loop... is that correct or am I being stupid? (that line seem a bit not complete to me..)



Thanks
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Yes that is correct, assuming I understood what you wanted?

The regex will pick out the second field from every line of the file and store it in $second_field.

If you want all second fields to be put in an array or something you'd need:

/^\d+\|([^|]+)/ and push @array, $1;
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Got it now.. Works perfectly!! Where do I define what column I want to grab? ie 2nd for the above code.

Thanks a bunch! Smile
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Hmm if you want to define them you'd be better splitting each line...

split /\|/;
push @array, $_[x];


....where x is the field less 1. So if you wanted to print field 2, you'd use:

push @array, $_[1];
> >