Jump to content

country-state dropdown AJAX


anujgarg

Recommended Posts

I have created a dynamically populating drop down menu. Like: choose a Country and then choose a state from that Country.

 

I have incorporated it in a form having some other fields. I have integrated this form with some validations. All I need to do is to put validation on country - state relationship that if form is posted and the validation arises, then the respective state should be selected as per the country.

 

In my case, country is being selected but state doesn't.

 

Here is my code to do so:

 

//Country Code

<select name="country" id="select" onChange="get_state(this.value)">

                <option>-- Select Country --</option>

 

                <?php foreach ($listAllCountry->result() as $country): ?>

                <option value="<?php echo $country->countries_id;?>" <?php echo $this->validation->set_select('country', $country->countries_id); if($country->countries_id == "222" || $country->countries_id == "223" || $country->countries_id == "38") { echo " style=\"font-weight:bold;\""; } ?>> <?php echo $country->countries_name;?> <?php if($country->countries_name == "Canada") echo "<option value=\"\" disabled=\"disabled\"> ---------------------------------- </option>"; ?> </option>

                <?php endforeach; ?>

              </select>

 

//State Code

<div id="statediv">

<select name="state" id="select">

  <option value="0">-- Select State --</option>

  <?php foreach ($listAllState->result() as $state): ?>

<option value="<?php echo $state->states_id;?>" <?php echo $this->validation->set_select('state', $state->states_id);?>><?php echo $state->states_name;?></option>

  <?php endforeach; ?>

</select>

</div>

 

 

//Function that is responsible to populate state drop-down and is being called in function get_state(this.value)

function statesearch()

{

$country_id = $this->uri->segment(3);

$query = $this->user_group_model->findState($country_id);

$output = "<select name=\"state\" id=\"state\" class=\"select141\">";

        $output .= $this->new_option('-- Select State --', '0', $country_id);

 

        if ($query->num_rows() > 0)

        {

            foreach ($query->result() as $row)

            {

$output .= $this->new_option($row->states_name, $row->states_id, $country_id);

            }

        }

 

    echo $output .= "</select'>";

}

 

Please suggest....

Link to comment
Share on other sites

I forgot to write a function. Here is it:

 

function new_option($text, $value, $value_cmp)

    {

$output = "<option value=\"" . $value . "\"";

 

if ($value === $value_cmp)

{

$output .= " selected";

}

 

$output .= ">" . $text . "</option>";

 

return ($output);

    }

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.