Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Auto Generate Form Fields

(Page 3 of 4)
> > > >
Quote Reply
Re: [Alex] Auto Generate Form Fields In reply to
I've been trying this piece of code in V2.1.2 but seems to have problems to get it to work. The dropdown (select) fields builds as they should but when going to "modify" the existing value is once again not preselected. Did anyone else have this problem?

Code:
sub {
my $html = $DB->html ( ['Users'], $IN );
return $html->select ( { name => 'Status', value => $IN->param('Status') });
}

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Probably you forgot adding values key:
values => { form_value => displayed_value }
Code:
sub {
my $html = $DB->html ( ['Users'], $IN );
return $html->select ( {
name => 'Status',
value => $IN->param('Status'),
values => { 'foo'=> 'Foo', 'bar'=> 'Bar' }
});
}

Hope that helps.

EDIT: also a bit help for further reference (comment from GT::SQL::Display::HTML::select code):
# Make a select list. Valid options are:
# name => FORM_NAME
# values => { form_value => displayed_value }
# value => selected_value
# or
# value => [selected_value1, selected_value2]
# multiple => n - adds MULTIPLE SIZE=n to select list
# sort => coderef called to sort the list or array ref specifying the order in
# which the fields should be display. A code ref, when called, will be
# passed the following arguments: ($value{$a}, $value{$b}, $a, $b)
# blank => 1 or 0. If true, a blank first option will be printed, if false
# the blank first element will not be printed. Defaults to true.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Apr 7, 2003, 4:19 PM
Quote Reply
Re: [webmaster33] Auto Generate Form Fields In reply to
Thanks for the help. It looks to me like this solution will only allow values defined in your global. I'm building the dropdowns with the options and values that I include though the admin interface (a long list, of all the countries in the world). I'd like for the appropriate value to be "selected" when I load the form again to modify a link depending on the value that was chosen when the record was entered. I've got to believe there's a way to do this without defining all the countries again in my global as well.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
If you have a country list somewhere in the database or database definition files, then it's easy to get them, and have them sorted into a hashref, and including into the global above...

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Auto Generate Form Fields In reply to
How would you do that, webmaster33 ?

Thanks Smile

------------------------------------------
Quote Reply
Re: [DogTags] Auto Generate Form Fields In reply to
If you tell me where & how the country data is stored, I show you example.

I show you now example with a guessed environment.
For example if you have a Country field, you fill Form Names & Form Values like this:
Form Names:
us
uk
etc.

Form Values:
United States
United Kingdom
etc.

Once you have the items there, you can easily get & mix them into hash => value pairs like this:
Code:
sub {
my %hash;
my $form_names_array_ref = $DB->table('Links')->form_names->{'Country'};
my $form_values_array_ref = $DB->table('Links')->form_values->{'Country'};
foreach my $form_name (@{$form_names_array_ref}) {
foreach my $form_value (@{$form_values_array_ref }) {
$hash{$form_name} = $form_value;
}
}
my $html = $DB->html ( ['Users'], $IN );
return $html->select ( {
name => 'Country',
value => $IN->param('Country'),
values => { %hash }
});
}
I did not try the code, so I'm not sure it's correct syntactically.
But gives you a good starting point.
You could get country data from anywhere, even from the database, this is just an example.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Apr 8, 2003, 7:18 AM
Quote Reply
Re: [DogTags] Auto Generate Form Fields In reply to
This should do the trick for database selection:

Code:
sub {
my %tmp = ();
my $html = $DB->html( ['Countries'], $IN );
my $sth = $DB->table('Countries')->select('Country_Value', 'Country_Name');
$tmp{$val} = $name while (my ($val, $name) = $sth->fetchrow);

return(
$html->select({
name => 'Country',
value => scalar $IN->param('Country'),
values => \%tmp

})
);
}

That assumes all your country names/values are in a table called "Countries" and the column names are "Country_Name" and "Country_Value"

Last edited by:

Paul: Apr 8, 2003, 7:34 AM
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Thanks, webmaster33 and Paul. I'll give this a go.

Much appreciated Smile

------------------------------------------
Quote Reply
Re: [webmaster33] Auto Generate Form Fields In reply to
I tried this code with

$html = $DB->html ( ['Users'], $IN );

changed to

my $html = $DB->html ( ['Links'], $IN );

It builds the dropdown with the countries in just fine but it still does not select the value that is already in the db. The dropdown still shows up as if nothing is selected. I use this when someone comes back to modify a record. They already selected their country when they added the record. When the come back to modify I'd like the country that they've chosen before to be selected in the dropdown.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Probably $IN->param('Country') input isn't correct, of there is no input at all. Do you have that input parameter passed from other page?
Try setting it to a fixed value:
Code:
value => "uk",

Also did you try to look at the generated html code?
Can't help debugging until you don't give us more info.

Furthermore add this code before the return line:
Code:
use Data::Dumper; # Used for script debugging
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
print $IN->header();
print "<br>\n hash: " . Dumper(\%hash) . "\n";
print "<br>\n form_names_array_ref: " . Dumper(\$form_names_array_ref) . "\n";
print "<br>\n form_values_array_ref: " . Dumper(\$form_values_array_ref) . "\n";

List the printed debug values from the top of the page, and the generated html form...

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Can you show us the rest of the code you have, we may be able to spot something.
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
In Reply To:
Can you show us the rest of the code you have, we may be able to spot something.
Sure...

In my DB I have a record called Ad_Country. The input is a FORM and the FORM TYPE = SELECT in my settings.

In the "Form Names" field I have:

Argentina
Australia
Austria
Bahamas
...etc

In "Form Values" I have:

Argentina
Australia
Austria
Bahamas
...etc

I have a global called select_country that looks like this:

Code:
sub {
my %hash;
my $form_names_array_ref = $DB->table('Links')->form_names->{'Ad_Country'};
my $form_values_array_ref = $DB->table('Links')->form_values->{'Ad_Country'};
foreach my $form_name (@{$form_names_array_ref}) {
foreach my $form_value (@{$form_values_array_ref }) {
$hash{$form_name} = $form_value;
}
}
my $html = $DB->html ( ['Links'], $IN );
return $html->select ( {
name => 'Ad_Country',
value => $IN->param('Ad_Country'),
values => { %hash }
});
}

In my template I call this with the tag <%select_country%>

It builds a drop down with countries and enters the country name in the DB when the user adds a link. When they come back to modify it I'd like for the country that they currently have in there to be pre-selected. This is where I have a problem. It doesn't happen.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Try this to change this part in the global, so let we see if 'Austria' becomes selected:
Code:
return $html->select ( {
name => 'Ad_Country',
value => 'Austria',
values => { %hash }
});

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Auto Generate Form Fields In reply to
Nope... when I do that it builds a dropdown with nothing selected... and the only country in the dropdown is Zimbabwe!! (the last country on the list) and Zimbabwe appears as many times as I have countries on the original list! Crazy

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Try this:
Code:
sub {
my %hash;
my $links_db = $DB->table ('Links');
my $form_names_array_ref = $DB->table('Links')->form_names->{'Ad_Country'};
my $form_values_array_ref = $DB->table('Links')->form_values->{'Ad_Country'};
foreach my $form_name (@{$form_names_array_ref}) {
foreach my $form_value (@{$form_values_array_ref }) {
$hash{$form_name} = $form_value;
}
}
my $html = $DB->html ($links_db, $IN);
return $html->select ( {
name => 'Ad_Country',
value => 'Austria',
values => { %hash }
});
}
I replaced ['Links'] with links table object. Probably it helps.
If it does not work, you should debug (print with Dumper) all the variable values until the return. What was the variable contents?

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Auto Generate Form Fields In reply to
This still gives me Zimbabwe only...

Quote:
you should debug (print with Dumper) all the variable values until the return
I don't know how to do this.... Unimpressed

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Code:
sub {
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
my %hash;
my $links_db = $DB->table ('Links');
my $form_names_array_ref = $DB->table('Links')->form_names->{'Ad_Country'};
my $form_values_array_ref = $DB->table('Links')->form_values->{'Ad_Country'};
foreach my $form_name (@{$form_names_array_ref}) {
foreach my $form_value (@{$form_values_array_ref }) {
$hash{$form_name} = $form_value;
}
}
my $html = $DB->html ($links_db, $IN);
print $IN->header();
print "form_names_array_ref: " . Dumper(\$form_names_array_ref) . "<br>\n";
print "form_values_array_ref: " . Dumper(\$form_values_array_ref) . "<br>\n";
print "hash: " . Dumper(\%hash) . "<br>\n";
return $html->select ( {
name => 'Ad_Country',
value => 'Austria',
values => { %hash }
});
}

Be warned, this will print variable contents, so debugging this way on a live site, is not too lucky.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Here's some working, tested, shortened code =)

Code:
sub {
my $count = 0;
my %temp = ();
my $html = $DB->html( ['Links'], $IN );
my $table = $DB->table('Links');
my $names = $table->form_names->{'Ad_Country'};
my $vals = $table->form_values->{'Ad_Country'};
for my $val (@$vals) {
$temp{$val} = $names->[$count++];
}
return $html->select(
{
value => scalar $IN->param('Ad_Country'),
sort => [sort(@$names)],
blank => 0,
name => 'Ad_Country',
values => \%temp
}
);
}

Last edited by:

Paul: Apr 21, 2003, 1:47 AM
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Pretty cool :) and only 2 years in the making <G>

Can this be generalized to allow passing in:

<%make_select_box ("table", "field")%>

by adding

my ($table_name, $field_name) = @_;

and changing the apropriate lines in the sub?

Could this be generalized to handle the check_box and radio subtypes?


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Sweet! Paul, you are a good man! Like pugdog said... I've been whining for this like a little biatch for 2 years. Wink

Thank you.

Safe swoops
Sangiro

Last edited by:

sangiro: Apr 21, 2003, 8:00 PM
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Hmmm... seems like I may have spoken a little soon... well, I still think you're almost there but I did some more testing...

It seemed to work well when i modified the first link. When I went to the second, in this case a link that had no prior setting for country it automatically selected the first country on the list (a small problem in itself but not a major issue) I then selected a country for the second link and saved it. Went back to the first link and it selected the first country on the list again (Argentina) while the value in the DB for that link is the United States.

Hereafter, doesn't matter which link I go to it simply selects the first country every time... almost like it stopped working after the first time. Weirdness.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
This may be more of an issue in your html/templates.

Do you have it set so that if you have passed in values already, you don't overwrite them in some way?

The code above just generates the form values. It's up to you to keep track of them, and make sure you are assigning them properly. (this can mean some awefully _huge_ templates)


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Auto Generate Form Fields In reply to
Hmmm... I have the <%select_country%> tag on the include_form.html template only. I'm not using it anywhere else.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
It selects $IN->param('Ad_Country') so when loading one of the modification forms the value to select will be stored as a tag and not an input parameter, so you'd need to pass that into the global then receive it with:

my ($select) = shift;

Then change:

value => scalar $IN->param('Ad_Category')

to:

value => scalar $IN->param('Ad_category') || $select


*I think*

Last edited by:

Paul: Apr 22, 2003, 1:15 AM
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Paul may be on to something, which is why I don't like using the $IN hash. It's good for passing in initial parameters to a called script, but that's about all you should count on it for -- and even then, not trust it <G>

Take a look at passing in the values, and modifying the call:

<%get_select_box ($table, $field_name, $selected)%>

You might need to do something with an <%if ..%> wrapper, to prevent odd errors -- my comment about large templates. In most cases, the $table could be passed a 'Table_Name' and 'Field_Name' since you know what you are checking for in any specific template call, but the $selected field would vary based on the value in the Link record.

In your specific case, you'd want to do something like

<%if Country%>
<%get_select_box ($Country)%>
<%else%>
<%get_select_box (0)%>
<%endif%>

And reverse Paul's suggestion of changing:

value => scalar $IN->param('Ad_Category')

to:

value => scalar $IN->param('Ad_category') || $select

to:

value => $select || scalar $IN->param('Ad_category')

instead.

This might be more on the right track to solving it. It should override and ignore anything in the $IN hash, if something was passed in. You might want to check to see what was passed in was a valid value, such that

exists $temp{$select} || $select = "some default value";


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
> > > >