php - Fetch db data depending on selected drop down value -
is possible fetch data db upon changing value of drop down, want pull data corresponds value of drop down , show text area, got codes i've tried create don't know how make work want.
i want page show drop down after selecting value show data in text area without refreshing page, here code:
<div id="maincontent"> <table width="619" border="0" align="center"> <td align="center"><form id="form1" name="form1" method="post" action="" > <fieldset> <legend><strong>ea</strong></legend> <p> <select name="ea_name" id="ea_name"> <option value="" selected="selected">please select...</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> </select> </p> </fieldset> </form></td> </table> <div id="results"></div> </div>
i thinking "onchange" dont know how implement it, want echo resulting data text area within fieldset.
here code i've tried pull data , show text area:
<?php require 'include/db_open.php'; $ea_name = $_post['ea_name']; $sql="select * ea_error ea_name = '" . $ea_name . "'"; $mydata = mysql_query($sql); //to count if there results $numrow = mysql_num_rows($mydata); if($numrow == 0) { echo "no results found."; } else { echo "<fieldset><legend><strong>information</strong></legend><p> <table width="auto" height="172" border="0"> <tr><th scope="row">error</th></tr> <tr><th scope="row">resolution</th></tr> <tr><th scope="row">contact/s</th></tr>;" while($info = mysql_fetch_array($mydata)) { echo "<form action='retrieve.php' method='post'>"; echo"<tr>"; echo "<td align='center'>" . $info['error'] . "<input type=hidden name=error value=" . $info['error'] . " </td>"; echo "<td align='center'>" . $info['resolution'] . "<input type=hidden name=resolution value=" . $info['resolution'] . " size='11' maxlength='11' /> </td>"; echo "<td align='center'>" . $info['contacts'] . "<input type=hidden name=contacts value=" . $info['contacts'] . "' /> </td>"; echo "</tr>"; echo "</form>"; } } echo "</fieldset>"; include 'include/db_close.php'; ?>
updated code:
<?php require 'include/db_open.php'; $ea_name = $_post['ea_name']; $sql="select * ea_error ea_name = '" . $ea_name . "'"; $mydata = mysql_query($sql); //to count if there results $numrow = mysql_num_rows($mydata); if($numrow == 0) { echo "no results found."; } else { echo '<fieldset><legend><strong>information</strong></legend><p> <table width="auto" height="172" border="0"> <tr><th>error</th></tr> <tr><th>resolution</th></tr> <tr><th>contact/s</th></tr>'; while($info = mysql_fetch_array($mydata)) { echo "<form action='retrieve.php' method='post'>"; echo"<tr>"; echo "<td align='center'>" . $info['error'] . "<input type=hidden name=error value=" . $info['error'] . " </td>"; echo "<td align='center'>" . $info['resolution'] . "<input type=hidden name=resolution value=" . $info['resolution'] . " size='11' maxlength='11' /> </td>"; echo "<td align='center'>" . $info['contacts'] . "<input type=hidden name=contacts value=" . $info['contacts'] . "' /> </td>"; echo "</tr>"; echo "</form>"; } } echo "</fieldset>"; include 'include/db_close.php'; ?>
this should trick. put javascript either in script tags in head of page, after jquery include, or in js file , include after jquery...
$(function() { // document.ready $("#ea_name").on("change", function() { $.ajax({ url: "phpfile.php", type: "post", data: { ea_name: $(this).val() }, success: function(data) { $("#results").html(data); } }); }); });
it assigns event handler change event of drop down. when triggered sends ajax request php file (don't forget put correct filename url!) returns html. pushed results div.
note: fixed typo may or may not in php file. @ end of line create query, you'd missed closing ". in case copy , paste real file.
Comments
Post a Comment