user2366467
user2366467

Reputation: 25

Fill Text field with second jquery and php

I need help with the code below. I am trying to populate a text field with the data from the row set, but I want to use another field within the results. So the dropdown gives me the company name and I want to populate text fields with street, and country, etc.

Thanks in advance

<form>
<select name="name" id="name" onchange="">
<?php
do {  
 ?>
  <option value="<?php echo $row_Recordset1['CustomerCompanyName']?>"<?php if (!(strcmp($row_Recordset1['CustomerCompanyName'],     ucwords($row_Recordset1['CustomerCompanyName'])))) {echo     "selected=\"selected\"";} ?>>
  <?php echo $row_Recordset1['CustomerCompanyName']." | ".$row_Recordset1['Street'] ?></option>
  <?php
} while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
  $rows = mysql_num_rows($Recordset1);
  if($rows > 0) {
      mysql_data_seek($Recordset1, 0);
      $row_Recordset1 = mysql_fetch_assoc($Recordset1);

  }

?>

</select>

<input name="firstname" id="firstname" type="text" />

<script type="application/javascript">

$(document).ready(function(){
    $('#name').on('change', function () {
        var selection = $(this).val();
    $('#firstname').val(selection);
    });
});

</script>

Upvotes: 0

Views: 49

Answers (1)

leoap
leoap

Reputation: 1729

Considering that you are storing the company data in the text of option, like this:

<option value="companyName">companyName|street|country</option>

Then you should get the selected option text, split it and use the fragmented data to populate the fields you need.

<form>
    <select name="name" id="name" onchange="">
            <option value="google">google|18st.|usa</option>
            <option value="facebook">facebook|25st.|usa</option>
            <option value="microsoft">microsoft|05st.|usa</option>
    </select>

    <input name="firstname" id="firstname" type="text" />
    <input name="street" id="street" type="text" />
    <input name="country" id="country" type="text" />
</form>
<script type="application/javascript">
    $(document).ready(function(){
        $('#name').on('change', function () {
            var companyData =  $(this).find("option:selected").text().split("|");

            var companyName = companyData[0];
            var street = companyData[1];
            var country = companyData[2];

            $('#firstname').val(companyName);
            $('#street').val(street);
            $('#country').val(country);
        });
    });
</script>

Upvotes: 1

Related Questions