php - 从一个选择中获取值并从数据库中调用另一选择的值

标签 php mysql

我正在使用 PHP 和 MYSQL 开发一个图书馆项目,其中我在数据库中创建了 3 个表,即:library_books、student_db 和 Issued_books。

有一个名为issue.php的页面,它是为管理员创建的,用于向学生发放书籍。该页面有 Book_id、Book_title、Author、Class(学生类(class) I 至 XII)、Student_name & Student_id(禁用文本类型)等列。

我正在从library_books数据库本身获取所有与书籍相关的详细信息,

我想要实现的是,当用户在“类(class)”字段中选择一个值时,页面应自动获取该类(class)注册的学生姓名列表,并且当从此列表中选择“Student_name”时,应自动获取“Student_id”进入 Student_id 字段。

这是代码,请看一下

<select required class='styled-select green semi-square' style='width: 15%; margin-left: 5px;' id='Class' name='st_Class'>
    <option selected='true' disabled='disabled' value=''>Select Class</option>
    <option>I</option>
    <option>II</option>
    <option>III</option>
    <option>IV</option>
    <option>V</option>
    <option>VI</option>
    <option>VII</option>
    <option>VIII</option>
    <option>IX</option>
    <option>X</option>
    <option>XI</option>
    <option>XII</option>
    </select>   
    <select required class="styled-select green semi-square" style="width: 15%" id="st_name" name="st_name">

    <option selected='true' disabled='disabled' value=''>Select Student</option>
<?php
    $query2 = mysqli_query($conn,"SELECT Student_name FROM Student_db WHERE Class='st_Class'");
while($row1 = mysqli_fetch_array($query2,MYSQLI_NUM))
{
    $st_name = $row1[6];
    $st_id = $row1[5];
}
?>
    <option><?php echo(isset($_POST['st_name'])&&($_POST['st_name']=='1'));?></option>
    </select>
    <input type="text" id="st_id" value="<?php echo $st_id?>" name="st_id" disabled/>

最佳答案

如果我正确理解您需要什么,为了使用选择选项将数据库中的一些数据填充到 div/input 中,您可以查看 jQuery change function 。直接来自文档:

Attaches a change event to the select

在您的情况下,您可以从具有 ID 的选择列表中检索学生数据(或从姓名中检索 ID,无论哪种方式都可以,只要姓名链接到数据库中的唯一 ID)

I will definately not write code for you, just show you the path :)

记住在页面的头部包含 jQuery 库:)

您可以使用的示例:

-> 选择列表(应来自 PHP 查询,此处硬编码以使其简单)

<select id="st_id">
<option value=""> -- select --</option>
<option value="1"> Name 1</option>
<option value="2"> Name 2</option>
<option value="3"> Name 3</option>
</select>
<p>RESPONSE : <input type="text" name="response" id="response" value="" /></p>

-> Jquery 部分:

$(document).ready( function() { // to make sure it's all loaded before use

$("#st_id").on('change', function() { // #st_id is the ID of your select
          var id = $(this).val(); // to catch the value of the selected option
          $.ajax({
           url: "select_get_data.php", // page that will catch/handle the ID and retrieve data from DB accordingly
           method: "POST",
           data: "id="+id, // the selected ID
           success: function(resp){ // if PHP sends correct answer
           $("#response").val(''+resp+''); // #response is the ID of the div/input where data response will be populated
            },
            error: function (request, status, error) {
            alert(request.responseText);
            }
          });
         });
});

-> PHP 部分

<?php
error_reporting(E_ALL); ini_set('display_errors', 1); // to handle errors if any

$id = $_POST['id']; // from AJAX -> here, you must perfom some checking
// never ever trust data from users !!!

echo"Name is MYNAME with ID #$id"; // this will be the response handled in JS by the line
// success: function(resp){ // your echo will be known as 'resp'

// of course, you need to write all checking and query instead of just echo'ing
// but that's the way it works...
?>

hope this helps...

关于php - 从一个选择中获取值并从数据库中调用另一选择的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42946409/

相关文章:

用于选择多个计数列的MySQL查询

PHP/MySQL 插入查询

mysql - 要调用此方法, “Membership.Provider” 属性必须是 “ExtendedMembershipProvider” 的实例

php - API 平台 : Filtering Custom Data Provider

php - MYSQL: Order Topics By Latest Comment(评论有主题)

php - 浏览器的 X-XSS-Protection/XSS Auditor 是否足以防止 XSS?

mysql - Express引擎 : Custom query - Grouping entries daily

php - MySQL 从两个不同的表中选择唯一值?

php - 困惑的人寻求 : "Warning: Cannot modify header information"

mysql - 更新具有潜在冲突的主键字段