javascript - PHP Populate Text Box from DropDown Selection -
i trying populate 2 text fields after user makes choice on drop down. code below works manual effort in function. need values form.accounts , form.client come query select box using. ideas. should jquery - not familiar it.
<?php $db = new mysqli("127.0.0.1", "root", "", "centrum"); $sql = "select accounts_client_accounts.accounts_client_account_id, accounts_client_accounts.accounts, accounts_client.client accounts_client_accounts inner join accounts_client on accounts_client_accounts.accounts_client_id = accounts_client.accounts_client_id order accounts_client_account_id "; if(!$result = $db->query($sql)){ die('there error running query [' . $db->error . ']'); }?> <form class="form-ui"> <span class="form-elements reqfield"> <select class="dropdown" onchange="populatedata(this)"> <option value="0">choose account</option> <?php while($row = $result->fetch_assoc()){ echo "<option value='". $row['accounts_client_account_id']."'>".$row['accounts']; } ?> </select> </span> <span class="form-elements"> <input name="accounts" type="text" class="text" value="" /> </span> <span class="form-elements reqfield"> <input name="client" type="text" class="text" value="" /> </span> </form> <script> function populatedata(sel){ var form = sel.form, value = sel.options[sel.selectedindex].value; switch(value){ case '1': form.accounts.value = 'account1'; form.client.value = 'client1'; break; case '2': form.accounts.value = 'account2'; form.client.value = 'client2'; break; case '3': form.accounts.value = 'account3'; form.client.value = 'client3'; break; default: } } </script>
you can use data attributes drop down options , populate fields` values form them. here code:
<form class="form-ui"> <span class="form-elements reqfield"> <select class="dropdown" onchange="populatedata(this)"> <option value="0">choose account</option> <?php while($row = $result->fetch_assoc()){ echo "<option data-account=".$row['accounts']." data-client=".$row['client']." value='". $row['accounts_client_account_id']."'>".$row['accounts']; } ?> </select> </span> <span class="form-elements"> <input name="accounts" type="text" class="text" value="" /> </span> <span class="form-elements reqfield"> <input name="client" type="text" class="text" value="" /> </span> </form> <script> function populatedata(sel){ var account = $(this).find(':selected').data('account'), client = $(this).find(':selected').data('client'); form.accounts.value = account; form.client.value = client; } } </script>
Comments
Post a Comment