Gossamer Forum
Home : Products : DBMan : Customization :

Passing values in the URL

(Page 1 of 2)
> >
Quote Reply
Passing values in the URL
Hi!

Im making a method=get form with checkboxes... the reason is that I want (!) the values to be visible in the URL..

Code:


<input type=checkbox name=my value=40 checked>

<input type=checkbox name=my value=50 checked>

<input type=checkbox name=my value=50 checked>


when submitted I do this in the .cgi:

Code:
$all = $in{'my'};
@fields = split(",", $all);


... to split them up into separete $fields[x]..

Now, this is all good and the $fields gets populated with the correct values. The problem is when I start comparing those $fields with other variables I have.. ie:

Code:
if ($fields[1] eq $banana) { jdjdjdjdjdjdjddj};


they never match - even though I can see that they both have the same value - it never executes (the program doesnt see them as the same).

Is this due to some weird encode formatting reason or what is going on?

Really appreciate any help on this. Thanks
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Question is: Where do you declare your @fields and where do you use $fields[1]? Might be a problem of variable scoping.
kellner
Quote Reply
Re: [kellner] Passing values in the URL In reply to
Do your values have spaces?

chmod
Quote Reply
Re: [chmod] Passing values in the URL In reply to
no, the values are all numeric [8282] [20290] [292] etc. and separeted by "," at the moment. ie <input type=checkbox name=my value="40," checked> <input type=checkbox name=my value="50," checked> etc

and the split up by:

$mys = $data{'my'};
@fields = split(",", $mys);



Thanks
Quote Reply
Re: [kellner] Passing values in the URL In reply to
Not totally sure what you mean. Where in the script I do this? I do it all within then same sub



thanks
Quote Reply
Re: [eric74] Passing values in the URL In reply to
OK. Next point: you seem to have several checkboxes with the same name "my". This means that when the values are passed on the script, you get one string for "my": "firstvalue\0secondvalue\0thirdvalue".

The subroutine parse_form should take care of splitting these on one particular character, so that "\0" is replaced with something else. I don't know how the standard parse_form in db.cgi does this; mine is already edited. Take a look at it - you may want to do something, by adding the commas at the end of your form input, that dbman already does. Maybe your problem comes from there? Just guessing ...
kellner
Quote Reply
Re: [kellner] Passing values in the URL In reply to
You can delimit values using $, ....eg if you have 5 checkboxes all called "my" with values 1 2 3 4 5 then you could do:

local $, = "|";

and then $in{my} would look like 1|2|3|4|5

Last edited by:

RedRum: Jan 30, 2002, 2:53 PM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Sorry, didnt follow you.



local $, = "|";
@fields = split("|", $in{my});


The reason this is so weird is that I manage to get the values and split them correctly through the solution I described below. BUT it doesnt get recognized when doing ($a eq $b) ? even if those values are the same..... argh

Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Do you mean to have the code like this:?



local $, = "|";
@fields = split("|", $in{my});




Thanks!
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Yeah that should work.

Or

local $, = "|";
@fields = split /\|/, $in{my};

Last edited by:

RedRum: Feb 6, 2002, 7:02 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Got it now... finally. Thanks a bunch
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Actually, Im kinda back to square one :(

I am now able to extract the values I get back into proper $fields. for instance my=103104015 become $fields[0]=103,$fields[1]=104,$fields[2]=105



The weird problem is that when I use this in --- if($something eq $fields[x]) ---

only the first field is usable.... the other ones gets ignored even though I can see that the values in the if string is identical. very strange and it makes me mad :).
Quote Reply
Re: [eric74] Passing values in the URL In reply to
How about:

if (grep { ^/\Q$something\E$/ } @fields) {

>>if($something eq $fields[x]) <<

You'd need == for numbers instead of eq

Last edited by:

RedRum: Feb 6, 2002, 11:44 AM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
sorry. what does the

if (grep { ^/\Q$something\E$/ } @fields) {


do? thanks
Quote Reply
Re: [eric74] Passing values in the URL In reply to
It will just see whether $something matches any of the elements in @fields...eg...

Code:
my $something = 55;
my @fields = qw/100 50 55 20/;

if (grep { /^\Q$something\E$/ } @fields) {
print "Yipee";
}
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
ah cool. basically, I have a "foreach line" (in the txt db) --> check if that lines $id equals any of the $my's. If it does... then --> push (@result_list, $line)

What would be the best solution for that do you think?



thanks!
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Could you just explain a little more?

Do you mean you have a list of numbers in a file and you want to match them against $in{my} ?

Last edited by:

RedRum: Feb 6, 2002, 12:03 PM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
yeah, I have this file:

18,dfgdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdf

32,dfgdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdf

15,dfgdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdfg,sdfgsdf



where the first number is the $id. Then I grab $my from a method=get form and divide them into $fields[x].

The last thing I do is:

foreach $line (@chunk) {
($id,$a,$b,$c) = split(/\|/,$line);

for ($count=0;$fields[$count]>0 ;++$count) {

if ($fields[$count] eq $id) {
push (@result_list, "$line")

}

}

}
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Ah I see....hmm this should work...

Code:
my @numbers = split /\|/, $in{my};

open FH, "<bla..."....
while (<FH>) {
if (/^(\d+)/ && grep { /^\Q$1\E$/ } @numbers) {
push @result_list, $_;
}
}
close FH;

Last edited by:

RedRum: Feb 6, 2002, 12:15 PM
Quote Reply
Re: [eric74] Passing values in the URL In reply to
I just made a few alterations. Thought I'd point it out incase you missed them :)

Last edited by:

RedRum: Feb 6, 2002, 12:15 PM
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Thanks a bunch. Didnt really get it to work though.

What does:

if (/^(\d+)/ && grep { /^\Q$1\E$/ } @numbers) {

do? Thanks again
Quote Reply
Re: [eric74] Passing values in the URL In reply to
What happened?

>> What does: if (/^(\d+)/ && grep { /^\Q$1\E$/ } @numbers) { do? <<

Looping through the file assigns every line to $_ on every iteration of the loop and with regex $_ can be omitted, so:

/^(\d+)/

....is the same as saying "if this line begins with one or more digits"

......then the grep line is checking whether the id we found (which is now assigned to $1) ... exists in @numbers.

So to review here's how it looks as words....

"if this line begins with a number and the number can be found in @numbers then add that number into the @result_list array"

Maybe I misunderstood what you wanted it to do?
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
Hmm, been doing some thinkg (!). Could it be that the code I use for grab and format the method=get values mess this up?:



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
#######################################################################################
}
Quote Reply
Re: [eric74] Passing values in the URL In reply to
Hmm unusual code :)

Can you print $in{my} though and does it looks right?
Quote Reply
Re: [RedRum] Passing values in the URL In reply to
no that code put it as $data{'my'}
> >