[英]How do I make my html <select> sticky with php?
I have been trying to make my select sticky so that it will retain the value of the firstname and lastname of the user that is currently being searched. 我一直在尝试使我的选择置为粘性,以便它保留当前正在搜索的用户的名字和姓氏。 I have tried many approaches including structuring my php differently so that the script is at the beginning of the file and starts with if(isset($_POST['submit'])){} then it echoes the html with the fields filled out and else{} echo the html blank.
我尝试了许多方法,包括以不同的方式构造php,以便脚本位于文件的开头,并以if(isset($ _ POST ['submit'])){}开头,然后它会回显html,并填写字段并else {}回显html空白。 This approach below is the closest I have gotten to making it sticky I think but I still can't get it to work.
我认为,以下这种方法是使粘性最接近的一种方法,但仍然无法使它起作用。
<?php
$user_id = 2;
$user_firstname = "Soren";
$user_lastname = "Craig";
require ('mysqli_connect.php');
# based on student name, get list of files uploaded
$pageTitle = "View User Files";
include ('includes/header.html');
?>
<div class="container">
<h2>Uploaded Files</h2>
<div class="form-group col-xs-4">
<form action="newview.php" method="POST">
<label for="sel1">Select User:</label>
<select class="form-control" id="select-user" name="select-user">
<option><?php if(isset($firstname) && isset($lastname)) echo $lastname . ", " . $firstname?></option>
<?php
$query = "SELECT user_id, firstname, lastname FROM studentusers ORDER BY lastname ASC";
$stmt = mysqli_prepare($dbconnect, $query);
if(mysqli_stmt_execute($stmt))
{
mysqli_stmt_bind_result($stmt, $user_id, $firstname, $lastname);
while(mysqli_stmt_fetch($stmt))
{
echo '<option value="' . $user_id . '">' . $lastname . ", " . $firstname . "</option>";
}
}
echo "</select>";
mysqli_stmt_close($stmt);
echo '<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>';
if(isset($_POST['submit']))
{
$user_id = $_POST['select-user'];
echo '<p>' . $firstname . " " . $lastname . '</p>
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Date</th>
</tr>
</thead>
<tbody>';
$query = "SELECT filename, posteddate FROM fileuploads WHERE user_id = ?";
$statement = mysqli_prepare($dbconnect, $query);
mysqli_stmt_bind_param($statement, 'i', $user_id);
if(mysqli_stmt_execute($statement))
{
// bind the result
mysqli_stmt_bind_result($statement, $fileName, $fileDate);
while(mysqli_stmt_fetch($statement))
{
echo "<tr>
<td><a href='../uploads/'" . $fileName . "> " . $fileName . " </a></td>
<td></td>
<td>" . date('F j, Y', strtotime($fileDate)) . "</td>
</tr>";
}
}
}
?>
</div>
</body>
</html>
Any help is much appreciated... Thanks! 非常感谢任何帮助...谢谢!
You mean that after the form's submitted and re-displayed, you want the previous selected values to be already-selected in the form? 您是说在提交并重新显示表单后,是否要在表单中已经选择了先前选择的值?
Easy enough: 很简单:
while(...) {
if (should be selected) {
<option selected="selected">...</option>
} else {
<option>...</option>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.