Gossamer Forum
Home : Products : DBMan : Customization :

javascript within html.pl problem - URGENT!

Quote Reply
javascript within html.pl problem - URGENT!
I have a problem wtih a javascript that is embedded in an html.pl file: The javascript works, but for some reason, the data is not saved in the data base. Here are pertinent snipits of code:


In my Script (sub html_page_top, sub html_error_page_top and sub html_imp_preview):
function init(frm) {
var combo = frm.Select_Box;
var combo2 = frm.Category;
frm.category.value = combo.options[combo.selectedIndex].value;
frm.occupancytype.value = combo2.options[combo2.selectedIndex].value;
}


In my sub html_imp_record_form code:

<SELECT NAME="Select_Box" onChange="fillSelectFromArray(this.form.Category, ((this.selectedIndex == -1) ? null : team[this.selectedIndex-1]));">
<OPTION VALUE="-1">Select Category
<OPTION VALUE="Site Improvements-Retaining Wall">Site Improvements - Retaining Wall
<OPTION VALUE="Site Improvements-Culvert">Site Improvements - Culvert
<OPTION VALUE="Site Improvements-Curbing">Site Improvements - Curbing
<OPTION VALUE="Site Improvements-Flagpole">Site Improvements - Flagpole
<OPTION VALUE="Site Improvements-Fencing">Site Improvements - Fencing
<OPTION VALUE="Site Improvements-Grating">Site Improvements - Grating
<OPTION VALUE="Site Improvements-Landscaping">Site Improvements - Landscaping
<OPTION VALUE="Site Improvements-Miscellaneous">Site Improvements - Miscellaneous
<OPTION VALUE="Site Improvements-Parking Lot">Site Improvements - Parking Lot
<OPTION VALUE="Site Improvements-Paving">Site Improvements - Paving
<OPTION VALUE="Site Improvements-Sprinklers">Site Improvements - Sprinklers
<OPTION VALUE="Site Improvements-Stairway/Deck">Site Improvements - Stairway/Deck
<OPTION VALUE="Site Improvements-Swimming Pool">Site Improvements - Swimming Pool
<OPTION VALUE="Site Improvements-Synthetic Surface">Site Improvements - Synthetic Surface
<OPTION VALUE="Site Improvements-Tennis Court">Site Improvements - Tennis Court
<OPTION VALUE="Other">Other
</SELECT><br>&nbsp;
<SELECT NAME="Category" SIZE="1">
<OPTION> </OPTION>
</SELECT></TD></TR>


In my sub html_imp_add_form (and other places) code:
print qq|
<form action="$db_script_url" method="POST" name="form1" onsubmit="init(this)">
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<input type=hidden name="category" value="">
<input type=hidden name="occupancytype" value="">

|;

I really need help figuring out what I've done that keeps this code from working. The first select box puts a "-1" in the database, the options list puts nothing in the database -- it was working at one time too! now it's not!

Thanks for your help!
Lynette
Lynette
Hollister, Ca

Last edited by:

Alex: Aug 5, 2003, 3:53 PM
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
Just an additional note --

I have this script in 3 places in the htmimp.pl file:

In the sub html_page_top, in sub html_err_page_top and in sub html_imp_preview

This is because on each of these pages, the form appears and the javascript needs to run to allow new selections where appropriate.

I just realized that the script loading on a secondary page (record preview, error on page) the values are probably being zeroed out.... how can I get around this? change the value names maybe? so nothing gets trampled? but then I need the correct values to be the ones written....
Lynette
Hollister, Ca
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
Hi Lynnette,

I am not a programmer and I can't help you with your specific problem but I found that if I put the java script file in a *.js file it would work for me. I found the info on this thread.

http://gossamer-threads.com/..._string=java;#140000

I hope this is useful.

Regards,

Dave
Quote Reply
Re: [daverad] javascript within html.pl problem - URGENT! In reply to
Don't have a clue!!!!

but you have

<OPTION VALUE="Site Improvements-Retaining Wall">Site Improvements - Retaining Wall

should it be ???

<OPTION VALUE=1>Site Improvements - Retaining Wall

You did a great job of documenting the mods!

Do you have a working back up somewhere thait you can restore?

/signed
Clueless in Atlanta


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
I too am clueless, but perhaps a few questions would give me a clue.

1. Is this a multiple select field, i.e. select all that apply?

2. Perhaps the data type for that field is numer? thus -1 is the only valid value?

Other than that, ???

I have built select boxes from a seperate database, and since the database is a seperate file, I can add or delete options from it without messing around with html.pl

I used sub build_select_field_from_db in db.cgi, then modified it to open a different database, which is where the select options are stored.

This way I avoided Javascript issues all together.

Sorry if that was not the answer you wanted.
Quote Reply
Re: [joematt] javascript within html.pl problem - URGENT! In reply to
No, not multiple selects but the second field is dependent on the value of the first and it is mostly static, very seldom if ever will it change.

the -1 comes from this (coded in my sub html_record_form):

<TD Colspan='3' VALIGN="TOP">&nbsp;
<SELECT NAME="Select_Box" onChange="fillSelectFromArray(this.form.Category, ((this.selectedIndex == -1) ? null : team[this.selectedIndex-1]));">
<OPTION VALUE="-1">Select Category
<OPTION VALUE="Land">Land
<OPTION VALUE="Other">Other
</SELECT><br>&nbsp;
<SELECT NAME="Category" SIZE="1">
<OPTION> </OPTION>
</SELECT></TD></TR>


I think what is happening is that when the preview form is loading, the correct values shown in the long_record are being over written by the null values from the script because the script has to load again on that form incase a change to the form is needed. I may need to take the preview mod out of the script or find a way to modify it so that loading the script for that form, doesn't trample the values from the initial form pass through.

My question for you would be, can I do related select boxes your way? from a db? if so, HOW?? a simple code example would be appreciated!

THanks!
Lynette
Hollister, Ca
Quote Reply
Re: [esm] javascript within html.pl problem - URGENT! In reply to
No, the values are correct, if I just number the option values - the numbers are what get written to the database! I want the name written to the database.

The javascript is running fine and it doesn't work when I take it out to a .js file for some reason.

So I am stumped on this one, trying to write the values to the database is the only problem but that's a BIG problem!
Lynette
Hollister, Ca
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
well, just to clarify: If you use numbers, do they actually get written to the database and when you use the category names, they do not get written to the database?

Speaking of categories, if the values are actually categories, do you need to format the value in the same format as a category???

How about that backup?


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."

Last edited by:

esm: Apr 15, 2003, 9:41 AM
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
how about changing?

<SELECT NAME="Select_Box" onChange="fillSelectFromArray(this.form.Category, ((this.selectedIndex == -1) ? null : team[this.selectedIndex-1]));">

to

<SELECT NAME="Select_Box" onChange="fillSelectFromArray(this.form.Category, ((this.selectedIndex == -1) ? null : Category[this.selectedIndex-1]));">


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
OK, so some sample code, but first here is a link I found that might help;

http://www.gossamer-threads.com/...i?post=186816#186816

I have attached code as txt file, but this is mostly the same as the MOD build_select_field_from_other_db so you might go back to the original.

I added to the original a line that catches old values (old values not in the current database of select fields) so that the user has clue why the value they put in last time is not selected.

But I think you could do something in the same part of the code (about 23 lines up from bottom) that would;

if the current value of the field eq "xyz" then $somenewvariable = "abc"
then use $somenewvarible later in the form, such as that second field that is dependant on the first.

???????

Sorry this post is kinda rushed, Lunch time.
Quote Reply
Re: [esm] javascript within html.pl problem - URGENT! In reply to
that just fixes the null problem.
Lynette
Hollister, Ca

Last edited by:

ltillner: Apr 15, 2003, 3:40 PM
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
OK, I just looked at what you wrote and based on that I understand that the add page (with the selects) goes to a preview (confirm) page. That appears to be your problem.

sub html_imp_preview just calls html_imp_record_form again, which I assume just re-initializes the variables. To test this, enter the values on you input screen and then look at your preview page. Do the values from input screen appear on the preview page - both select areas.

I use links 2 and I had to do the following in my add_confirm page.

Quote:
<SELECT NAME=Color>
<OPTION value="Brown"<%if Color eq 'Brown'%> SELECTED<%endif%>>Brown</OPTION>
<OPTION value="Black"<%if Color eq 'Black'%> SELECTED<%endif%>>Black</OPTION>
<OPTION value="Red"<%if Color eq 'Red'%> SELECTED<%endif%>>Red</OPTION>
</SELECT>


I would assume you would have to do something similar. For both select sections in the preview page. Although I am not sure what effect that would have on that special javascript. Maybe nothing. Just have to try it and see. May have to create a memory variable mCategory and carry that forward to the preview screen.

In any event, you would create a new html_imp_record_form for the preview page and use something like the above. Since you are not using templates, I would assume just plain ole perl IF statements could be used.

Only the "SELECTED" values will be transmitted from your preview page. Right now the values in those two sections appear not to be selected since you re-initialized them when you called the html_imp_record_form routine again in the preview page.


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."

Last edited by:

esm: Apr 15, 2003, 6:56 PM
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
just to clarify, notice the highlited:

Quote:


category = new Array(
new Array(
new Array("Non-Conforming", 01001)
),
new Array(
new Array("Administration", 02001),
...
<FORM NAME="main">
<SELECT NAME="Make" onChange="fillSelectFromArray(this.form.Category, ((this.selectedIndex == -1) ? null : category[this.selectedIndex-1]));">
<OPTION VALUE="-1">Select Category
<OPTION VALUE=1>Buildings/Structures - Non-Conforming
...
<OPTION VALUE=27>Land
</SELECT>
<BR>
<SELECT NAME="Category" SIZE="8">
<OPTION> </OPTION>
<OPTION> </OPTION>


Sorry about getting that wrong earlier. If I change them all to Category, I get an error msg. I guess category refers to the array and Category to the name of the links field. So using team/Category would have worked.

And I am still of the opinion that the values of the select stmt should conform to the actual formating in the database for that field.


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] javascript within html.pl problem - URGENT! In reply to
Well the names have to do with the original script that I used as a template. But that's NOT the issue. The script runs fine --- I just changed category to "team" and it cleared up the issue with nothing being written for the second select <for some reason when I change team to category the select doesn't work - so I did it the other way around - easier)

BUT, the REAL PROBLEM is 2 fold:

1 - nothing is writing to the second database field in the preview mod

AND

2 - neither field is writing the correct value to the database when you press ADD IT from the preview form.

I have narrowed the issue down to these sections of the code, but not sure how to fix the problem!

First -- code in the script (this fills the select boxes when the script runs):

function fillSelectFromArray(selectCtrl, itemArray, goodPrompt, badPrompt, defaultItem) {
var i, j;
var prompt;
// empty existing items
for (i = selectCtrl.options.length; i >= 0; i--) {
selectCtrl.options = null;
}


Second -- code added to the original script (this creates variables for the data to be written to 2 fields)

function init(frm) {
var combo = frm.Select_Box;
var combo2 = frm.team;
frm.category.value = combo.options[combo.selectedIndex].value;
frm.occupancy.value = combo2.options[combo2.selectedIndex].value;
}


Then in the HTML.PL file in sub html_preview

the code that should put the data in the variables into the database (but doesn't for a couple of reasons):

<form action="$db_script_url" method="POST" name="form1" onsubmit="init(this)">
<center><INPUT TYPE="SUBMIT" NAME="cap_add_record" VALUE="Add It!"><HR></center>
<input type=hidden name="db" value="$db_setup">
<input type=hidden name="uid" value="$db_uid">
<input type=hidden name="category" value="">
<input type=hidden name="occupancytype" value="">


Here's what IS happening - selecting items from both select boxes(in html_add_form), then submitting that, I see the word value for the first box in the correct field, NOTHING in the second text box for the second field ---

AND when I click on the ADD IT button (from html_preview) I get a -1 in the first field.

NOTE, the preview record form ALSO loads the script again, and I believe the script empties the variables so that is why the -1 is written which is the null value if no selection is made.

IF I go to the form and select an item before clicking on the ADD IT Button, the data is written to the database and the successful submission form shows the fields filled in.

THEREFORE, where in my code am I:

1 - not writing the var combo2 data to the field?

2 - how can I keep the first data selected, allow for a change if needed, and WRITE the correct data (either originally selected or reselected) from the preview form to the file?

Thanks!
Lynette
Hollister, Ca
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
The reason for my clarification was to agree with you that the names (Category, team) would not be the cause of the data not being written to the database. As you recall, I suggested maybe that was the problem in an earlier post and you even agreed and that that would only affect the null value. I just wanted to correct it for anyone reading this some months later. If the names are not correct, then the javascript will generate an error and you won't even be able to select from the boxes.

Now on to your problem as you succintly stated at the end of your post ( and I admit my initial focus was on the add process and not the preview process ). Unfortunately, I know less about javascript than I do perl - and remember I did state at the beginning of this that I was clueless! Unsure

First, it would seem that you should delete the following from the sub html_imp_preview routine
<input type=hidden name="category" value="">
<input type=hidden name="occupancytype" value="">

or better yet, change them to
<input type=hidden name="category" value="Select_box">
<input type=hidden name="occupancytype" value="Category">
(when you load the preview page, view the source code and see what these say, they should have come over from the add page)

You have two SELECT statements, one named Select_Box and one named Category. When referring to the options of those select statements, it seems to me that you would need to use Select_Box and Category, not category and occupancytype. I know that perl is case sensitive; not sure about javascript but it seems to be.

Shoud the init function be?
function init(frm) {
var combo = frm.Select_Box;
var combo2 = frm.Category;
frm.Select_box.value = combo.options[combo.selectedIndex].value;
frm.Category.value = combo2.options[combo2.selectedIndex].value;
}

but I'm not sure why you need the init function as the values should be there when you click the submit button (you could rename the select statements. Or maybe you could create a var for category and occupancytype and assign the Select_box.value and frm.Category.value values respectively.

But I don't know quite how to initially populate the Category select statement in the preview form.

Sorry, just some ideas and rushed ones at that, and not even very good ones, I'm afraid Pirate. Maybe someone else has some better suggestions Blush. I even thought of using the onload feature in the body tag to populate the two select statments but couldn't it to work and not enough time to pursue it right now.


Gene
"The older I get, the more I admire competence, just simple competence in any field from adultery to zoology."
Quote Reply
Re: [esm] javascript within html.pl problem - URGENT! In reply to
Thanks Gene,

I'm glad you clarified what you trying to say. I'm just frustrated that I can't get this figured out! Now I found something else that's broken... oh well.

Good suggestions, I will play with them this afternoon and see what I come up with.

I do now have both fields being written to. One of the complications is that this "little" project now has 5 databases and 6 html.pl versions -- ALL needed. so I'm going through not only multiple instances of the script within one html.pl -- but multiple versions of the script within DIFFERENT html.pl's!!

Thanks again for your help and ideas!

Lynette
Lynette
Hollister, Ca
Quote Reply
Re: [esm] javascript within html.pl problem - URGENT! In reply to
In Reply To:

First, it would seem that you should delete the following from the sub html_imp_preview routine
<input type=hidden name="category" value="">
<input type=hidden name="occupancytype" value="">

or better yet, change them to
<input type=hidden name="category" value="Select_box">
<input type=hidden name="occupancytype" value="Category">
--------------- Response: This is needed or nothing writes to the form. and the value makes no difference... this helps me narrow my problem further. It's the script. I need the script on the preview page to NOT reset the values for those fields unless they are changed.... How to do THAT is my current problem. I know ZIP about javascript. I guess I'll be learning quickly though! -------------------- Shoud the init function be?
function init(frm) {
var combo = frm.Select_Box;
var combo2 = frm.Category;
frm.Select_box.value = combo.options[combo.selectedIndex].value;
frm.Category.value = combo2.options[combo2.selectedIndex].value;
}
-------------- Response: This is working correctly. It's not the problem from what I've been able to tell.
---------------
But I don't know quite how to initially populate the Category select statement in the preview form. -------------- Response: Me neither! sigh....... and I believe that is the real issue. If I could either get the select boxes to initialize with the originally chosen values or to write the original values without overwriting them with default null values, I'd be all set. Thanks again for your help.
moved this to a new thread to see if a new title will get some javascript input! http://gossamer-threads.com/perl/gforum/gforum.cgi?post=240241
Lynette
Hollister, Ca

Last edited by:

ltillner: Apr 16, 2003, 2:31 PM
Quote Reply
Re: [ltillner] javascript within html.pl problem - URGENT! In reply to
I tried to follow along, but got lost. Sorry.

I debug scripts by seeing the results and using error logs and script debuggers. I'm not very good at reading code to solve problems.
If you use Netscape, it includes a javascript console which lists any errors and explains the problem.

As I read the code, the problem seems to be in the JavaScript itself. Meaning, the script is meant to return a value from the second drop down menu. I considered using the same script as well but it appeared that the original script was written for a specific purpose and doesn't offer a lot of options. (mostly for indexing and not so much for form submission)

I believe that what you need is an if statement to prevent the script from zeroing out your values on the preview page.

I would suggest going to a javascript forum for help in modifying the script.
http://forums.webdeveloper.com/...php?s=&forumid=3

On another note, your html.pl file is well organized so I thought that I'd offer a suggestion to someone else who loves order.

Instead of placing the javascript in the header of each page, if you don't want to or can't us a .js file, create a new sub for your javascripts and the call them in the headers the use the scripts.

Example:

sub javascripts {
# --------------------------------------------------------
# Place all JavaScripts here.
print qq|
<script language="JAVASCRIPT">
function example() {
}
</script>
|;
}

________________________________
in sub html_add_form
________________________________
<head>
<title>$html_title: Add a New Record.</title>
|;
&javascripts;
print qq|
</head>

<body ....

This way, all of your javascripts are in one place for editing.


Just a thought.
beetlemanTongue

Marcus L. Griswold