2016-07-09 48 views
0

我正在開發一個項目,並希望在更新記錄時爲用戶提供每個確定的值。如何在php中創建選擇下拉列表

這是我的代碼到目前爲止。

<?php 
// if there are any errors, display them 
if ($error != '') 
{ 
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>'; 
} 
?> 

<form action="" method="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>"/> 
<div> 
<p><strong>ID:</strong> <?php echo $id; ?></p> 
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br> 
<strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br> 
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br> 
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br> 
<input type="submit" name="submit" value="Submit"> 
</div> 
</form> 
</body> 
</html> 
<?php 
} 



// connect to the database 
include('connect-db.php'); 

// check if the form has been submitted. If it has, process the form and save it to the database 
if (isset($_POST['submit'])) 
{ 
// confirm that the 'id' value is a valid integer before getting the form data 
if (is_numeric($_POST['id'])) 
{ 
// get form data, making sure it is valid 
$id = $_POST['id']; 
$Name = mysql_real_escape_string(htmlspecialchars($_POST['Name'])); 
$Status = mysql_real_escape_string(htmlspecialchars($_POST['Status'])); 
$Comments = mysql_real_escape_string(htmlspecialchars($_POST['Comments'])); 
$Type = mysql_real_escape_string(htmlspecialchars($_POST['Type'])); 

// check that firstname/lastname fields are both filled in 
if ($Name == '' || $Type == '') 
{ 
// generate error message 
$error = 'ERROR: Please fill in all required fields!'; 

//error, display form 
renderForm($id, $Name, $Status, $Comments, $Type, $error); 
} 
else 
{ 
// save the data to the database 
mysql_query("UPDATE Schools SET Name='$Name', Status='$Status', Comments='$Comments', Type='$Type' WHERE id='$id'") 
or die(mysql_error()); 

// once saved, redirect back to the view page 
header("Location: view.php"); 
} 
} 
else 
{ 
// if the 'id' isn't valid, display an error 
echo 'Error!'; 
} 
} 
else 
// if the form hasn't been submitted, get the data from the db and display the form 
{ 

// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) 
{ 
// query db 
$id = $_GET['id']; 
$result = mysql_query("SELECT * FROM Schools WHERE id=$id") 
or die(mysql_error()); 
$row = mysql_fetch_array($result); 

// check that the 'id' matches up with a row in the databse 
if($row) 
{ 

// get data from db 
$Name = $row['Name']; 
$Status = $row['Status']; 
$Comments = $row['Comments']; 
$Type = $row['Type']; 

// show form 
renderForm($id, $Name, $Status, $Comments, $Type, ''); 
} 
else 
// if no match, display result 
{ 
echo "No results!"; 
} 
} 
else 
// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error 
{ 
echo 'Error!'; 
} 
} 
?> 

我想用選項的下拉列表替換狀態文本提交。

+0

記住改爲'mysqli_ *'離開'mysql_ *'因爲它是在PHP 7刪除,當你改變的是,這將是一個痛苦將所有函數改爲'mysqli_ *',還有一件事情,總是使用'require_once'而不是'include' – matiaslauriti

回答

0

您可以使用html <datalist><select>標籤。

我希望我可以幫忙。

1

替換您<input通過<select

<form action="" method="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>"/> 
<div> 
<p><strong>ID:</strong> <?php echo $id; ?></p> 
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br> 
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>--> 
<strong>Status:</strong> <select name="Status"> 
          <option value="1">Status 1</option> 
          <option value="2">Status 2</option> 
          </select> 
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br> 
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br> 
<input type="submit" name="submit" value="Submit"> 
</div> 
</form> 

如果你的狀態是在一個表中,填寫<select>與查詢:

<form action="" method="post"> 
<input type="hidden" name="id" value="<?php echo $id; ?>"/> 
<div> 
<p><strong>ID:</strong> <?php echo $id; ?></p> 
<strong>School Name:</strong> <input type="text" name="Name" value="<?php echo $Name; ?>"/><br/><br> 
<!-- <strong>Status:</strong> <input type="text" name="Status" value="<?php echo $Status; ?>"/><br/><br>--> 
<strong>Status:</strong> <select name="Status"> 
<?php 
$result = mysql_query("SELECT * FROM tbl_status",$cnx); 
while ($row = mysql_fetch_array($result)) 
    echo "<option value='" . $row["id"] . "'>" . $row["text"] . "</option>"; 
?> 
          </select> 
<strong>Comments:</strong> <input type="text" name="Comments" value="<?php echo $Comments; ?>"/><br/><br> 
<strong>Type:</strong> <input type="text" name="Type" value="<?php echo $Type; ?>"/><br/><br> 
<input type="submit" name="submit" value="Submit"> 
</div> 
</form> 
+0

謝謝。這正是我所期待的。 –

+0

@CarlosOulman,如果答案是有用的,你可以點擊複選標記接受它^ _: –

0

所有你需要從mysql_ *切換到第一mysqli_ *,因爲它會在PHP 7.0得到去除我使用這個功能,我創造它可以幫助你 這裏是PHP代碼

function GetOptions($request) 
{ 
global $con; 
$sql = "SELECT * FROM data GROUP BY $request ORDER BY $request"; 
$sql_result = mysqli_query($con, $sql) or die('request "Could not execute SQL query" ' . $sql); 
while ($row = mysqli_fetch_assoc($sql_result)) { 
echo "<option value='" . $row["$request"] . "'" . ($row["$request"] == $_REQUEST["$request"] ? " selected" : "") . ">" . $row["$request"] . "$x</option>"; 
} 
} 

和HTML代碼是這樣

<label>genre</label> 
<select name="genre"> 
<option value="all">all</option> 
<?php 
GetOptions("genre"); 
?> 
</select> 
相關問題