Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Auto Generate Form Fields

Quote Reply
Auto Generate Form Fields
I'm sure this is either obvious or has been discused before but I can't find it in with a search....

I'd like to automatically generate some drop-downs and checkbox fields from the database and call then with <%tags%> on the add.html template. How do I do this?

Safe swoops
Sangiro
http://www.dropzone.com/
Quote Reply
Re: Auto Generate Form Fields In reply to
Alex should answer this, so we have the right way.

It would most likely involve creating a sub {} in the globals.txt file, but I know he has added the tools to do lists more easily.

Too much for me to try to figure out all at once. It's going to take me awhile to catch up with the 2.0 version <sigh>.... so much going on!

PUGDOG«
PUGDOG« Enterprises, Inc.
FAQ: http://LinkSQL.com/FAQ


Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

Pugdog has got the right idea. Go to Build->Template Globals and add a tag called 'dropdown' and put:

Code:
sub {
my $links_db = $DB->table ('Links');
my $sth = $links_db->select ( ['CustomField'] );
my @vals;
while (my $field = $sth->fetchrow) { push @vals, $field; }
my $html = $DB->html ($links_db, $IN);
my $select = $html->select ( name => 'CustomField', values => \@vals );
return $select;
}
What this does is line by line:

1. Get a GT::SQL::Table object for the Links table.
2. Select the CustomField values from the Links table.
3. Go through the results and save them in @vals.
4. Get a GT::SQL::Display::HTML object to display forms.
5. Generate a select list with name='CustomField' using the values we just generated.
6. Returns the select list.

You can simliarily use ->checkbox, or ->radio instead of select to generate different values.

Hope this helps,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
Hey Alex,

Tried to build a radio select field (simple yes/no select) from the database using your code:

In Reply To:
sub {
my $links_db = $DB->table ('Links');
my $sth = $links_db->select ( ['Tandem'] );
my @vals;
while (my $field = $sth->fetchrow) { push @vals, $field; }
my $html = $DB->html ($links_db, $IN);
my $radio = $html->radio ( name => 'Tandem', values => \@vals );
return $radio;
}
This gave me the following error:

In Reply To:
Error in subroutine: Can't use string ("name") as a HASH ref while "strict refs" in use at /path_to_script/admin/GT/SQL/Display/HTML.pm line 284.
Suggestions?

Safe swoops
Sangiro
http://www.dropzone.com/
Quote Reply
Re: Auto Generate Form Fields In reply to
I got the same error message. What's may possibly wrong?

Thanks

http://www.biomedgate.com
****Portal to life****
Quote Reply
Re: Auto Generate Form Fields In reply to
Alex...any thoughts on this error?

Safe swoops
Sangiro
http://www.dropzone.com/
Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

Sorry, I may be not understanding what you are trying to do. Are you trying to generate a select/radio list with the values being all the values in the database? Or are you trying to generate a select/radio list with the values being the allowed values for that field?

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
Alex,

Not exactly sure what the difference is in your mind but I believe I'm trying to do the latter. i.e. When you create a new column in the database, you can enter a range of possible options for that field (let's assume it's select field)

When using the admin interface to add a link that select field will be built automatically with all the possible options to choose from. I'd like to do the same and imbed it in the add.html template through a tag.

I'd be interested in knowing how to do te first one as well! That would open all kinds of interesting possibilities!

Safe swoops
Sangiro
http://www.dropzone.com/
Quote Reply
Re: Auto Generate Form Fields In reply to
In Reply To:
trying to generate a select/radio list with the values being the allowed values for that field?
That will be my interested, and if possible, those allowed values could be import from a text file or database fields.



Quote Reply
Re: Auto Generate Form Fields In reply to
Alex,

Any help on this?

Safe swoops
Sangiro
http://www.dropzone.com/
Quote Reply
Re: Auto Generate Form Fields In reply to
I like also to have the auto-generatet dropdown/select fields on the add.html like it is at add-function of the admin section.

Michael

Quote Reply
Re: Auto Generate Form Fields In reply to
So let's assume we are dealign with the Status field in the User table which has values 'Not Validated', .., 'Administrator'. We want to build a select list out of this now. I think this would be the quickest way:

Code:
sub {
my $html = $DB->html ( ['Users'], $IN );
return $html->select ( { name => 'Status', value => 'Default' });
}
Default is the value you want to Default it to. If you don't specify it, I think it will use CGI input instead.

Hope this helps,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
Alex,

it builds a select field, but in my case it contains no data to select, like it does in admin section.

Michael

Quote Reply
Re: Auto Generate Form Fields In reply to
Same problem here. Builds an empty select field.

Safe swoops
Sangiro
http://www.dropzone.com/
Quote Reply
Re: Auto Generate Form Fields In reply to
in the links.def file are my options under form_values,
like i made the input at links-properties.
the values section is empty.

'values' => [
],

Michael

Quote Reply
Re: Auto Generate Form Fields In reply to
Alex,

did you fix this in 2.02 ??

I still get empty select-fields.

Michael

Quote Reply
Re: Auto Generate Form Fields In reply to
Hey Alex,

Looking at the amount of times this thread has been viewed by comparison to others, it seems like there are a lot of folks who are curious about how this is done. Any additional thoughts?

Safe swoops
Sangiro
http://www.dropzone.com/
Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

I'll figure this one out when I get back on tuesday. =)

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
Hi Alex,

Did you already figure this one out?



Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

This is a bug in the html module and will be fixed in the next release due out shortly.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
I updated to 2.0.3

Now, if I add a new Column (select field) then in the admin there is shown a
multiselect field !?

Whats wrong? And what is exactly to do to display the field in the add.cgi form?

Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

The form_size attribute determines how many you can select, make sure it is at 0 to make it a single select.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
Thanks Alex that is OK (in the admin, it works),
but what is now exactly to do to display the select field in the add.cgi formular?
I tried it with the globals as you decscibe it above but this doesnt work it only
shows an "Unknown Tag:" message.

Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

The code at http://gossamer-threads.com/p/127748 works fine for me. Are you sure you are putting it in the right template set?

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
Hi Alex,

Ok all works fine - I added my select tag in the add formular - great - but if a user dont fill out all the fields
the add-error template resets all the select fields and the user has to fill out all select fields again.
How can I take the input values of the select and radio fields in the add error formular? I think little changes in global templates can help?

Quote Reply
Post deleted by Addy In reply to
Quote Reply
Re: Auto Generate Form Fields In reply to
Who can solve this problem? (resetting of select and radio fields)

Alex, you have no idea?

Quote Reply
Re: Auto Generate Form Fields In reply to
Addy,

this is not the only problem to customize linksql. I wait also for answer in another thread about "modifying select-field column". Creating, modifying and displaying select- and checkbox-fields is a MUST to customize linksql.

It┤s very quiet about this topics, so I have the feeling that you need to wait for further releases .....

Not pretty, but seems to be reality.

Michael

Quote Reply
Re: Auto Generate Form Fields In reply to
Is there any reason why you posted the same post 4 times?

You can delete them by going to Edit > Delete

Paul Wilson.
Installations:
http://www.wiredon.net/gt/
Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

You could try:

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

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
No, Paul I have no idea, why the message was displayed 4 times !! I only posted one. Now I saw it and deleted the 3 others.

Quote Reply
Re: Auto Generate Form Fields In reply to
Hi Alex,

I used your code and it works fine for select-fields, and I tried to use the code also for checkbox-fields in this way

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

The checkboxes where displayed fine at the add-form, but only the first selected checkbox-field will be saved.

Any ideas?

Michael


Quote Reply
Re: Auto Generate Form Fields In reply to
Hi All

In Reply To:
Are you trying to generate a select/radio list with the values being all the values in the database? Or are you trying to generate a select/radio list with the values being the allowed values for that field?
What I am interested in is generating a select box with the values allowed in a specific field for a specific record - all within link.html and detail.html - as with a web store catalog.

The link.html would be the specific product with its specific options (generated from its values in the fields), and the detail.html would have the same specific options with a larger image, etc.

So, for example, if you had a field called SIZE, and you had 500 records of, say, shoes and shirts, each with it's own range of values in the SIZE field, I want to build a select box for each link in link.html and detail.html containing just the values for each record. If there are no values in the Size field, a select box is not built.

Is this specifically what Alex's code will do, or does Alex's code snag all values up and down that entire column of values and build one big giant list of all values within the db for the Size field?

What I'm trying to do is use the script as a web store catalog, and being able to build select boxes for Item Options like Size by using just the values in the Size field for each ID is an important feature.

I hope that I have explained this well enough.

Thanks very much for your help. Smile

DT

Quote Reply
Re: Auto Generate Form Fields In reply to
Hi,

Probably best to use a bit of code to do this. I'm not 100% sure what you want to do, but this might help:

size_select_list =>
Code:
sub {
my $tags = shift;
my @allowed_sizes = split /\s+/, $tags->{Allowed_Sizes};
my $selected = $IN->param('Size');
my $output = '<select name="Size">';
foreach my $size (@allowed_sizes) {
if ($selected eq $size) {
$output .= "<option selected>$size";
}
else {
$output .= "<option>$size";
}
}
$output .= "</select>";
return $output;
}
To use that, you would add a column called Allowed_Sizes that would contain a space separated list of sizes that apply to this item. Then you would simply put:

<%size_select_list%>

to generate the select list.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Auto Generate Form Fields In reply to
Thank you so much, Alex. Smile

What I'm trying to do is set up a web store that can handle all kinds of pricing options (volume discounts and the like), item options (including options that adjust the price), shipping options, etc. - very flexible.

All these pricing and other options would be included in the database, and the output pages would be automatically built to work with my shopping cart.

This requires the ability to easily create and manage multiple select boxes, checkboxes, and radio buttons, as well as lots of hidden stuff.

To offer options that adjust the price, while still building the select boxes, some additional code has to be added to each option, and I'm concerned that the extra symbols might mess up the build process. For example, if I want to offer size options, and the 2XL and larger sizes involve an additional charge, the select options should look like this:

Small
Medium
Large
X-Large
XXL!+1.50
XXXL!+2.00

and the field value would look like this, given your code above using a space as a delimiter within the size field:

In Reply To:
Small Medium Large X-Large XXL!+1.50 XXXL!+2.00
Will the extra ! and + symbols mess up the building process?

Also, another thing that I want to do is offer volume discount pricing. This involves a string of pipe-delimited options for the cart to recognize the volume discount pricing scheme and also the use, again, of the ! symbol.

For example, the format that my shopping cart uses for volume discount pricing looks like the following:

In Reply To:
<input type=hidden name=id_price value="1-10!9.95|11-20!9.50|21-100!9.00">
I would have to include the full string of:

In Reply To:
1-10!9.95|11-20!9.50|21-100!9.00
in the "Price" field of X product, but my concern is that the pipes and the exclamation points will mess up the build process.

Should the cart's required pipes delimiters and exclamation points be escaped somehow so that the build process is unaffected, while safeguarding the integrity of the data that is to be transferred from the db to the output pages? Perhaps the quantity discount line and the size line should be enclosed in quotes.

Thank you so much, again. I really appreciate your help.SmileSmile

DT



Quote Reply
Re: Auto Generate Form Fields In reply to
In Reply To:
Will the extra ! and + symbols mess up the building process?
No, you can store whatever data you like in the fields and Links SQL won't care. My only concern would be that if you need a size with a space in it, you may want to pick a different delimiter. You could also use checkboxes or a multi-select list, and Links SQL will use newlines to delimit it automatically. That may be a better solution. All it would mean is changing the above to split on \n rather than \s.

As for the discounts, same answer. The build won't care about what's in the data.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Auto Generate Form Fields In reply to
Hi Alex

This has worked great for select boxes. Bulls-eye!

What I am now trying to do is use something similar for creating checkboxes and radio buttons, where there are already values in a link field like Allowed_Sizes.

For example, there might be Allowed_Colors, etc.

I have been hacking away with the size_select_list stuff to build checkboxes and radio buttons, but I keep getting errors.

Here is what I'm trying for radio buttons:

Code:
sub {
my $tags = shift;
my @allowed_sizes = split /\s+/, $tags->{Allowed_Sizes};
my $selected = $IN->param('Size');
foreach my $size (@allowed_sizes) {
if ($selected eq $size) {
$output .= "<input type=radio name=Size value=$size checked> $size";
}
else {
$output .= "<input type=radio name=Size value=$size> $size";
}
}
return $output;
}

And, here is the current error message that I'm getting:

Quote:
A fatal error has occured:

Unable to compile 'size_radio_list'. Reason: Global symbol "$output" requires explicit package name at (eval 21) line 7.
Global symbol "$output" requires explicit package name at (eval 21) line 10.
Global symbol "$output" requires explicit package name at (eval 21) line 13.

How would I adjust things to be able to create checkboxes and radio buttons?

Many thanks, again, for your help. Smile

DogTags



------------------------------------------

Last edited by:

DogTags: Nov 18, 2001, 5:21 AM
Quote Reply
Re: [DogTags] Auto Generate Form Fields In reply to
** Bump **


------------------------------------------
Quote Reply
Re: [DogTags] Auto Generate Form Fields In reply to
Quote:
A fatal error has occured:

Unable to compile 'size_radio_list'. Reason: Global symbol "$output" requires explicit package name at (eval 21) line 7.
Global symbol "$output" requires explicit package name at (eval 21) line 10.
Global symbol "$output" requires explicit package name at (eval 21) line 13.
You can fix those error by adding a:
my $output;
above the foreach loop.


Adrian
Quote Reply
Re: [brewt] Auto Generate Form Fields In reply to
Thanks, Adrian!

Worked great Cool

------------------------------------------
Quote Reply
Re: [DogTags] Auto Generate Form Fields In reply to
You always need to code "strictly" when writing the code for globals (and in general it is a good thing)

You need to declare all variables with my()

Last edited by:

PaulW: Nov 28, 2001, 7:16 AM
Quote Reply
Re: [PaulW] Auto Generate Form Fields In reply to
Thanks, PaulW

I'll eventually get the hang of Links SQL...maybe

Cool

------------------------------------------
Quote Reply
Re: [Alex] Auto Generate Form Fields In reply to
Linksl 2.10

I wanted to use a custom-select-field at include_form.html and tried to use your code without success



size_select_list =>
Code:
sub {
my $tags = shift;
my @allowed_sizes = split /\s+/, $tags->{Allowed_Sizes};
my $selected = $IN->param('Size');
my $output = '<select name="Size">';
foreach my $size (@allowed_sizes) {
if ($selected eq $size) {
$output .= "<option selected>$size";
}
else {
$output .= "<option>$size";
}
}
$output .= "</select>";
return $output;
}

--
Michael Skaide

http://www.cycle24.de

Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Would there also be a way for doing this with Textboxes??

I'd need to generate anywhere from 1 to 6 textboxes in the same form, and each textbox would have to have a unique name.

Thanks very much. Smile

------------------------------------------
Quote Reply
Re: [DogTags] Auto Generate Form Fields In reply to
Hi There,

Not sure if this helps you too much, but this will allow you to autopopulate your form inputs.

Upload this file into your ../admin/ directory, yup the same directory as admin.cgi, and you will be able to do the following.

For text boxes

Code:
<%HTML::text( 'name' => 'textbox1' )%>

If you have something like ?....&textbox1=foobarbaz or a keyname in the tags for GT::Template it will create the appropriate HTML for a form textbox.

As well, if you add additional parameters as following:

Code:
<%HTML::text( 'name' => 'textbox1', 'size' => 12 )%>

It will add the size=12 attribute to the input tag. You can also force the value of the textbox by just adding a .... value => 'sometext'

Please note that support for radioboxes and checkboxes are a little bit iffy, the following code

Code:
<%HTML::checkbox( 'name' => 'checkbox1', 'value' => 'bar' )%>

Will create the checkbox, but only if you have ...&checkbox1=bar... will it be checked. (makes sense why, I hope Wink )

Take a look inside the module for what other form tags it supports.

Let me know if you have any problems.

Please note that if anyone wants to use this in their Links SQL plugin, please rename the package into your own namespace, store it in another location and use that version.... as the interface may yet change. (This is just a personnal module that I'm making public that may be of use to others)

Last edited by:

Aki: Jul 11, 2002, 4:13 PM
Quote Reply
Re: [Aki] Auto Generate Form Fields In reply to
Hey, thanks, Aki !

This looks really interesting. You've got a lot of form stuff in HTML.pm

I'm having a problem uploading the script into /admin, though. I've tried to FTP your file into the /admin directory, but I'm getting denied - 550

I can upload stuff to the /templates directory, and even rename, etc., but for some reason, files at the /admin level seem to have other restrictions, but I can't seem to identify anything different.

Would you have any suggestions??

Many thanks for your help. Smile

------------------------------------------
Quote Reply
Re: [DogTags] Auto Generate Form Fields In reply to
np ;)

Make sure that the admin/ directory's chmod has been set to something 777 (temporarily) so that you can upload using ftp. Maybe try uploading using fileman as well, you might have better luck.
Quote Reply
Re: [Aki] Auto Generate Form Fields In reply to
Thanks muchly, Aki !

I'll give it a go. I appreciate your help. Smile Smile

------------------------------------------
Quote Reply
Re: [Aki] Auto Generate Form Fields In reply to
Fileman upload a success.

Thanks, Aki ! Smile

------------------------------------------
Quote Reply
Re: [Alex] Auto Generate Form Fields In reply to
Alex,

I used this piece of code and it seems to work fine. How can this be changed to build the list from values in a different installation of LinksSQL than the one you're displaying it in?

Safe swoops
Sangiro
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.
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Paul,

I'm still struggling with this. I tried to make the changes you suggested (not sure if this is right) but my code now looks like this:

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'};
my ($select) = shift;
for my $val (@$vals) {
$temp{$val} = $names->[$count++];
}
return $html->select(
{
value => scalar $IN->param('Ad_category') || $select
sort => [sort(@$names)],
blank => 0,
name => 'Ad_Country',
values => \%temp
}
);
}

This gives me a "Unable to compile 'select_country': " error....

Suggestions or fixes on how I implemeted your suggestion? Crazy

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
You are missing a comma:

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



Unable to compile or hard errors are almost _always_ punctuation or other syntactical errors, not logic ones.


PUGDOG´┐Ż Enterprises, Inc.

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

Last edited by:

pugdog: May 14, 2003, 1:02 AM
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
There was an error:
Code:
value => scalar $IN->param('Ad_category') || $select,

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: [pugdog] Auto Generate Form Fields In reply to
I actually added the comma before I mde this post. It gave me another error so I assumed (worngly I guess) that it should not be there. When I add it back I get:

Quote:
Not an ARRAY reference at /path_to_cgi/cgi-bin/classads/admin/GT/SQL/Display/HTML.pm line 306.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Oops, change:

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

to...

Code:
value => [scalar $IN->param('Ad_category') || $select ],
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Paul,

Here's the code I'm currently working with:

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'};
my ($select) = shift;
for my $val (@$vals) {
$temp{$val} = $names->[$count++];
}
return $html->select(
{
value => [scalar $IN->param('Ad_category') || $select ],
sort => [sort(@$names)],
blank => 0,
name => 'Ad_Country',
values => \%temp
}
);
}

I'm not sure if I added the my ($select) = shift; statement at the right place. At the moment this generates a dropdown but always defaults to the first country on the list... It does not go to the selected country when modifying. Unsure

Safe swoops
Sangiro
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Code:
value => [scalar $IN->param('Ad_category') || $select ],

Paul, what field should I reference for the Ad_category above? I don't have an Ad_category field, I obviously have the normal Category table. Is this what this is referring to? If so, should this just be "Category" or should it have the db prefix in fromt of it?

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Category should be ok.
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
Just from a stylistic and "safety" note, if you are shifting items off the passed parameters stack, it's much safer and better, less confusing, to shift those values immediately, not down in the code. It's possible to get confused, and chase your tail for 'bugs' if you don't.

The problem is this:


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


It would be better to do it:


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


Since $select is what you are shifting off the stack (what was passed in) and if nothing was passed in, default to the <yecch> $IN hash for some potentially corrupted or weird value .


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
I think $IN->param('Category') can be removed. I just looked further up the thread at the tag structure (which I hadn't seen before) and he is wrapping the global with:

<%if Category%>

...and then if that returns true then he's passing the category into the global. As $Category is the same as $IN->param('Category') then it becomes redundant.

Quote:
Since $select is what you are shifting off the stack (what was passed in) and if nothing was passed in, default to the <yecch> $IN hash for some potentially corrupted or weird value .

$select contains the same value as $IN->param('Category') - $select is just as insecure :)
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
You are now pulling the $IN values from another set of routines, inside another module or compiled block.

You can't trust it to pull the right $IN values, or not have them mixed up by that time. Maybe another running process, or maybe just buggy code refs, or whatever.

It's also confusing, and unnecessary -- especially if you can easily and specifically pass that value in. The key here, is "unnecessary" risk.

Good coding would suggest that you only take the $IN values from where they were originally passed in. Anything you need, you should specifically pass on (not necessarily from $IN, but from _any_ where). This has been a mantra for good coding for 20+ years.

If you need the $IN values, pass them as $in = $IN->get_hash()

When subroutines start pulling in values out of the air, you don't know where they are pulling them from -- only where you _think_ they are pulling them from <G>.

Repeatable, reproducable, and reliable coding would suggest that any values be specifically passed in, or requested (ie: grabbing your own table reference via $DB rather than the old $LINKS_DB || $DB->table().

I checked out some GT code, and in Build.pm, they ask for $IN in the category and detailed routine, as a last resort, and only to try to find an ID, if the check for a hash ref or passed in ID fails. That's probably OK, since those routines are called in several ways, one pretty directly via a user script. But it's only done on the right side of an || after everything else has failed, and only for one very specific -- and expected -- "harmless" type of value. "ID" is a key passed fragment of data, and the worst that will happen is it displays the wrong link. Also, it's the "top" or "key" field, so by calling it, even wrongly, you are getting the "whole" object, not a sub fraction of it. It's an entry point, not a branch.

Anyway, this is something that has been a mantra since I wrote my first line of code. "KNOW" where your data is coming from, and anything you need, EXPLICITLY pass. Don't "assume" anything. And this was back in the old stand-alone, single-user, single-process, single-brain-dead-OS days. It's even more true today.


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
Quote:
It's also confusing, and unnecessary -- especially if you can easily and specifically pass that value in.

Yes, that's the point I was making. As $Category is passed in to the global then you don't need $IN->param('Category') in the global itself. $Category is only $IN->param('Category') anyway, but just in the form of a template tag.
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
<G> Actually I realized that all that passing in was unnecessary -- redundant here, but I was commenting on the point about $select and $IN->_whatever_ being equally insecure. $select is "better" for the reasons above.

$IN should only be used in the original .cgi, or when you think the original .cgi didn't pass it in, or you are desperately trying to find a value, any value, and not bomb. But it's a risk. Are you grabbing the $IN that was, or the $IN that is now?

Once you call other modules (the jump.cgi / Jump.pm type construct, excluded) you should use explicitly passed values. Especially in things like the globals, or called functions from the templates. $IN will be unreliable. Even in the Jump.pm type construct, $IN should be called as soon as possible, then assigned to $in if needed more than initially. You at least know you have a uniform snapshot of what was in $IN when you started to work with it.

$IN is out of the modules control, but $in is encapsulated as best as perl can do it.

We've differed on this for quite a while now <G> But I'm old-school, and like to play it safe.


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
Quote:
I was commenting on the point about $select and $IN->_whatever_ being equally insecure. $select is "better" for the reasons above.

They are both exactly the same. $select is the Category parameter but just in the form of a tag. Whether you pass $Category into the global, or call $IN->param('Category') directly, you'll still end up with the same value.

Quote:
Especially in things like the globals, or called functions from the templates. $IN will be unreliable. Even in the Jump.pm type construct, $IN should be called as soon as possible, then assigned to $in if needed more than initially. You at least know you have a uniform snapshot of what was in $IN when you started to work with it.

$IN doesn't change in the middle of your code. The parameters are retrieved when the $IN object is created, which is when the script is requested by a client.
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
Paul,

We need to agree to disagree on this. $IN is an object that can be accessed by methods outside the subroutine, or other process. $IN is a "global" object, that was created where $IN was requested -- usually quite a ways away from a global or called template function.

$in, or a passed parameter, is a copy of what was in $IN, at the time you started the process. It can't (easily, or accidentally) be altered or changed, and you'll always get the values you expected, or thought you would at _that_ point in the program.

While in most cases it may not matter, there are going to cases -- the ones that really create grief -- where it might matter. Play it safe :)

While $select and $IN->{'Adcategory'} or whatever may (usually) contain the same "value", $select is a value that was passed in, and belongs to, the currently running code segment. $IN belongs to _EVERY_ code segment called after the original script was initialized.


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
Pugdog,

OK - I've tried your alternative (Paul, I couldn't get your s to work either). When modifying it still gave me the first country in the list only and not the country already in the DB. Here's what I went with:

Global: (select_country)
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'};
my ($select) = shift;
for my $val (@$vals) {
$temp{$val} = $names->[$count++];
}
return $html->select(
{
value => [$select || scalar $IN->param('Ad_category') ],
sort => [sort(@$names)],
blank => 0,
name => 'Ad_Country',
values => \%temp
}
);
}

And in my template I called it with:

Code:
<%if Ad_Country%>
<%select_country ($Country)%>
<%else%>
<%select_country (0)%>
<%endif%>

Where:

Ad_Country is the name of the field that holds the country value for each record
select_country is the name of the Global (above) that bulds the dropdown
Country is the name of the DB table tha holds all the country names

Any thoughts?

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
You are passing in $Country to the global - can you check what it contains.

Last edited by:

Paul: May 18, 2003, 3:10 AM
Quote Reply
Re: [sangiro] Auto Generate Form Fields In reply to
change

my ($select) = shift;

to

my $select = shift;


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
That won't make a difference. It only makes a difference the other way around (so to speak). For example if you pass in an array to the subroutine and do:

my $arg = @_;

Then $arg will contain the number of elements in @_...but:

my ($arg) = @_;

....will make $arg the value of element 0 in @_

It's to do with scalar vs list context.

shift() will force scalar context anyway so brackets or no brackets will be the same.
Quote Reply
Re: [Paul] Auto Generate Form Fields In reply to
I suspect this post will help solve this problem. Smile

Thanks guys!

Safe swoops
Sangiro

Last edited by:

sangiro: Oct 26, 2003, 5:52 PM