简体   繁体   中英

how do i create an objectArray in javascript for fetching values for a drop down based on another drop down

**I need an object array ( var objArray ) to be fetched from php .for drop down value selection ... as i have hardcoded var objArray as key will be from one drop down and value from another drop down .. i dont want to hardcode like this .. i want the key and value which is generated from php **

define('TIER_LIST', json_encode(
     array(
        '1' => 'Tier 1',
        '2' => 'Tier 2',
        '3' => 'Tier 3',
     )
 ));

  define('FEE_LIST', json_encode(
  array(
    '1' => 'Tier 1 -100 Rupees ',
    '2' => 'Tier 2 -200 Rupees',
    '3' => 'Tier 3 -300 Rupees',
  )
  ));

$tierList = json_decode(TIER_LIST,true);
$feeList = json_decode(FEE_LIST,true);
?>

<select class='browser-default selectField' id="TierSelection" onchange="onchangeFeeSelection()">
                        <option value="0">Choose your option</option>
                        <?php foreach($tierList as $value => $text) { ?>
                            <option value="<?php echo $value; ?>"><?php echo $text; ?></option>
                        <?php } ?>
                    </select>

<select class='browser-default selectField' id="FeeSelection">
                        <option value="">Choose your Fee option</option>
                   <?php foreach($feeList as $value => $text) { ?>
 <option value="<?php echo $value; ?>"><?php echo $text; ?></option>
                        <?php } ?>
                    </select>

  <input type = "text" id = "feeValue">   


var objArray = {'1' => 'Tier 1 -100 Rupees ',  
    '2' => 'Tier 2 -200 Rupees',
    '3' => 'Tier 3 -300 Rupees'}


$("#TierSelection").change(function()
{
var ddText = $(this).val();
$.each(objArray,function(key,value)
{
   if(ddText == key)
       $("#FeeSelection").val(value);
});
});




 var objArray = {'1' => 'Tier 1 -100 Rupees ',  
        '2' => 'Tier 2 -200 Rupees',
        '3' => 'Tier 3 -300 Rupees'}     this part i need to fetch key values  values from php
function onchangeFeeSelection(selectedTier)
{

   //define php info and make ajax call
     $.ajax({
         url: "getTierFee.php",
         type: "POST",
         data: { tier: selectedTier },
         cache: false,
         success: function (response) {
             $("#FeeSelection").val(value);
         }
     });
});

In getTierFee.php write the query to fetch the fee array based on the selected tier value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM