tags:

views:

130

answers:

3

Below is my full code, its basic, you select a country and it shows, or hides the correct form underneath, problem is it gives an error

"getState" is not define

Now I am a total noob at this but how do you debug these kind of errors?

<form method="post" name="form1"> 
   <select style="background-color: #ffffa0" name="country" onchange="getState(this.value)"> 
      <option>Select Country</option> 
      <option value="223">USA</option> 
      <option value="224">Canada</option> 
      <option value="225">England</option> 
      <option value="226">Ireland</option> 
   </select> 

   <select style="background-color: #ffffa0" name="state"> 
      <option>Select Country First</option> 
   </select> 

   <input type="text" name="othstate" value="" class="textBox" style="display: none;"> 
</form> 

<script> 
$(function() { 
    $('#country').change( function() { 
        var val = $(this).val(); 
        if (val == 223 || val == 224) { 
            $('#othstate').val('').hide(); 
            $.ajax({ 
               url: 'findState.php', 
               dataType: 'html', 
               data: { country : val }, 
               success: function(data) { 
                   $('#state').html( data ); 
               } 
            }); 
        } 
        else { 
           $('#state').val('').hide(); 
           $('#othstate').show(); 
        } 
    }); 
}); 
</script>

**UPDATED CODE PARTIALLY WORKING**

<script>
$(document).ready(function() {
   getState();
});

function getState() {
    $('#country').change( function() {         
        var val = $(this).val();
     if (val == 223) {
      $('#state').val('').show();
         $('#othstate').hide();
     }else {
        $('#state').val('').hide();
        $('#othstate').show();
     }
   });
 }
</script>

<form method="post" name="form1">
   <select style="background-color: #ffffa0" name="country" id="country">
      <option>Select Country</option>
      <option value="223" selected="selected">USA</option>
      <option value="224">Canada</option>
      <option value="225">England</option>
      <option value="226">Ireland</option>
   </select>
<div id="state">
   <select style="background-color: #ffffa0" name="state" id="state">
      <option>Select State</option>
      <option value="1">Florida</option>
      <option value="2">New York</option>
      <option value="3" selected="selected">Georgia</option>
      <option value="4">California</option>
   </select>
</div>
<div id="othstate"><input type="text" name="othstate" id="othstate" value="" class="textBox"></div>
</form>
+3  A: 

I think you just need to remove the onchange="getState(this.value)" attribute from your select tag, since the getState JavaScript function is no longer needed/being used.

Gordon Bell
+3  A: 

You haven't named your function getState.... it doesn't know where to go to find the definition of the function "getState"!

also you have not set up your events correctly... should look like this:

<script>
$(document).ready(function() {
   getState();
});

function getState() {
    $('#country').change( function() {         
        var val = $(this).val();
        if (val == 223 || val == 224) {
         $('#othstate').val('').hide();
         $.ajax({
            url: 'findState.php',
            dataType: 'html',
            data: { country : val },
            success: function(data) {
                $('#state').html(data);
            }
         });
     }else {
        $('#state').val('').hide();
        $('#othstate').show();
     }
   });
 }
</script>

you may also need to remove the "onChange" from the select tag as the other poster mentioned. in jQuery, you don't actually set the events in the HTML (a benefit!), you just bind the events to elements based on their tag, id, or CSS class.

EDIT: I've noticed you're using name="country" instead of id="country" or class="country"... this will prevent you from making proper selections

Jason
um... why the downvote?
Jason
This is so misleading you should delete it immediately. And then slap yourself.
Josh Stodola
how is this possibly misleading?
Jason
Because you are telling him to invoke his change event handler on page load! I don't even think you understand the question...
Josh Stodola
Now you've edited it to fix the concerns, so I will remove the down-vote. Thank you.
Josh Stodola
sorry i wasn't clear enough for you, oh wise josh... lol. i understand the question just fine, thank you!
Jason
Thank you this code makes a lot more since to me but i still cannot figure out how to make it work your code shows this on page load "missing ) after argument list function getState(){" then when I select a country it brings up this "getState is not defined" can you help please
jasondavis
@jasondavis - sorry, i forgot to close the last bracket... i've updated accordingly... also please check my EDIT for more help
Jason
I appreciate your help I really don't know much about javascript but I am still getting errors = "missing ) after argument list" and when I select country I still get the getState is not defined
jasondavis
Ok I removed the "onChange" opart and that takes away the getState is not defined error but not sure if I will see it again once I get the other error corrected
jasondavis
WHOOPS! i forgot the paren in the first function (document.ready)... updated!
Jason
That made some progress there are now 0 errors! Bad thing is it does nothing though, it does not change no matter which country is selected =(
jasondavis
well that will take some experimentation on your part... i don't know what your ajax function returns... if you want to check to make sure it's getting called, you can add a `alert("i'm working!");` in the beginning of the function to let you know that it's at least being called... then you will have to play around from there...
Jason
Ok thanks 1 day im gonna get this thing working lol BTW I added in that alert when I put it right above the ajax call it alerts when usa or canada are selected and not when other countries are so that seems to be correct however it never hides or changes the states or other state box, I even try removing the ajax part, like if I had the state values on the page already but it wont change i am pretty lost now
jasondavis
Ok i got it to change the boxes at least I had to remove style="display: none;" from otherstate field
jasondavis
where are you now? what have you accomplished and have yet to accomplish?
Jason
Got it working finally, first of all I have to change the state to a DIV instead of a dropdown because the ajax retunrs a whole new dropdown, also at the top of the javascript I had to move some hide and show functions around a bit to get the desired result, thanks a ton for your help
jasondavis
no problem... be sure to accept this as your answer if it fixed your problem!
Jason
It almost works perfect there is 1 flaw can you help me?When I add selected="selected" to USA which is will most existing users on my site will have, it loads with the othstate box instead, how can I get around this? I have added my new complete code to my question post above
jasondavis
Also when they have a state value saved in DB it seems this might be a problem again, i am considering generating the state list for the USA and having it on the page instead of calling it with ajax
jasondavis
can you make the usa box load by default and then check to see if it's another box, and load accordingly?
Jason
if you have something like a list of states that will not change, i suggest just creating a static list and putting it in. no reason to pull it up every time when it's not going to change.
Jason
yeah thats what I did, I have it almost working how I want it now I just need to get state drop down to show by default, I added my updated code again if you care to take a look
jasondavis
glad you got it working and that i could help you!
Jason
Im kinda dissapointed, it seems I may have to use php to output what javascript is shown, after lots of test just now it seesm that I cant make USA and other countries happy, I can hide other state easily but if a user has a non USA country saved then when page loads, it shows there country like England but with US states, this code is great for user signup form but when they have values saved, I don't see a way with this to make it work depending on what they have saved already, do you have any suggestions?
jasondavis
maybe I will write 2 functions and depending on what country they have saved I will output the javascript function depending on that in php in php
jasondavis
I am going to start a new question about a this code but a different issue on it
jasondavis
A: 

You should remove the call to getState in your select element. Also, I noticed that in your script you're referencing the names of the elements as if they are IDs. You should either change your name attributes to id attributes, or use selectors like "select[name='country']".

DLH
do you mean to remove onchange="getState(this.value)" if so then how would the function be called, BTW I have switched to the code posted by Jason if that makes a difference however I still get errors
jasondavis
@DLH: "name" has been depracated (or was IE specific, I forget which) and should be avoided. Use ID attributes or class names.
ScottSEA
@jasondavis: Yeah I meant the onchange="getState(this.value)". The "$('#country').change(" handles the onchange event for you, and the function inside that runs every time the element with the id "country" fires a change event.@ScottSEA: I don't believe the name attribute is deprecated for the select element. http://www.w3.org/TR/xhtml1/#h-4.10 lists some elements for which it IS deprecated.
DLH