php - MySQL Select based on drop down value -


i have following code:

<?php session_start(); include_once("config.php");  $query = "select category books"; $result = mysqli_query ($mysqli, $query);  echo '<select name="dropdown" value=""><option value="">dropdown</option>';     while($row = mysqli_fetch_array($result))     {         echo '<option value="' . $row['category'] . '">' . $row['category'] . '</option>';     }     echo "</select>";  ?> 

the values of drop down box filled database. wondering if theres way have select statement run when user clicks on 1 of options in drop down menu , populate results in table?

any information help!

thanks

ok, resontant81, want fill table depending on option selected, next code want, explanation comes after :

<html>   <head>     <title>my list</title>     <script type="text/javascript"> //---------------------------------------------------------------- // sends selected option retrieve data fill table. function send_option () { var sel = document.getelementbyid( "my_select" ); var txt = document.getelementbyid( "my_option" ); txt.value = sel.options[ sel.selectedindex ].value; var frm = document.getelementbyid( "my_form" ); frm.submit(); } //----------------------------------------------------------------     </script>   </head>   <body>      click on option     <br/>     <select id="my_select" onchange="send_option();">       <option>select option</option> <?php //---------------------------------------------------------------- // list filled database (allegedly). ( $i = 0; $i < 5; $i++ ) { $text = chr($i+65) . chr($i+65) . chr($i+65);   echo "<option value='" . $text . "'>" . $text . "</option>"; } //---------------------------------------------------------------- ?>     </select>     <br/>      <br/>     <table> <?php //---------------------------------------------------------------- // table filled database according selected option. if ( isset( $_post["my_option"] ) ) // if user selected option.      ( $i = 0; $i < 4; $i++ ) // display rows.      { echo "<tr>";        ( $j = 0; $j < 6; $j++ ) // display columns.          echo "<td>" . $_post["my_option"] . "</td>"; // display option.        echo "</tr>";      } else echo "<tr><td>table empty</td></tr>"; //---------------------------------------------------------------- ?>     </table>  <!-- form send selected option. -->     <form method="post" action"01.php" style="display:none" id="my_form">       <input type="text" id="my_option" name="my_option"/>     </form>    </body> </html> 

to make things easier (and me), not using database, have copy-paste previous code text file, rename "01.php" (because that's action of form, can change it), , run in browser, ready use.

the dropdown filled database (in case, letters), when option selected page reloads selected option , fills table.

you said: "a select statement run when user clicks on 1 of options in drop down menu , populate results in table". select statement want must put right after line :

if ( isset( $_post["my_option"] ) ) // if user selected option. 

so select statement take selected option $_post , use retrieve right data , display it.

let me know if helps you.

this code fill dropdown, it's code yours combined:

// list filled database (allegedly). $query = "select category books"; $result = mysqli_query ($mysqli, $query); while ( $row = mysqli_fetch_array($result) )   echo "<option value='" . $row['category'] . "'>" . $row['category'] . "</option>"; 

next edit fill table. change query right 1 if it's not right :

// table filled database according selected option. $query = "select category books category '" . $_post["my_option"] . "'"; $result = mysqli_query ($mysqli, $query); while( $row = mysqli_fetch_array($result) )   echo "<tr>" .        "<td>" . $row['book_name'] . "</td>" .        "<td>" . $row['author'] . "</td>" .        "<td>" . $row['category'] . "</td>" .        "</tr>"; 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -