Advertisement:

Author Topic: How to default the dropdown value  (Read 362 times)

wave333

  • Newbie
  • *
  • Posts: 4
How to default the dropdown value
« on: January 14, 2019, 07:05:13 am »
Hi

I'm assuming it is a simple questions as I'm new to osclass, i'm trying to default the value of the dropdown box- Region on my theme. Currently, i'm able to select all the regions that are available from the dropdown box, but I would like to default the value to one and make the drop down still selectable to different value. For example. the dropdown has values Yellow, Orange, Blue, Red, I want to show the default value to Blue when page loads. This is the code I found on theme that is creating the dropdown entries:
<?p
.....
ItemForm::region_select(osc_get_regions(),osc_user());
?>

Any help or suggestions is greatly appreciated.

calinbehtuk

  • Sr. Member
  • ****
  • Posts: 450
Re: How to default the dropdown value
« Reply #1 on: January 14, 2019, 11:05:27 am »
My question for you, you use one country?
The region selector is populated with region after page is load or you have to select the country first?

wave333

  • Newbie
  • *
  • Posts: 4
Re: How to default the dropdown value
« Reply #2 on: January 14, 2019, 06:20:19 pm »
@calinbehtuk Thanks for your quick reply. Basically, only one country is setup which is US. This is shown as text field before the region like this -
<div class="controls">
<?php
 ItemForm::country_text(osc_get_countries(), osc_user());
?>
</div>

When the page loads the country field is greyed out and it displays United States.

calinbehtuk

  • Sr. Member
  • ****
  • Posts: 450
Re: How to default the dropdown value
« Reply #3 on: January 14, 2019, 07:23:13 pm »
This is only an example tested in bender theme,  you need to work more on this code.
This will work if the region field is populated, bender theme has a function that will populate the regions if  you have only  one country on site.

Original code
Code: [Select]
ItemForm::region_select($aRegions, osc_user());
Changed code
Code: [Select]
ItemForm::region_select($aRegions, array('fk_i_region_id'=> '782162'));
I just added an array with the id of the region that i want to be selected by default.

You need to work on this code, but as an idea is possible what you want. You need to take in consideration if the user is logged, to allow the user location instead of your location.

wave333

  • Newbie
  • *
  • Posts: 4
Re: How to default the dropdown value
« Reply #4 on: January 14, 2019, 08:55:32 pm »
@calinbehtuk It works perfectly  :) :). I can see the region field auto populated with the region_id nbr that I choose when the page loads. However, the city field below it seems to take the value of the first available region from the drop down box instead of 782162. When i change region again on the page to any thing else, the city values changes as expected.  This is the code for the city field -

<div class="controls">
<?php
 ItemForm::city_select(osc_get_cities(osc_user_region_id()), osc_user()) ;
?>
</div>

calinbehtuk

  • Sr. Member
  • ****
  • Posts: 450
Re: How to default the dropdown value
« Reply #5 on: January 14, 2019, 09:04:21 pm »
I give you a simple solution, maybe is not the best but this do the job. Again you have to work to this code, to take in consideration logged users and to load this only on post page.

Code: [Select]
                            <script type="text/javascript">
                                $(document).ready(function () {
                                    $('#regionId').change();
                                });
                            </script>

wave333

  • Newbie
  • *
  • Posts: 4
Re: How to default the dropdown value
« Reply #6 on: January 14, 2019, 09:23:43 pm »
That did the trick  :). Thanks again. And you are right, I need to check for logged users. I will work on that part too. I will close this thread as solved.

One thing you mentioned previously to check for user location instead of defaulting it, I like that idea. Could you provide me any example to do this? Also, I see lot of websites are using location services, how can we achieve that, plugins?


calinbehtuk

  • Sr. Member
  • ****
  • Posts: 450
Re: How to default the dropdown value
« Reply #7 on: January 14, 2019, 10:55:45 pm »

Code: [Select]
$oscUser = osc_user();
if (!osc_is_web_user_logged_in() && Params::getParam('action') != 'item_edit') {
    $oscUser = array('fk_i_region_id'=> '782162');
}
Code: [Select]
ItemForm::region_select($aRegions, $oscUser);
Code: [Select]
<?php if(!osc_is_web_user_logged_in() && Params::getParam('action') != 'item_edit'){ ?>
              <script type="text/javascript">
                    $(document).ready(function () {
                       $('#regionId').change();
                    });
              </script>
<?php ?>