Advertisement:

Author Topic: Cities dropdown based on Region Select  (Read 36434 times)

yellow53

  • Full Member
  • ***
  • Posts: 142
Re: Cities dropdown based on Region Select
« Reply #45 on: December 16, 2013, 11:01:49 am »
Hi cartagena68,

thanks for the reply ! actually I'm looking for a solution in order to get the cities based on the region selected, on search.php and item-post.php

any idea how to solve it ?

thanks
vincent

cartagena68

  • issues
  • Hero Member
  • *
  • Posts: 1198
Re: Cities dropdown based on Region Select
« Reply #46 on: December 16, 2013, 06:36:51 pm »
HI,
for item-post you can look here

http://doc.osclass.org/Changing_between_drop-down_and_autocomplete_for_locations

i don't know for search

yellow53

  • Full Member
  • ***
  • Posts: 142
Re: Cities dropdown based on Region Select
« Reply #47 on: December 16, 2013, 09:51:47 pm »
Hi cartagena68,

I did this, then I got drop down menu, which is fine...but doesnt display cities based on the selected region.....it shows all the cities.

many thanks,
vincent

DjNavel

  • Newbie
  • *
  • Posts: 18
Re: Cities dropdown based on Region Select
« Reply #48 on: December 22, 2013, 08:29:06 pm »
fantastic!!!!

Code: [Select]
<?php  $conn getConnection(); $aStates $conn->osc_dbFetchResults("SELECT * FROM %st_city "DB_TABLE_PREFIX); ?>
      <?php if (count($aStates) >= ) {?>
      <?php  foreach($aStates as $state) { ?>
<?php $paese $state['fk_c_country_code']; ?> 
     <?php ?><?php ?>
<form name="modulo" action="<?php echo osc_base_url(true) ; ?>" method="get" class="search" >

    <input type="hidden" name="page" value="search" />
    <fieldset class="main">
   
   
                                      <select style="width:300px;"  name="sCategory" id="sCategory" class="" >
                                      <option value="">Select a category</option>
                                            <?php // RESET CATEGORIES IF WE USED THEN IN THE HEADER ?>
                                            <?php osc_goto_first_category() ; ?>
                                            <?php while(osc_has_categories()) { ?>
              <option id="cat<?php echo osc_category_id(); ?>" value="<?php echo osc_category_id(); ?>"<?php if(Params::getParam('sCategory') == osc_category_id()) { ?>selected<?php ?>><?php echo osc_category_name(); ?></option>
                                               
                                            <?php ?>
                                       </select>
                                   
                               
        <?php $aRegions Region::newInstance()->getByCountry($paese); ?>
<?php  if(count($aRegions) >= ) { ?>
    <select id="sRegion" name="sRegion"  style="width: 280px;" class="">
      <option value="">Select a State</option>
      <?php  foreach($aRegions as $region) { ?>
      <option id="idregioni"  value="<?php echo $region['pk_i_id']; ?>"<?php if(Params::getParam('sRegion') == $region['pk_i_id']) { ?>selected<?php ?>><?php echo $region['s_name'] ; ?></option>
<?php ?>
  </select><?php ?>
     
   <?php $regionId $_GET[sRegion]; ?>
     <?php  $conn getConnection(); $aCities $conn->osc_dbFetchResults("SELECT * FROM %st_city WHERE fk_i_region_id = '$regionId'"DB_TABLE_PREFIX); ?>
     <?php if (count($aCities) >= ) {?>
     
  <select  style="width:280px;"  name="sCity" id="sCity" class="">
      <option value="">Select a State First</option>
     
   <?php  foreach($aCities as $city) { ?>     
        <option value="<?php echo $city['pk_i_id']; ?>"<?php if(Params::getParam('sCity') == $city['pk_i_id']) { ?>selected<?php ?>><?php echo $city['s_name'] ; ?></option>
<?php ?>
   </select>
<?php ?>

<button type="submit"  onmouseover="this.style.cursor='pointer';" class="" style="height:26px;" >Search</button>
                   
                                   
    </fieldset>
</form>

How to make multiple select city?

Code: [Select]
<select  style="width:280px;"  name="sCity[]" id="sCity" class="" multiple>
it works fine, but I can not do as in Example

http://www.ryancramer.com/projects/asmselect/examples/example1.html

Help me, please

Ramesh

  • Jr. Member
  • **
  • Posts: 61
Re: Cities dropdown based on Region Select
« Reply #49 on: January 03, 2014, 09:31:44 pm »
this has been solved by me . if any one need help . ping me .. i will reply u ..

mkmobi

  • Full Member
  • ***
  • Posts: 119
Re: Cities dropdown based on Region Select
« Reply #50 on: March 03, 2014, 12:59:07 pm »


How to use the code for item-post.php (bcute theme)


Hi,
you can try this form together with the javascript i will post to see if is working.
 I HAVE ONLY CATEGORY, NOT SUB CATEGORY, so may be you need to change the category part.
Also i don't have the text box where you put what you are looking for, but i think that's easy to add.

REMEMBER TO KEEP A ORIGINAL COPY OF INC.SEARCH.PHP BEFORE YOU MAKE ANY CHANGE.

FORM CODE
Code: [Select]
<?php  $conn getConnection(); $aStates $conn->osc_dbFetchResults("SELECT * FROM %st_city "DB_TABLE_PREFIX); ?>
      <?php if (count($aStates) >= ) {?>
      <?php  foreach($aStates as $state) { ?>
<?php $paese $state['fk_c_country_code']; ?> 
     <?php ?><?php ?>
<form name="modulo" action="<?php echo osc_base_url(true) ; ?>" method="get" class="search" >

    <input type="hidden" name="page" value="search" />
    <fieldset class="main">
   
   
                                      <select style="width:300px;"  name="sCategory" id="sCategory" class="" >
                                      <option value="">Select a category</option>
                                            <?php // RESET CATEGORIES IF WE USED THEN IN THE HEADER ?>
                                            <?php osc_goto_first_category() ; ?>
                                            <?php while(osc_has_categories()) { ?>
              <option id="cat<?php echo osc_category_id(); ?>" value="<?php echo osc_category_id(); ?>"<?php if(Params::getParam('sCategory') == osc_category_id()) { ?>selected<?php ?>><?php echo osc_category_name(); ?></option>
                                               
                                            <?php ?>
                                       </select>
                                   
                               
        <?php $aRegions Region::newInstance()->getByCountry($paese); ?>
<?php  if(count($aRegions) >= ) { ?>
    <select id="sRegion" name="sRegion"  style="width: 280px;" class="">
      <option value="">Select a State</option>
      <?php  foreach($aRegions as $region) { ?>
      <option id="idregioni"  value="<?php echo $region['pk_i_id']; ?>"<?php if(Params::getParam('sRegion') == $region['pk_i_id']) { ?>selected<?php ?>><?php echo $region['s_name'] ; ?></option>
<?php ?>
  </select><?php ?>
     
   <?php $regionId $_GET[sRegion]; ?>
     <?php  $conn getConnection(); $aCities $conn->osc_dbFetchResults("SELECT * FROM %st_city WHERE fk_i_region_id = '$regionId'"DB_TABLE_PREFIX); ?>
     <?php if (count($aCities) >= ) {?>
     
  <select  style="width:280px;"  name="sCity" id="sCity" class="">
      <option value="Select a City">Select a State First</option>
     
   <?php  foreach($aCities as $city) { ?>     
        <option value="<?php echo $city['pk_i_id']; ?>"<?php if(Params::getParam('sCity') == $city['pk_i_id']) { ?>selected<?php ?>><?php echo $city['s_name'] ; ?></option>
<?php ?>
   </select>
<?php ?>

<button type="submit"  onmouseover="this.style.cursor='pointer';" class="" style="height:26px;" >Search</button>
                   
                                   
    </fieldset>
</form>

JAVASCRIPT CODE
Code: [Select]
<script>
$(document).ready(function() {
   
        $("#sRegion").live("change",function(){
            var pk_c_code = $(this).val();
            <?php if($path=="admin") { ?>
                var url = '<?php echo osc_admin_base_url(true)."?page=ajax&action=cities&regionId="?>' + pk_c_code;
            <?php } else { ?>
                var url = '<?php echo osc_base_url(true)."?page=ajax&action=cities&regionId="?>' + pk_c_code;
            <?php }; ?>

            var result = '';

            if(pk_c_code != '') {
               
                $("#sCity").attr('disabled',false);
                $.ajax({
                    type: "GET",
                    url: url,
                    dataType: 'json',
                    success: function(data){
                        var length = data.length;
                        if(length > 0) {
                            result += '<option selected value=""><?php _e("Select a city..."); ?></option>';
                            for(key in data) {
                                result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
                            }

                            $("#city").before('<select name="sCity" id="sCity" ></select>');
                            $("#city").remove();
                        } else {
                            result += '<option value=""><?php _e('No results'?></option>';
                            $("#sCity").before('<input type="text" name="city" id="city" />');
                            $("#sCity").remove();
                        }
                        $("#sCity").html(result);
                    }
                 });
             } else {
                $("#sCity").attr('disabled',true);
             }
        });

        if( $("#sRegion").attr('value') == "")  {
            $("#sCity").attr('disabled',true);
        }

       
});
 </script>


The script select 2 is on github you can find some examples here

http://ivaynberg.github.com/select2/

Let me know if is working for you

serjuc11111

  • Hero Member
  • *****
  • Posts: 814
Re: Cities dropdown based on Region Select
« Reply #51 on: March 08, 2014, 12:30:02 am »
finally what is the solution to reset city for item-post ?

thx

jujes

  • Newbie
  • *
  • Posts: 1
Re: Cities dropdown based on Region Select
« Reply #52 on: March 26, 2014, 07:04:44 pm »
Hi, I need load the Region by default when I coming from the main.php, in my case when I click the map image.
I use this script into sidebar "search.php" Attach captures, please help me :)
Thanks in advance,
 

amoric69

  • Full Member
  • ***
  • Posts: 185
Re: Cities dropdown based on Region Select
« Reply #53 on: March 28, 2014, 01:34:51 pm »
Hello,
Do you place inc.search.php in /oc-includes/osclass/gui/inc.search.php ?
In which directory do you place the select2 folder ? Root osclass or theme's root ?

In all the cases that does not function.  ???
 I use 3.3.2 version and Bender theme. I have not parent category.
Also, in form search, in the bender/header.php
I added :

<form action="<?php echo osc_base_url(true); ?>" method="get" class="search nocsrf" <?php /* onsubmit="javascript:return doSearch();"*/ ?>>
        <input type="hidden" name="page" value="search"/>
        <div class="main-search">
            <div class="cell">
                <input type="text" name="sPattern" id="query" class="input-text" value="" placeholder="<?php echo osc_esc_html(__(osc_get_preference('keyword_placeholder', 'bender_theme'), 'bender')); ?>" />
            </div>
            <?php  if ( osc_count_categories() ) { ?>
                <div class="cell selector">
                    <?php osc_categories_select('sCategory', null, __('Select a category', 'bender')) ; ?>
                </div>
               
               <div class="cell selector">
                    <?php /*item_city_box(__('city', 'bender'), __('Select a city...', 'bender')) ;*/ ?>
         <?php /*item_city_area(__('cityarea', 'bender'), __('Select a cityarea...', 'bender')) ;*/ ?>
                </div>

               
                <div class="cell reset-padding">
            <?php  } else { ?>
                <div class="cell">
            <?php  } ?>
           
                <button class="ui-button ui-button-big js-submit" style="background-color:#000000"><?php _e("Search", 'bender');?></button>
            </div>
        </div>
        <div id="message-seach"></div>
    </form>


But Ihave a fatal error, why ?

fre2mansur

  • Hero Member
  • *****
  • Posts: 711
Re: Cities dropdown based on Region Select
« Reply #54 on: October 20, 2014, 05:55:08 pm »
with country updated

Code: [Select]
<form action="<?php echo osc_base_url(true); ?>" method="get" class="nocsrf">
                    <input type="hidden" name="page" value="search"/>
                           
                               
                <?php  $conn getConnection(); $aStates $conn->osc_dbFetchResults("SELECT * FROM %st_country "DB_TABLE_PREFIX); ?>
      <?php if (count($aStates) >= ) {?>
      <select id="countryId" name="sCountry">
      <option value="">Select a Country</option>
      <?php  foreach($aStates as $state) { ?>
       <option value="<?php echo $state['pk_c_code']; ?>"><?php echo $state['s_name'] ; ?></option>
<?php ?>
</select>
<?php ?>
     
     
     

   
<?php  if(count($aRegions) >= ) { ?>
    <select id="regionId" name="sRegion"  style="width: 280px;">
      <option value="">Select a State</option>
      <?php  foreach($aRegions as $region) { ?>
      <option id="idregioni"  value="<?php echo $region['pk_i_id']; ?>"<?php if(Params::getParam('sRegion') == $region['pk_i_id']) { ?>selected<?php ?>><?php echo $region['s_name'] ; ?></option>
<?php ?>
  </select><?php ?>
     
 
     <?php if (count($aCities) >= ) {?>
      <select  style="width:280px;"  name="sCity" id="cityId">
      <option value="Select a City">Select a State First</option>
     
   <?php  foreach($aCities as $city) { ?>     
        <option value="<?php echo $city['pk_i_id']; ?>"<?php if(Params::getParam('sCity') == $city['pk_i_id']) { ?>selected<?php ?>><?php echo $city['s_name'] ; ?></option>
<?php ?>
   </select>
<?php ?>   
                               
           <button class="btn btn-primary" type="submit" >Search</button>                   
    </form>



<script>
$(document).ready(function(){
        $("#countryId").live("change",function(){
            var pk_c_code = $(this).val();
            <?php if($path=="admin") { ?>
                var url = '<?php echo osc_admin_base_url(true)."?page=ajax&action=regions&countryId="?>'   pk_c_code;
            <?php } else { ?>
                var url = '<?php echo osc_base_url(true)."?page=ajax&action=regions&countryId="?>'   pk_c_code;
            <?php }; ?>
            var result = '';

            if(pk_c_code != '') {

                $("#regionId").attr('disabled',false);
                $("#cityId").attr('disabled',true);

                $.ajax({
                    type: "POST",
                    url: url,
                    dataType: 'json',
                    success: function(data){
                        var length = data.length;
                       
                        if(length > 0) {

                            result  = '<option value=""><?php _e("Select a region..."); ?></option>';
                            for(key in data) {
                                result  = '<option value="'   data[key].pk_i_id   '">'   data[key].s_name   '</option>';
                            }

                            $("#region").before('<select name="regionId" id="regionId" ></select>');
                            $("#region").remove();

                            $("#city").before('<select name="cityId" id="cityId" ></select>');
                            $("#city").remove();
                           
                            $("#regionId").val("");

                        } else {

                            $("#regionId").before('<input type="text" name="region" id="region" />');
                            $("#regionId").remove();
                           
                            $("#cityId").before('<input type="text" name="city" id="city" />');
                            $("#cityId").remove();
                           
                        }

                        $("#regionId").html(result);
                        $("#cityId").html('<option selected value=""><?php _e("Select a city..."); ?></option>');
                    }
                 });

             } else {

                 // add empty select
                 $("#region").before('<select name="regionId" id="regionId" ><option value=""><?php _e("Select a region..."); ?></option></select>');
                 $("#region").remove();
                 
                 $("#city").before('<select name="cityId" id="cityId" ><option value=""><?php _e("Select a city..."); ?></option></select>');
                 $("#city").remove();

                 if( $("#regionId").length > 0 ){
                     $("#regionId").html('<option value=""><?php _e("Select a region..."); ?></option>');
                 } else {
                     $("#region").before('<select name="regionId" id="regionId" ><option value=""><?php _e("Select a region..."); ?></option></select>');
                     $("#region").remove();
                 }
                 if( $("#cityId").length > 0 ){
                     $("#cityId").html('<option value=""><?php _e("Select a city..."); ?></option>');
                 } else {
                     $("#city").before('<select name="cityId" id="cityId" ><option value=""><?php _e("Select a city..."); ?></option></select>');
                     $("#city").remove();
                 }
                 $("#regionId").attr('disabled',true);
                 $("#cityId").attr('disabled',true);
             }
        });

        $("#regionId").live("change",function(){
            var pk_c_code = $(this).val();
            <?php if($path=="admin") { ?>
                var url = '<?php echo osc_admin_base_url(true)."?page=ajax&action=cities&regionId="?>'   pk_c_code;
            <?php } else { ?>
                var url = '<?php echo osc_base_url(true)."?page=ajax&action=cities&regionId="?>'   pk_c_code;
            <?php }; ?>

            var result = '';

            if(pk_c_code != '') {
               
                $("#cityId").attr('disabled',false);
                $.ajax({
                    type: "POST",
                    url: url,
                    dataType: 'json',
                    success: function(data){
                        var length = data.length;
                        if(length > 0) {
                            result  = '<option selected value=""><?php _e("Select a city..."); ?></option>';
                            for(key in data) {
                                result  = '<option value="'   data[key].pk_i_id   '">'   data[key].s_name   '</option>';
                            }

                            $("#city").before('<select name="cityId" id="cityId" ></select>');
                            $("#city").remove();
                        } else {
                            result  = '<option value=""><?php _e('No results'?></option>';
                            $("#cityId").before('<input type="text" name="city" id="city" />');
                            $("#cityId").remove();
                        }
                        $("#cityId").html(result);
                    }
                 });
             } else {
                $("#cityId").attr('disabled',true);
             }
        });

        if( $("#regionId").attr('value') == "")  {
            $("#cityId").attr('disabled',true);
        }

        if($("#countryId").length != 0) {
            if( $("#countryId").attr('type').match(/select-one/) ) {
                if( $("#countryId").attr('value') == "")  {
                    $("#regionId").attr('disabled',true);
                }
            }
        }
});
 </script>

wade421

  • Newbie
  • *
  • Posts: 5
Re: Cities dropdown based on Region Select
« Reply #55 on: March 27, 2015, 10:03:23 am »
Hi, I found a malfunction here, and just want to check if it's just at my site or not.

When I search with this code the results are showing less listings, than if I will search with original drop down...
Not even selecting a city, for some regions I have 10% of total listings.

Also I've noticed that the address in the bar will contain digits for region and city, while with the original it's just words.
For example, was searching by DC region from dropdown menu with subject code, not selecting a city, got ZERO results:
http://XXXXXXX.com/index.php?page=search&CSRFName=CSRF686213058_1900406726&CSRFToken=2d17bb9a1ae16ce63ac22f094ecab434137519a41192962b42bcf8bebc532dd8b1cc36af1537b2b3392abee51408118882d1609f5d6287d36b20deb59782c3d0&sCategory=96&sRegion=782096

And the same DC inquiry with the original code brings to all 8 listings:
http://XXXXXXX.com/index.php?page=search&CSRFName=CSRF686213058_1900406726&CSRFToken=2d17bb9a1ae16ce63ac22f094ecab434137519a41192962b42bcf8bebc532dd8b1cc36af1537b2b3392abee51408118882d1609f5d6287d36b20deb59782c3d0&sCategory=96&sRegion=DC

So I think that the way this code inquiries about the region is the problem. I've tried to combine original code and this, spent 3 hours with no luck..
Can we fix it somehow, please? ::) ::) ::)

Here is the original code:
Code: [Select]
<form action="<?php echo osc_base_url(true) ; ?>" method="get" class="search"
onsubmit="javascript:return doSearch();"  />
    <input type="hidden" name="page" value="search" />
    <fieldset class="main">

        <?php  if ( osc_count_categories() ) { ?>
            <?php osc_categories_select('sCategory'null,  __('Select Size''osshaft_bender')) ; ?>
<br> <br>
                        <?php $aRegions Region :: newInstance()->listAll();?>

        <?php if (count($aRegions) > 0) {?>
        <select name="sRegion" id="sRegion">
        <option value="">Select State &nbsp; &nbsp; &nbsp; &nbsp</option>
        <?php foreach ($aRegions as $region) {?>
        <option  value="<?php echo $region['s_name'];?>"><?php echo $region['s_name'];?></option>
<?php ?>
</select>
<br> <br>
<?php ?>
<?php $aCities City::newInstance()->listAll(); ?>
<?php if(count($aCities) > ) { ?>
<select name="sCity" id="sCity" >
<option value="">Select City</option>
<?php foreach($aCities as $City) { ?>
<option value="<?php echo $City['s_name'] ; ?>"><?php echo $City['s_name'] ; ?></option>
        <?php }?>
        </select>
<br> <br>
<strong>&nbsp; Price &nbsp;</strong>
<br> <br>
        <?php }?>
 <input type="text" id="priceMin" name="sPriceMin" value="" size="12"
maxlength="8" ><span class="style1">&nbsp Min</span>
<br> <br>                               
 <input type="text" id="priceMax" name="sPriceMax" value="" size="12"
maxlength="8" ><span class="style1">&nbsp Max </span>                               
<?php }?>
<br> <br>


        </div>
       <button type="submit" class="btn btn-primary fullwidth"><?php _e('Search''bender') ; ?></button>
    </fieldset>
    <div id="search-example"></div>

</form>

lucato

  • Full Member
  • ***
  • Posts: 182
  • [<o>] Brasil
Re: Cities dropdown based on Region Select
« Reply #56 on: July 02, 2015, 03:02:52 pm »
Hi Catagena and folks, I'm very newbie with programming. I trying to use your code into the registration moment (user-register.php). The city/region selection works wonderful for Brazil, but by adding your code, after registering, when I check/edit the user on the back-end, the Region and City aren't saved into the user register. All other data are saved there, unless Region and City.

So, any help on your code below to make it saves the picked region and city into the database (user register)?

Thanks.

Script:
Code: [Select]

                        <!-- --------------------- INICIO: Código para seleção de cidade baseada no estado ---------------------- -->
                       
                        <script>
$(document).ready(function() {
   
           $("#sRegion").live("change",function(){
            var pk_c_code = $(this).val();
            <?php if($path=="admin") { ?>
                var url = '<?php echo osc_admin_base_url(true)."?page=ajax&action=cities&regionId="?>'   pk_c_code;
            <?php } else { ?>
                var url = '<?php echo osc_base_url(true)."?page=ajax&action=cities&regionId="?>'   pk_c_code;
            <?php }; ?>


            var result = '';


            if(pk_c_code != '') {
               
                $("#sCity").attr('disabled',false);
                $.ajax({
                    type: "GET",
                    url: url,
                    dataType: 'json',
                    success: function(data){
                        var length = data.length;
                        if(length > 0) {
                            result  = '<option selected value=""><?php _e("Selecione uma cidade..."); ?></option>';
                            for(key in data) {
                                result  = '<option value="'   data[key].pk_i_id   '">'   data[key].s_name   '</option>';
                            }


                            $("#city").before('<select name="sCity" id="sCity" ></select>');
                            $("#city").remove();
                        } else {
                            result  = '<option value=""><?php _e('Sem resultados'?></option>';
                            $("#sCity").before('<input type="text" name="city" id="city" />');
                            $("#sCity").remove();
                        }
                        $("#sCity").html(result);
                    }
                 });
             } else {
                $("#sCity").attr('disabled',true);
             }
        });


        if( $("#sRegion").attr('value') == "")  {
            $("#sCity").attr('disabled',true);
        }


       
});
 </script>
   


PHP Code:
Code: [Select]
   <!-- ----------------------------------- END OF SCRIPT STARTING OF PHP INSTRUCTIONS CITY/REGIONS----------------------------------------- >
                   


         <?php  $conn getConnection(); $aStates $conn->osc_dbFetchResults("SELECT * FROM %st_city "DB_TABLE_PREFIX); ?>
      <?php if (count($aStates) >= ) {?>
      <?php  foreach($aStates as $state) { ?>
    <?php $paese $state['fk_c_country_code']; ?> 
     <?php ?><?php ?>
<form name="modulo" action="<?php echo osc_base_url(true) ; ?>" method="get" class="search" >


    <input type="hidden" name="page" value="search" />
    <!-- <fieldset class="main"> -->
                                                                         
                               
        <?php $aRegions Region::newInstance()->getByCountry($paese); ?>
<?php  if(count($aRegions) >= ) { ?>
    <label for="region"><?php _e('Estado''brasil'); ?></label>
    <select id="sRegion" name="sRegion"  style="width: 280px;" class="">
      <option value="">Selecione um Estado</option>
      <?php  foreach($aRegions as $region) { ?>
      <option id="idregioni"  value="<?php echo $region['pk_i_id']; ?>"<?php if(Params::getParam('sRegion') == $region['pk_i_id']) { ?>selected<?php ?>><?php echo $region['s_name'] ; ?></option>
    <?php ?>
     </select><?php ?>
        <?php $regionId $_GET[sRegion]; ?>
     <?php  $conn getConnection(); $aCities $conn->osc_dbFetchResults("SELECT * FROM %st_city WHERE fk_i_region_id = '$regionId'"DB_TABLE_PREFIX); ?>
     <?php if (count($aCities) >= ) {?><br><br/>
         <label for="city"><?php _e('City''brasil'); ?></label>
     <select  style="width:280px;"  name="sCity" id="sCity" class="">
      <option value="Select a City">Selecione o ^ Estado ^ primeiro</option><br><br/>
     
   <?php  foreach($aCities as $city) { ?>     
        <option value="<?php echo $city['pk_i_id']; ?>"<?php if(Params::getParam('sCity') == $city['pk_i_id']) { ?>selected<?php ?>><?php echo $city['s_name'] ; ?></option>
   <?php ?>
   </select>
<?php ?>


                   
<!--                                                     
                        --------------------------- FIM DO CÓDIGO Cidade baseada estado ------------------------------------------- -->

Anyhelp will be appreciated, thanks.
« Last Edit: July 02, 2015, 11:26:13 pm by lucato »

Adyyda

  • Sr. Member
  • ****
  • Posts: 435
Re: Cities dropdown based on Region Select
« Reply #57 on: February 28, 2016, 02:42:54 pm »
Hi,
you can try this form together with the javascript i will post to see if is working.
 I HAVE ONLY CATEGORY, NOT SUB CATEGORY, so may be you need to change the category part.
Also i don't have the text box where you put what you are looking for, but i think that's easy to add.

REMEMBER TO KEEP A ORIGINAL COPY OF INC.SEARCH.PHP BEFORE YOU MAKE ANY CHANGE.

FORM CODE
Code: [Select]
<?php  $conn getConnection(); $aStates $conn->osc_dbFetchResults("SELECT * FROM %st_city "DB_TABLE_PREFIX); ?>
      <?php if (count($aStates) >= ) {?>
      <?php  foreach($aStates as $state) { ?>
<?php $paese $state['fk_c_country_code']; ?> 
     <?php ?><?php ?>
<form name="modulo" action="<?php echo osc_base_url(true) ; ?>" method="get" class="search" >

    <input type="hidden" name="page" value="search" />
    <fieldset class="main">
   
   
                                      <select style="width:300px;"  name="sCategory" id="sCategory" class="" >
                                      <option value="">Select a category</option>
                                            <?php // RESET CATEGORIES IF WE USED THEN IN THE HEADER ?>
                                            <?php osc_goto_first_category() ; ?>
                                            <?php while(osc_has_categories()) { ?>
              <option id="cat<?php echo osc_category_id(); ?>" value="<?php echo osc_category_id(); ?>"<?php if(Params::getParam('sCategory') == osc_category_id()) { ?>selected<?php ?>><?php echo osc_category_name(); ?></option>
                                               
                                            <?php ?>
                                       </select>
                                   
                               
        <?php $aRegions Region::newInstance()->getByCountry($paese); ?>
<?php  if(count($aRegions) >= ) { ?>
    <select id="sRegion" name="sRegion"  style="width: 280px;" class="">
      <option value="">Select a State</option>
      <?php  foreach($aRegions as $region) { ?>
      <option id="idregioni"  value="<?php echo $region['pk_i_id']; ?>"<?php if(Params::getParam('sRegion') == $region['pk_i_id']) { ?>selected<?php ?>><?php echo $region['s_name'] ; ?></option>
<?php ?>
  </select><?php ?>
     
   <?php $regionId $_GET[sRegion]; ?>
     <?php  $conn getConnection(); $aCities $conn->osc_dbFetchResults("SELECT * FROM %st_city WHERE fk_i_region_id = '$regionId'"DB_TABLE_PREFIX); ?>
     <?php if (count($aCities) >= ) {?>
     
  <select  style="width:280px;"  name="sCity" id="sCity" class="">
      <option value="Select a City">Select a State First</option>
     
   <?php  foreach($aCities as $city) { ?>     
        <option value="<?php echo $city['pk_i_id']; ?>"<?php if(Params::getParam('sCity') == $city['pk_i_id']) { ?>selected<?php ?>><?php echo $city['s_name'] ; ?></option>
<?php ?>
   </select>
<?php ?>

<button type="submit"  onmouseover="this.style.cursor='pointer';" class="" style="height:26px;" >Search</button>
                   
                                   
    </fieldset>
</form>

JAVASCRIPT CODE
Code: [Select]
<script>
$(document).ready(function() {
   
        $("#sRegion").live("change",function(){
            var pk_c_code = $(this).val();
            <?php if($path=="admin") { ?>
                var url = '<?php echo osc_admin_base_url(true)."?page=ajax&action=cities&regionId="?>' + pk_c_code;
            <?php } else { ?>
                var url = '<?php echo osc_base_url(true)."?page=ajax&action=cities&regionId="?>' + pk_c_code;
            <?php }; ?>

            var result = '';

            if(pk_c_code != '') {
               
                $("#sCity").attr('disabled',false);
                $.ajax({
                    type: "GET",
                    url: url,
                    dataType: 'json',
                    success: function(data){
                        var length = data.length;
                        if(length > 0) {
                            result += '<option selected value=""><?php _e("Select a city..."); ?></option>';
                            for(key in data) {
                                result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
                            }

                            $("#city").before('<select name="sCity" id="sCity" ></select>');
                            $("#city").remove();
                        } else {
                            result += '<option value=""><?php _e('No results'?></option>';
                            $("#sCity").before('<input type="text" name="city" id="city" />');
                            $("#sCity").remove();
                        }
                        $("#sCity").html(result);
                    }
                 });
             } else {
                $("#sCity").attr('disabled',true);
             }
        });

        if( $("#sRegion").attr('value') == "")  {
            $("#sCity").attr('disabled',true);
        }

       
});
 </script>


The script select 2 is on github you can find some examples here

http://ivaynberg.github.com/select2/

Let me know if is working for you

Hello. Seems that your code works in any theme (i tried modern and twitter so far) and you are wright when you say that is working in your site but others are wright as well when they tell that city selection is lost after performing previous search.
The difference is made by permalinks activated or not. If they are not activated, dropdown works perfect, both selections are kept. If you activate permalinks, region selection is not kept.

So, any solution to fix this issue? Thnks

vivekshan

  • Jr. Member
  • **
  • Posts: 70
Re: Cities dropdown based on Region Select
« Reply #58 on: July 03, 2018, 11:29:52 pm »
some days before i saw a website oozz.in and i like that pick your city dropdown menu
if you select city as hyderabad then hyderabad will we shown instead of pick your city

i am a leaner and dont have much knowledge about javascript
so please guide me how to that
i want to setup a dropdown menu in header for subdomains (cities and regions)
please help