【问题标题】:Get value from one select & call values for another select from database从一个选择中获取值并从数据库中为另一个选择调用值
【发布时间】:2017-03-22 08:36:26
【问题描述】:

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

创建了一个名为 issue.php 的页面,供管理员向学生发放书籍。该页面有 Book_id、Book_title、Author、Class(学生的班级从 I 到 XII)、Student_name 和 Student_id(禁用文本类型)等列。

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

我想要实现的是,当用户在 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/>

【问题讨论】:

  • 您的问题到底是什么?或者这只是一个“编码请求”?
  • @Alex_Odenthal 我已经提到了我想要实现的目标,请再读一遍,谢谢
  • 好的。但是,您应该准确阅读本论坛的条款。这不是一个要求“其他人,请你做我的工作吗?!”的地方。你确实展示了一个问题并解释了一个错误或非常准确的东西,你想要的东西,以及到目前为止你为实现它所做的事情。现在你刚刚发布了一些代码并告诉全世界你喜欢这段代码做什么。
  • 要解决您的“问题” - 看起来(根据您的描述)您需要一些 ajax 代码来对下拉列表的更改做出反应。

标签: php mysql


【解决方案1】:

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

将更改事件附加到选择

在您的情况下,您可以从具有 ID 的选择列表中检索学生数据(或从名称中获取 ID,只要名称与数据库中的唯一 ID 相关联,它就可以使用任何一种方式)

我肯定不会为你写代码,只是告诉你路径:)

记得在页面的头部包含 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...
?>

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2021-05-03
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    相关资源
    最近更新 更多