javascript - changing a textbox value when selected index from a combobox -
i have combobox has data fetched database. when index selected combobox, textbox value gets value database depending on index selected combobox. have following codes.
<form action = '' method = "post"> <b> school id: </b><br /> <select name = "school_id" required id = "schoold_id" onchange="get_data(this.value)"> <option value = "" selected></option> <?php $sql = "select distinct school_id school"; $result = $db->query($sql); if ($result->num_rows > 0){ while($row = $result->fetch_assoc()){ echo "<option value = ".$row["school_id"].">".$row["school_id"]."</option>"; } } ?> </select><br /><br /> <b> batch number: </b><br /> <input type= "text" name = "batch_no" /required><br /><br /> <b> school name: </b><br /> <input type= "text" name = "school_name" /readonly><br /><br /> <input type = "submit" value = " submit "/><br /> </form>
the combobox here school id , textbox needs changed school name.
i tried using javascript , here used.
<script> function get_data(value){ $.ajax({ url: "ajax.php", type: "post", datatype: "html", async: false, data: {value: value} success: function(data) { //here can populate required fields based on value database } }); }</script>
and ajax file
<?php include "connect.php"; $myschoolid = (filter_input(input_post, 'school_id')); $sql = $db->prepare('select school_name main school_id = ?'); $sql->bind_param('i', $myschoolid); $sql->execute(); $result = $sql->get_result(); if ($result->numrows >0){ while ($row = $result->fetch_assoc()){ $newschoolid = $row["school_name"]; } echo $newschoolid; } ?>
i'm doing wrong here. i'm not entirely familiar ajax. appreciate help.
Comments
Post a Comment