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. 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>
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.