[英]How to get details of items in drop down menu using php and mysql
我有一個數據庫,並且在該數據庫中有一個名為courses_details
的表。 當用戶從下拉菜單中選擇一門課程時,可以使用php以該表格的形式顯示該課程的信息(CourseCode,CourseTitle,CourseCredits)。 我無法得到它。 如果有人能幫助我,我將感到榮幸。
<form action="courses.php" method="POST" class="FormStyle">
<select name="courses">
<option value="ITC">Intro. To Computing</option>
<option value="OOP">Object Oriented Programming</option>
<option value="DS">Data Structures</option>
</select>
</form>
因此,這是創建$ _POST [submit]和$ _POST ['courses']變量的形式。 members
將是您希望顯示帶有查詢信息的表單的頁面。
<form action="members.php" method="POST" class="FormStyle">
<select name="courses">
<option value="ITC">Intro. To Computing</option>
<option value="OOP">Object Oriented Programming</option>
<option value="DS">Data Structures</option>
</select>
<input type="submit" name="submit" value="Submit">
</form>
在PHP手冊中定義的$ _POST變量 => An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
。
請記住, <input type="submit" name="submit" value="Submit">
是輸入“按鈕”,一旦用戶按下按鈕,該按鈕便會提交表單。 此輸入中的名稱定義為name="submit"
。 因此,一旦我們閱讀了有關POST變量的手冊,就會發現它是表單中通過http請求傳遞的變量數組。 因此,我們構建了邏輯來檢查是否按下了submit
按鈕。 if($_POST['submit'])
,運行與成功按下按鈕關聯的代碼。 在輸入字段中, name
屬性填充有三元運算符結果。 可以使用相同的方法或與此相關的任意數量的屬性添加selected
和/或checked
的內容。
在這里,您將構造查詢包含該值的數據庫和表的邏輯。 查詢成功后,定義要打印/回顯的變量。
更新 :您的courses.php頁面看起來像。
"your_external_page_source".php
define("HOST", 'yourhostvalue');
define("USER", 'yourusername');
define("PASSWORD", 'yourpassword');
define("DATABASE", 'yourdbname');
define("DB_MEMBERS", 'yourdbtable');
然后在“ form_page” .php上
include = 'constants.php';
if($_POST['submit']){//here we check to see if the input 'submit' was posted through the form
//success define your variable(s)
//$courses will now hold the $_POST variable that passed through the http POST method (the form names as defined in your various forms tags)
$courses = $_POST['courses'];//assign the value of your select field to the variable $courses
//connect to DB if not already
$db = new mysqli(HOST, USER ,PASSWORD, DATABASE);//CONSTANTS from a linked file
// Check for errors
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
//using an example DB with a table defined as a constant DB_MEMBERS
$result = $db->query("SELECT * FROM `DB_MEMBERS` WHERE `courses`=".$courses);
if($result->num_rows){// if rows are present from the query
// Cycle through results and define your variables
while ($row = $result->fetch_assoc()){
$courseCode = htmlspecialchars($row['CourseCode']); //*avoid possible exploits with htmlspecialchars()
$courseTitle = htmlspecialchars($row['CourseTitle']); //*
$courseCredits = htmlspecialchars($row['CourseCredits']);//*
}
}
創建一個表單以顯示您的結果。
<form action="somelink.php" method="POST">
<input class="form-group-item" value="<?php $cCode = ($courseCode > 0 ? echo $courseCode : echo ''); echo $cCode; ?>" name="courseCode">
<input class="form-group-item" value="<?php $cTitle = ($courseTitle > 0 ? echo $courseTitle : echo ''); echo $cTitle; ?>" name="courseTitle">
<input class="form-group-item" value="<?php $cCredits = ($courseCredits > 0 ? echo $courseCredits : echo ''); echo $cCredits; ?>" name="cCredits ">
<input type="submit" name="submitMembers" value="Submit">
</form>
查看官方PHP $ _POST手冊 ,還有$_GET
>通過URL site.com?get=".$somevalue;
和$_REQUEST->(Both $_POST and $_GET)
代碼尚未檢查
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.