Results 1 to 19 of 19
  1. #1

    Need Javascript help for form, zipcode database

    I have a form that calls a script that searches a zipcode flatfile database of over 42,000 zipcode records. To make it faster I want to break the database into 3 or more files and search the proper file based on the zipcode input.

    What I need is some javascript that will take the input of the form below and only use the proper hidden input for the right database file:

    <form name=myform action=/cgi-bin/script.pl>
    <input type=text name="zipcode">
    <input type=hidden name=database value="database1">
    <input type=hidden name="database" value="database2">
    <input type=hidden name="database" value="database3">
    <input type=submit value=search>
    </form>


    If the text input for zipcode is 0-19999 I want it input search database 1 only. If the input is 20000-60000 it should search database 2 only. If the input is 60000-99999 it should search database 3 only.

    So I need some javascript to take the zip code input and change the hidden input for "database" to search the proper database and not search the other database files.

    Any help is appreciated!

  2. #2
    Two questions:

    1) Is there some reason you're using a flat file rather than a database (such as MySQL)?

    2) Why not code script.pl to determine which file to search?

  3. #3
    Originally posted by OIS
    Two questions:

    1) Is there some reason you're using a flat file rather than a database (such as MySQL)?
    Because it is already set up and works fine just a little slow. I am searching lots of records (over 42,000 zipcodes) but each record only has 3 fields (zipcode, state, county) and all I need to do is return the one state and county field for each zip code that is inputed.

    2) Why not code script.pl to determine which file to search? [/B]
    Because script.pl is already set up and working and I thought it would be easier just to vary the input from the form. I will look in to altering it, however my skill with perl is about the same as javascript (poor)

    Thanks for your help.

  4. #4
    I would definitely discourage you from doing this client side (i.e., with JavaScript). What's going to happen when someone has JavaScript turned off?

    If you're interested in getting this to work much faster, let me know and I'll help you get it set up with MySQL. It's really not that hard (assuming you have the necessary resources on your system).

  5. #5
    Originally posted by OIS
    I would definitely discourage you from doing this client side (i.e., with JavaScript). What's going to happen when someone has JavaScript turned off?

    If you're interested in getting this to work much faster, let me know and I'll help you get it set up with MySQL. It's really not that hard (assuming you have the necessary resources on your system).
    I have a P4 2.8 dedicated server with 1GB ram.

    I just didn't want to start over since it works pretty good now. It takes about 2 seconds to search and return the proper record out of 42,000 records. Bear in mind that these records will not be changing (well maybe once a year I might remove obsolete codes and add new codes). They are in the format as below:

    PHP Code:
    Zipcode County     State
    00501    Suffolk    
    New York
    00544    Suffolk    
    New York
    00601    Adjuntas    Puerto Rico
    00602    Aguada    Puerto Rico 
    All the script does is search for 1 specific zip code that is entered into the form and returns the associated county and state and then passes that data onto another script. 2 seconds isn't bad, I just thought I would split up the database into smaller chunks and try to get the time down to about a second. It's not a big deal if I don't, just something I was working on.

    Made sense to me if I broke the database up into, say 5 smaller files and used javascript to determine which database file it would search based on the zipcode entered in the form. Really, if someone has javascript turned off, I doubt if they would be doing anything serious anyway as most sites require it. It wouldn't bother me to miss someone that had it turned off, as there are alternate ways for them to search on my website.

    I know MySQL is faster for large databases (I have an invision board forum that uses MYSQL) but it just didn't seem worth the trouble for this project since I do not have the skills myself to write a script to interface with MYSQL and also since I already had it working fairly well.

    Thanks for your help!

  6. #6
    Code:
    <script language="javascript">
         function chooseDB(objForm)
         {
              if(objForm.zipcode.value>0&&objForm.zipcode.value<20000)
                   objForm.database.value="database1"
              else if(objForm.zipcode.value>20000&&objForm.zipcode.value<60001)
                   objForm.database.value="database2"
              else if(objForm.zipcode.value>60001&&objForm.zipcode.value<100000)
                   objForm.database.value="database3"
         }
    </script>
    <form  name=myform action="/cgi-bin/script.pl" onSubmit="return chooseDB(this)">
    <input type=text name="zipcode">
    <input type=hidden name="database">
    <input type=submit value=search>
    </form>
    Hope this helps
    Cenvo Hosting
    Unmatched Quality Unbeatable Price
    www.cenvo.com

  7. #7
    Originally posted by cenvo
    Code:
    <script language="javascript">
         function chooseDB(objForm)
         {
              if(objForm.zipcode.value>0&&objForm.zipcode.value<20000)
                   objForm.database.value="database1"
              else if(objForm.zipcode.value>20000&&objForm.zipcode.value<60001)
                   objForm.database.value="database2"
              else if(objForm.zipcode.value>60001&&objForm.zipcode.value<100000)
                   objForm.database.value="database3"
         }
    </script>
    <form  name=myform action="/cgi-bin/script.pl" onSubmit="return chooseDB(this)">
    <input type=text name="zipcode">
    <input type=hidden name="database">
    <input type=submit value=search>
    </form>
    Hope this helps
    It looks like what I need however it doesn't pass the name of the database to the script via <input type=hidden name="database">, thus I get the error that no database name is specified.

    Of course I put in the actual file names of the database files in the javascript.

    Thanks, any other ideas?

  8. #8
    I just played with it and it looks like it should (at least it did for me).

    Are you testing it with a valid zip code? Numbers greater than 100000 will fail the way you're describing, so maybe you want to add a line of code to trap/catch those too.

    brian

  9. #9
    Originally posted by brianbloom
    I just played with it and it looks like it should (at least it did for me).

    Are you testing it with a valid zip code? Numbers greater than 100000 will fail the way you're describing, so maybe you want to add a line of code to trap/catch those too.

    brian
    Yes I am using a valid zip code database, over 42,000 records. The error I get from my perl script when I use the above javascript is that no database is selected. When I do it without the javascript and use 1 database file and use <input type=hidden name=database value="largedatabasefile.txt"> it works fine. Of course it is slower with the 1 large file than it would be with several files, and a speed up is what I was trying to achieve.

  10. #10
    Originally posted by hostseeker
    Yes I am using a valid zip code database, over 42,000 records. The error I get from my perl script when I use the above javascript is that no database is selected. When I do it without the javascript and use 1 database file and use <input type=hidden name=database value="largedatabasefile.txt"> it works fine. Of course it is slower with the 1 large file than it would be with several files, and a speed up is what I was trying to achieve.

    Could you provide the javascript code, form html and the zip code value that you used to test.
    Cenvo Hosting
    Unmatched Quality Unbeatable Price
    www.cenvo.com

  11. #11
    Here it is, thanks for your help:

    <script language="javascript">
    function chooseDB(objForm)
    {
    if(objForm.keywords.value>0&&objForm.keywords.value<20000)
    objForm.dbname.value="lower.txt"
    else if(objForm.keywords.value>20000&&objForm.keywords.value<60001)
    objForm.dbname.value="middle.txt"
    else if(objForm.keywords.value>60001&&objForm.keywords.value<100000)
    objForm.dbname.value="higher.txt"
    }
    </script>



    <form name="form2" method="POST" action="/cgi-bin/zip/search.cgi" onSubmit="return chooseDB(this)">
    <input type=text name="keywords" size=10 maxlength=5>
    <input type=hidden name="dbname">
    <input type="submit" value="search">

    Error message is "you did not select a database" meaning it did not pass the database file name to the <input type=hidden name="dbname">

  12. #12
    That's really strange since I just tested the latest code you show and it works for me... Try switching to GET instead of POST and see if you see the value in the URL?

    What zipcode number are you testing with?

  13. #13
    It looks to me like 20000 and 60001 will get skipped.

  14. #14
    try the following code to confirm if the value is being returned to the hidden field
    Code:
    <script language="javascript">
    function chooseDB(objForm)
    {
    if(objForm.keywords.value>0&&objForm.keywords.value<20000)
    objForm.dbname.value="lower.txt"
    else if(objForm.keywords.value>20000&&objForm.keywords.value<60001)
    objForm.dbname.value="middle.txt"
    else if(objForm.keywords.value>60001&&objForm.keywords.value<100000)
    objForm.dbname.value="higher.txt"
    return false;
    }
    </script>
    
    
    
    <form name="form2" method="POST" action="/cgi-bin/zip/search.cgi" onSubmit="return chooseDB(this)">
    <input type=text name="keywords" size=10 maxlength=5>
    <input type=text name="dbname">
    <input type="submit" value="search">
    once your problem is resolved you can revert it back to hidden field
    Cenvo Hosting
    Unmatched Quality Unbeatable Price
    www.cenvo.com

  15. #15
    The dbname is not being passed to the script. I tried your code cenvo in your last post and got the same error. I could manually type the dbname into the box for the dbname text input field and it would work, but the javascript would not put it in there when the form was submitted.

    I also tried using GET with the original code as someone else suggested and could see in the URL that dbname= (there was no value after the = sign).

    Could it be possible that there should be some sort of variable in this statement : <input type=hidden name="dbname" value="variable here"> ?

    I thank everyone for all their help!

  16. #16
    Okay, here's my guess: You have another Javascript error somewhere that is stopping the execution of the program.

    My suggestion: Download Mozilla or Firefox if you don't run it already. Open a browser window, and type javascript: (hmm looks like this BBS added a space.. make sure you make yours one word) in the url field. This will pop open the javascript debug window. Now in the original window load the page in question. Watch if any errors show up in the debug window.

    Plan B: Add a line at the top of the function like:

    function chooseDB(objForm) {
    alert(objForm.keywords.value);
    ... rest of code ...

    Reload the page and enter a zip code. When you click submit you should see an alert popup echoing the zip code you entered. No alert at all is proof that there's a different error somewhere.

    I've already mentioned that I ran the code cenvo offered and it worked for me, so something else must be going on here other than that snippet of code....

  17. #17
    And all this drives home the reason I don't like using JavaScript for critical purposes. For rollovers and other non-essential purposes it's fine. Of course, in this particular case, you could have the full db as the initial value and then just use JS to change it to the proper section. That way, it will still work even without JS.

  18. #18
    Originally posted by brianbloom
    Okay, here's my guess: You have another Javascript error somewhere that is stopping the execution of the program.

    Whoppee, you guessed it! I had a javascript form validation script also to verify that the input of the form was only numeric and exactly 5 numbers. When I removed that script the database file selection javascript worked perfectly!

    Thank you and everyone else for your help! Also OIS, what you said works also! I can have the value of the large single database as the dbname in the hidden field and if someone has javascript off then it will still search that database. If they have javascript enabled it will replace the value with the proper smaller database files and search then. I turned off javascript and tried it and it works fine.

    The speed is better with the smaller databases! About half the time, it takes about 1 second instead of 2 seconds, which makes it worth my time, also the CPU won't take as much of a hit searching smaller files. Actually I think I am going to split the large file in to about 5 small files.

    I am not even going to worry about the form input validation that I am losing due to removing the other javascript, I will just set the maxlength to 5 for the zipcode field and if someone doesn't put in numbers, or they use less than 5 numbers it will return an invalid zip code, they can figure it out from there.

    Thanks again!!!!!!!

  19. #19
    For your validation:
    http://developer.irt.org/script/form.htm#5
    Check out FAQ's 538 and 816 (and any of the others if you want to soup it up more)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •