Jump to content

Fetching results of dynamically generated dropdown?


lovephp

Recommended Posts

ok so this is how i generate state and towns name

 

 

<script type="text/javascript">
$(document).ready(function() {
    $('#wait_1').hide();
    $('#State').change(function(){
      $('#wait_1').show();
      $('#result_1').hide();
      $.get("state_towns.php", {
        func: "State",
        drop_var: $('#State').val()
      }, function(response){
        $('#result_1').fadeOut();
        setTimeout("finishAjax_1('result_1', '"+escape(response)+"')", 400);
      });
        return false;
    });
});

function finishAjax_1(id, response) {
  $('#wait_1').hide();
  $('#'+id).html(unescape(response));
  $('#'+id).fadeIn();
}
</script>
  <select name="State" id="State">
      <option value="" selected="selected" disabled="disabled">State</option>
      <?php getTowns(); ?>
    
    </select>
    
    <span id="wait_1" style="display: none;">
    <small><b>Loading...</b></small>
    </span>
    <span id="result_1" style="display: none;"></span>,

 

and here is the state_towns.php fike

 

<?php
function getTowns()
{
    $result = mysql_query("SELECT DISTINCT State FROM state_town")
    or die(mysql_error());

      while($tier = mysql_fetch_array( $result ))
 
        {
           echo '<option value="'.$tier['State'].'">'.$tier['State'].'</option>';
        }

}
if($_GET['func'] == "State" && isset($_GET['func'])) {
   state_dropdown($_GET['drop_var']);
}

function state_dropdown($drop_var)
{  
    include("manage/connect.php");
    $result = mysql_query("SELECT * FROM state_town WHERE State='$drop_var'")
    or die(mysql_error());
    
    echo '<select name="Town" id="Town">
          <option value="" disabled="disabled" selected="selected">Any Town</option>';

           while($town_dropdown = mysql_fetch_array( $result ))
            {
              echo '<option value="'.$town_dropdown['Towns'].'">'.$town_dropdown['Towns'].'</option>';
            }
    
    echo '</select> ';   
}
?>

 

in my update data page how could i fetch the state and town names which was stored already?

 

having a hard time figuring it out.

 

 

appreciate your help and time.

cheers

Link to comment
Share on other sites

the above code fetches results with no issues, but now im trying to make a edit.php page where i wish to fetch users saved data and there are fields as State and Town so how could i use the above code to display saved values? as of now after selecting the State name then the Town name dynamically loads

 

 

eg

 
$sql = mysql_query("SELECT * FROM posts WHERE userID='1'");
$row = mysql_fetch_array($result);
 
$state=$row['State'] ;
$town=$row['Town'] ;
 

 

the code above will have the saved value or the post right? so i want the results of my dropdown to also display the values of $state and $town.

Link to comment
Share on other sites

Oh ok, so in your edit script you will want to pass those values to the getTowns() and state_dropdown() functions. When the value of the <option> tag matches the state/town passed to the function you set the selected html attribute

 

First the getTowns function, get changed to. See comments in code 

// In edit page the state is pass as an argument. It will be the selected value in the dropdown menu
function getTowns($selected_state = null)
{
    $result = mysql_query("SELECT DISTINCT State FROM state_town")
    or die(mysql_error());

    while($tier = mysql_fetch_array( $result ))
    {
        // when the current state matches the state being passed, the selected attribute for the <option> tag will be set
        $selected = $tire['State'] == $selected_state ? ' selected="selected"' : '';
        echo '<option value="'.$tier['State'].'"'.$selected.'>'.$tier['State'].'</option>';
    }
}

When you call this function in your edit page, you pass $row['State'] as an argument  getTowns($row['State']);  on loading the edit page, it should have the state chosen whcih is stored in the database.

 

When calling the state_dropdown() function in your edit page you will pass $row['State'] and $row['town'] as arguments

<span id="result_1"><?php echo state_dropdown($row['State'], $row['Town']); ?></span>,

The function will be changed to

// the state and town are passed as arguments in the edit page
function state_dropdown($state, $town = null)
{  
    include("manage/connect.php");
    $result = mysql_query("SELECT * FROM state_town WHERE State='$state'")
    or die(mysql_error());

    // when a town has been passed, remove the selected attribute from the Any Town option
    echo '<select name="Town" id="Town">
          <option value="" disabled="disabled" ' . ($town != null ) ? 'selected="selected"' : '') .'>Any Town</option>';
        
        while($town_dropdown = mysql_fetch_array( $result ))
        {
            // if the current town matches the town passed to the function, set the selected attribute to the <option> tag
            $selected = ($row_dropdown['Towns'] == $town) ? ' selected="selected"' : '';
            echo '<option value="'.$town_dropdown['Towns'].'"'.$selected.'>'.$town_dropdown['Towns'].'</option>';
        }
    echo '</select> ';
}
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.