简体   繁体   中英

Get selected value from drop down box and use to populate text box php javascript

I am Populating values in textbox based on listbox changed

My code is working ...and also getting data properly...

BUT This works on the basis of id.

I need to select data based on cityname.

below is my code

<?php
require_once('includes/config.php');
?>

<?php
$result1 = $database->getRows("SELECT * from areamaster");
?>
<html>
<head>

<script type="text/javascript">
    var compInfoArray = [
        <?php                       
           foreach($result1 as $row1){ 

                echo 'id : ' . $row1['id'] . ',';
                echo 'cityname :  "'.$row1['cityname'].'",';
                echo 'area : "'.$row1['area'].'"';
            }
        ?>
    ];

    function showname() {
  var id = document.form1.id.value;
  var index = compInfoArray.contains(id);
  document.form1.cityname.value = compInfoArray[index]["cityname"];
  document.form1.area.value = compInfoArray[index]["area"];
}

window.onload = function() {
  showname();
}


Array.prototype.contains = function(needle) {
  for (var i in this) {
    for (var key in this[i]) {
      if (this[i][key] == needle) {
        return i;
      }
    }
  }
  return false;
}
</script>

  </head>
</head>
<body>
<form name="form1">
  <select name="id" onChange="showname()">
<?php foreach($result1 as $row1){   ?>  
    <option  value="<?php echo $row1["cityname"]; ?>"><?php echo $row1["cityname"]; ?></option>
    <?php }?>       
  </select>
  <label>
    <input type="text" name="cityname" value="" />
    <input type="text" name="area" value="" />
  </label>

</form>
</body>
    </html>

I try to reproduce your case in a minimalistic .html :

<form name="form1">  

    <select name="id" onChange="showname()">

       <option  value="Paris">Paris</option>
       <option  value="London">London</option>
       <option  value="Berlin">Berlin</option>          

    </select>
    <label>
        <input type="text" name="cityname" value="" />
        <input type="text" name="area" value="" />
    </label>

</form>

So If I've understood correctly, this is the html you get.

Next, I rebuilt your js array a bit differently :

var compInfoArray = [
    {
        "id" : 5,
        "cityname" : "Paris",
        "area" : "FR"
    },
    {
        "id" : 8,
        "cityname" : "London",
        "area" : "UK"
    },
    {
        "id" : 12,
        "cityname" : "Berlin",
        "area" : "GER"
    }
];

Then I wrote a prototype that will find a value inside any subarray keys :

Array.prototype.contains = function (needle) 
{
   for (var i in this) 
   {
       for(var key in this[i]) 
       {
            if(this[i][key] == needle )
            {
                return i;
            }
        }
   }
   return false;
}

Finally your showname() function :

function showname() 
{
    var id = document.form1.id.value;
    var index = compInfoArray.contains(id);
    document.form1.cityname.value = compInfoArray[index]["cityname"];
    document.form1.area.value = compInfoArray[index]["area"];
}

Working snippet :

 var compInfoArray = [{ "id": 5, "cityname": "Paris", "area": "FR" }, { "id": 8, "cityname": "London", "area": "UK" }, { "id": 12, "cityname": "Berlin", "area": "GER" }]; function showname() { var id = document.form1.id.value; var index = compInfoArray.contains(id); document.form1.cityname.value = compInfoArray[index]["cityname"]; document.form1.area.value = compInfoArray[index]["area"]; } window.onload = function() { showname(); } Array.prototype.contains = function(needle) { for (var i in this) { for (var key in this[i]) { if (this[i][key] == needle) { return i; } } } return false; } 
 <form name="form1"> <select name="id" onChange="showname()"> <option value="Paris">Paris</option> <option value="London">London</option> <option value="Berlin">Berlin</option> </select> <label> <input type="text" name="cityname" value="" /> <input type="text" name="area" value="" /> </label> </form> 

EDIT : This is how you probably will have to build your array with your query results :

var compInfoArray = [

        <?php                       
            foreach($result1 as $row1)
            { 
                echo '{';
                echo 'id : ' . $row1['id'] . ',';
                echo 'cityname :  "'.$row1['cityname'].'",';
                echo 'area : "'.$row1['area'].'"';
                echo '},';
            }
        ?>
    ];

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