Jump to content

Javascript question


robert_gsfame

Recommended Posts

Yeah, a 1MB JavaScript file just screams that it's in need of refactoring.  Stick to DRY - Don't Repeat Yourself.  If you find yourself repeating entire sections of code, put it in a function.

 

It's impossible to remove any part of the code as everything is necessary.

I put the javascript inside my php page

 

Ah, that's not really the best design decision (as you're currently learning the hard way).  Ideally, your markup should be separate from your PHP, and include/require-ed in with the bare minimum of code needed to pass in processed values for display.  JavaScript library code should also be put into external files.  Your critical JS - the stuff that actually manipulates page items - should be as small and concise as possible.  Again, DRY.

 

Also, are you coding your JS in an unobtrusive manner (i.e., not sticking a bunch of repeated JS in HTML tags)?  That can save a lot of repetition, and is really the way to go.  It also facilitates Separation of Concerns, making it easier for one to modify, edit, and debug their code.

Link to comment
Share on other sites

i think so...its like this  there are 3 combobox the first combobox is country where i can write it without using any javascript <option>America</option><option>Belgium</option> and each country will fill the second combobox so if i choose america n all cities in america will appear in my second combobox and finally the second will affect the third one which is the area code....

 

So can u give me some example on how to write those efficiently??

Link to comment
Share on other sites

:D how can i retrieve the data from database n put those directly to the second combobox using javascript....don't have any idea on how to do it..

i really wish to use Javascript as page doesnt need to reload

 

so let say i have this in database

country    city

America    Chicago;Texas;Las Vegas

 

n i choose America from first combobox, how can i retrieve all cities from database without reloading the page??using javascript..

 

 

Link to comment
Share on other sites

Like Ken said, you'd have to use AJAX.

 

I'm getting the feeling that you're shooting blind here.  That you're not entirely comfortable with JavaScript, and are essentially throwing code against the wall to see what sticks.  I was there myself not too long ago.  Unfortunately, that's not the most effective way to code.

 

My advice is to get a good JavaScript book.  I find John Resig's book to be the best: Pro JavaScript Techniques.

 

Also, if that's what your DB tables actually look like, you should probably spend some time with database design as well.  Cities should be in their own table, with their home country's id as a field to link them, like so:

 

Countries table -
id                    name
1                     America
2                     Albania
3                     Argentina
.
.
.

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

Cities table
id                   country_id                    name
.
.
.
22                  1                                  Baltimore
23                  1                                  Boston

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.