Fill textbox when click a button according to dropdown selection in php jQuery -
my process this: have dropdown menu , text box. when select id (unique id) dropdown , click submit button want display corresponding name text box.
my database fields :
id
(auto increment)agencyname_id
(unique id)- name
dispay.html
<select name="agencyid_dwn" class="idlookup_dwn" id="agencyid_dwn" > <option selected>...select...</option> <?php while($row = mysqli_fetch_array($result)){ ?> <option value="<?php echo $row['agencyname_id'];?>"> <?php echo $row['agencyname_id'];?></option> <?php } ?> </select> // input text <input type="text" id="testid"> // submit button <input type="submit" name="lookupsubmit">
dataget.php
<?php if (isset($_post["lookupsubmit"])) { $user_id=$_post['agencyid_dwn']; $query = "select * agencyhome agencyname_id = '$user_id'" ; $result=mysqli_query($db, $query); $data = mysqli_fetch_assoc($result); echo json_encode($data); exit(); } ?>
myjson.js
<script src="//code.jquery.com/jquery-1.11.2.min.js"> </script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"> </script> <script type="text/javascript"> $(document).ready(function(){ $('#agencyid_dwn').change(function(){ var reg_number = $(this).val(); var data_string; data_string = 'reg_number='+reg_number; $.post('dataget.php',data_string,function(data){ var data= jquery.parsejson(data); $('#testid').val(data.name); }); }); }); </script>
when click submit button got database results array in "dataget.php".but in textbox did not display result.any mistake in code?
here answer
your index.php
<?php $conn = mysqli_connect("localhost","root","","test_db"); ?> <!doctype> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <script src="//code.jquery.com/jquery-1.11.2.min.js"> </script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"> </script> <script type="text/javascript"> $(document).ready(function(){ $('#agencyid_dwn').change(function(){ var reg_number = $(this).val(); var data_string; data_string = 'reg_number='+reg_number; $.post('dataget.php',data_string,function(data){ console.log(data); var data= jquery.parsejson(data); $('#testid').val(data.name); }); }); }); </script>
<body> <form> <select name="agencyid_dwn" class="idlookup_dwn" id="agencyid_dwn" > <option selected>...select...</option> <?php $query = "select agencyname_id agencyname"; $result = mysqli_query($conn,$query); while($row = mysqli_fetch_array($result)){ ?> <option value="<?php echo $row['agencyname_id'];?>"> <?php echo $row['agencyname_id'];?></option> <?php } ?> </select> // input text <input type="text" id="testid"> // submit button <input type="submit" name="lookupsubmit"> </form> </body> </html>
and dataget.php file bellow
<?php $conn = mysqli_connect("localhost","root","","test_db"); $reg_number=$_post['reg_number']; $query = "select * agencyname agencyname_id = '$reg_number'" ; $result=mysqli_query($conn, $query); $data = mysqli_fetch_assoc($result); // print_r($data); echo json_encode($data); exit(); ?> ccheck table name , work
Comments
Post a Comment