我有以下代码:
<?php session_start(); include_once("config.php"); $query = "SELECT Category FROM 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>"; ?>从数据库中填充下拉框的值。 我想知道是否有一种方法可以在用户点击下拉菜单中的某个选项然后在表格中填充结果时运行一个select语句?
任何信息都会有帮助!
谢谢
I have the following code:
<?php session_start(); include_once("config.php"); $query = "SELECT Category FROM 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 the drop down box are filled from the database. I was wondering if theres a way to have a select statement that will run when a user clicks on one of the options in the drop down menu and then populate the results in a table?
any information will help!
Thanks
最满意答案
好的,共振81,你想要根据选择的选项填写一个表,下一个代码完全按照你想要的方式,解释就在下面:
<html> <head> <title>My list</title> <script type="text/javascript"> //---------------------------------------------------------------- // SENDS SELECTED OPTION TO RETRIEVE DATA TO 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 any option <br/> <select id="my_select" onchange="send_option();"> <option>Select an option</option> <?php //---------------------------------------------------------------- // LIST FILLED FROM DATABASE (ALLEGEDLY). for ( $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 FROM DATABASE ACCORDING TO SELECTED OPTION. if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION. for ( $i = 0; $i < 4; $i++ ) // DISPLAY ROWS. { echo "<tr>"; for ( $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 TO SEND THE 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>为了让你(和我)更容易,我不使用数据库,你所要做的就是将以前的代码复制粘贴到文本文件,将其重命名为“01.php”(因为这是表单的动作) ,你可以改变它,并在你的浏览器中运行它,随时可以使用。
下拉列表从数据库填充(在这种情况下,使用字母),当选择一个选项时,页面将使用所选选项重新加载并填充表格。
你说:“当用户点击下拉菜单中的一个选项然后在表格中填充结果时,将运行一个选择语句”。 你想要的这个选择语句必须把它放在行后面:
if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.因此,您的select语句将从$ _POST中选择所选项并使用它来检索正确的数据并显示它。
如果它对您有所帮助,请告诉我。
这是填写下拉列表的代码,这是我的代码与你的组合:
// LIST FILLED FROM DATABASE (ALLEGEDLY). $query = "SELECT Category FROM books"; $result = mysqli_query ($mysqli, $query); while ( $row = mysqli_fetch_array($result) ) echo "<option value='" . $row['Category'] . "'>" . $row['Category'] . "</option>";接下来的编辑是填写表格。 如果不正确,请更改正确的查询:
// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION. $query = "SELECT Category FROM books where category like '" . $_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>";Ok, resontant81, you want to fill a table depending on the option selected, next code does exactly what you want, the explanation comes just after :
<html> <head> <title>My list</title> <script type="text/javascript"> //---------------------------------------------------------------- // SENDS SELECTED OPTION TO RETRIEVE DATA TO 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 any option <br/> <select id="my_select" onchange="send_option();"> <option>Select an option</option> <?php //---------------------------------------------------------------- // LIST FILLED FROM DATABASE (ALLEGEDLY). for ( $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 FROM DATABASE ACCORDING TO SELECTED OPTION. if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION. for ( $i = 0; $i < 4; $i++ ) // DISPLAY ROWS. { echo "<tr>"; for ( $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 TO SEND THE 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 for you (and for me), I am not using a database, all you have to do is copy-paste previous code to a text file, rename it "01.php" (because that's the action of the form, you can change it), and run it in your browser, is ready to use.
The dropdown is filled from database (in this case, with letters), when an option is selected the page reloads with the selected option and fills the table.
You said: "a select statement that will run when a user clicks on one of the options in the drop down menu and then populate the results in a table". This select statement you want you must put it right after the line :
if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.So your select statement will take the selected option from $_POST and use it to retrieve the right data and display it.
Let me know if it helps you.
This is the code to fill the dropdown, it's my code with yours combined:
// LIST FILLED FROM DATABASE (ALLEGEDLY). $query = "SELECT Category FROM books"; $result = mysqli_query ($mysqli, $query); while ( $row = mysqli_fetch_array($result) ) echo "<option value='" . $row['Category'] . "'>" . $row['Category'] . "</option>";Next edit is to fill the table. Change the query for the right one if it's not right :
// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION. $query = "SELECT Category FROM books where category like '" . $_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>";MySQL根据下拉值选择(MySQL Select based on drop down value)我有以下代码:
<?php session_start(); include_once("config.php"); $query = "SELECT Category FROM 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>"; ?>从数据库中填充下拉框的值。 我想知道是否有一种方法可以在用户点击下拉菜单中的某个选项然后在表格中填充结果时运行一个select语句?
任何信息都会有帮助!
谢谢
I have the following code:
<?php session_start(); include_once("config.php"); $query = "SELECT Category FROM 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 the drop down box are filled from the database. I was wondering if theres a way to have a select statement that will run when a user clicks on one of the options in the drop down menu and then populate the results in a table?
any information will help!
Thanks
最满意答案
好的,共振81,你想要根据选择的选项填写一个表,下一个代码完全按照你想要的方式,解释就在下面:
<html> <head> <title>My list</title> <script type="text/javascript"> //---------------------------------------------------------------- // SENDS SELECTED OPTION TO RETRIEVE DATA TO 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 any option <br/> <select id="my_select" onchange="send_option();"> <option>Select an option</option> <?php //---------------------------------------------------------------- // LIST FILLED FROM DATABASE (ALLEGEDLY). for ( $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 FROM DATABASE ACCORDING TO SELECTED OPTION. if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION. for ( $i = 0; $i < 4; $i++ ) // DISPLAY ROWS. { echo "<tr>"; for ( $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 TO SEND THE 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>为了让你(和我)更容易,我不使用数据库,你所要做的就是将以前的代码复制粘贴到文本文件,将其重命名为“01.php”(因为这是表单的动作) ,你可以改变它,并在你的浏览器中运行它,随时可以使用。
下拉列表从数据库填充(在这种情况下,使用字母),当选择一个选项时,页面将使用所选选项重新加载并填充表格。
你说:“当用户点击下拉菜单中的一个选项然后在表格中填充结果时,将运行一个选择语句”。 你想要的这个选择语句必须把它放在行后面:
if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.因此,您的select语句将从$ _POST中选择所选项并使用它来检索正确的数据并显示它。
如果它对您有所帮助,请告诉我。
这是填写下拉列表的代码,这是我的代码与你的组合:
// LIST FILLED FROM DATABASE (ALLEGEDLY). $query = "SELECT Category FROM books"; $result = mysqli_query ($mysqli, $query); while ( $row = mysqli_fetch_array($result) ) echo "<option value='" . $row['Category'] . "'>" . $row['Category'] . "</option>";接下来的编辑是填写表格。 如果不正确,请更改正确的查询:
// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION. $query = "SELECT Category FROM books where category like '" . $_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>";Ok, resontant81, you want to fill a table depending on the option selected, next code does exactly what you want, the explanation comes just after :
<html> <head> <title>My list</title> <script type="text/javascript"> //---------------------------------------------------------------- // SENDS SELECTED OPTION TO RETRIEVE DATA TO 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 any option <br/> <select id="my_select" onchange="send_option();"> <option>Select an option</option> <?php //---------------------------------------------------------------- // LIST FILLED FROM DATABASE (ALLEGEDLY). for ( $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 FROM DATABASE ACCORDING TO SELECTED OPTION. if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION. for ( $i = 0; $i < 4; $i++ ) // DISPLAY ROWS. { echo "<tr>"; for ( $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 TO SEND THE 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 for you (and for me), I am not using a database, all you have to do is copy-paste previous code to a text file, rename it "01.php" (because that's the action of the form, you can change it), and run it in your browser, is ready to use.
The dropdown is filled from database (in this case, with letters), when an option is selected the page reloads with the selected option and fills the table.
You said: "a select statement that will run when a user clicks on one of the options in the drop down menu and then populate the results in a table". This select statement you want you must put it right after the line :
if ( IsSet( $_POST["my_option"] ) ) // IF USER SELECTED ANY OPTION.So your select statement will take the selected option from $_POST and use it to retrieve the right data and display it.
Let me know if it helps you.
This is the code to fill the dropdown, it's my code with yours combined:
// LIST FILLED FROM DATABASE (ALLEGEDLY). $query = "SELECT Category FROM books"; $result = mysqli_query ($mysqli, $query); while ( $row = mysqli_fetch_array($result) ) echo "<option value='" . $row['Category'] . "'>" . $row['Category'] . "</option>";Next edit is to fill the table. Change the query for the right one if it's not right :
// TABLE FILLED FROM DATABASE ACCORDING TO SELECTED OPTION. $query = "SELECT Category FROM books where category like '" . $_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>";
发布评论