0

This is the continution of this Question

Here is the Code for Dynamic JQuery Option Select Generator.

First i will choose the Country, which i didn't include the code, it will return the CountryID which is passed to the controller, then RegionID, then CityID. IT works well until generating the CityID Option select Menu, while i move to the AreaID it didn't work.

What i am missing and how can i fix that ?

I can't able to generate the Menu after the third Second One.

Here is the HTML :

<div id='result' name="result">
<select  id="name">
<option value="" disabled>Please select above</option>
</select>
</div>
<div id='resulta' name="resulta">
<select  id="name">
<option value="" disabled>Please select above</option>
</select>
</div>
<div id='resultb' name="resultb">
<select  id="name">
<option value="" disabled>Please select above</option>
</select>

Here is my Script :

<script>
  var ab = $.noConflict();
  ab(document).ready(function() {
    ab(document).on("change", "#CountryID", function() {
      var CountryID = ab("#CountryID").val();
      ab.post("globalregiongenerator", {
          CountryID: CountryID
        },
        function(data) {
          ab("#result").html(data);
        });
    });

  });
</script>


<script>
  var ac = $.noConflict();
  ac(document).ready(function() {
    ac(document).on("change", "#RegionName", function() {
      var RegionName = ac("#RegionName").val();
      ac.post("globalcitygenerator", {
          RegionName: RegionName
        },
        function(data) {
          ac("#resulta").html(data);
        });
    });
  });
</script>

<script>
  var ad = $.noConflict();
  ad(document).ready(function() {
    ad(document).on("change", "#CityName", function() {
      var CityName = ad("#CityName").val();
      ad.post("globalareagenerator", {
          CityName: CityName
        },
        function(data) {
          ad("#resultb").html(data);
        });
    });
  });
</script>

And here is the Server Side :

public function globalregiongenerator()
    {
    $CountryID = Input::get('CountryID');

    $result = DB::select("SELECT * FROM region where CountryID =$CountryID");
    echo "<select  id='RegionName' name='RegionName'> <option value='' id=''>Select the Area</option>"; 
    foreach ($result as $value) 
    {
    echo "<option value=".$value->RegionID.">".$value->RegionName."</option>";
    }
    echo "</select>";
    }

    public function globalcitygenerator()
    {
    $RegionID = Input::get('RegionName');
    $result = DB::select("SELECT * FROM city where RegionID =$RegionID");
    echo "<select  id='CityName' name='CityName'> <option value='' id=''>Select the Area</option>"; 
    foreach ($result as $value) 
    {
    echo "<option value=".$value->CityID.">".$value->CityName."</option>";
    }
    echo "</select>";
    }

    public function globalareagenerator()
    {
    $CityID = Input::get('CityName');
    $result = DB::select("SELECT * FROM area where CityID =$CityID");
    echo "<select  id='CityName' name='CityName'> <option value='' id=''>Select the Area</option>"; 
    foreach ($result as $value) 
    {
    echo "<option value=".$value->CityID.">".$value->CityName."</option>";
    }
    echo "</select>";
    }

Note :

Uncaught TypeError: undefined is not a function

This is the error i got in console

5
  • Just a side note: $.noConflict will not work if there is a conflict with $ (it should have been jQuery.noConflict()). You can use the shortcut DOM ready handler jQuery(function($){ YOUR CODE USING A LOCAL $ }); to also provide a locally scoped $ to your code. Commented Nov 14, 2014 at 10:22
  • Please do a "save-as" from the page in your browser, and show us the generated HTML that jQuery sees. :) Commented Nov 14, 2014 at 10:28
  • @TrueBLueAussie : Should i replace all the elements of $.noConflict with jQuery.noConflict ? Commented Nov 14, 2014 at 11:05
  • Simpler & smarter to use this DOM ready handler syntax instead: jQuery(function($){ YOUR CODE USING A LOCAL $ }); (but, yes you can just use jQuery.noConflict()) Commented Nov 14, 2014 at 11:06
  • Thanks i follow your advice and my issue "Undefined" error was overcome. Commented Nov 14, 2014 at 11:15

1 Answer 1

1

Change

echo "<option value=".$value->CityID.">".$value->CityName."</option>";

to

echo "<option value='".$value->CityID."'>".$value->CityName."</option>";

Same with other echos. You're generating

<option value=united states>United States</option>

but it needs to be

<option value='united states'>United States</option>

Update:

Change them to

echo "<option value=\"".$value->CityID."\">".$value->CityName."</option>";
Sign up to request clarification or add additional context in comments.

9 Comments

Double-quotes on attributes are recommended for best compatibility with HTML standards. Use single-quotes on the outside strings when generating HTML (or delimit inside "s with \").
@TrueBlueAussie Didn't know that, I thought value='states' was the same as value="states". Also I've never used php so I wasn't sure if you can use single quote on the php side.
I updated to all echo "<option value='".$value->RegionID."''>".$value->RegionID."</option>";as you said, but still it has error the same error as - Uncaught TypeError: undefined is not a function
" or ' will work on most browsers, but best to stick to ". Use \" if you cannot use single-quote on the string literal. Also a lot of tools cannot cope with single-quoted attributes.
@Stackerman See the update. Also in your comment you have an extra single quote.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.