Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何制作html<;选择>;php的粘性?_Php_Html_Mysql - Fatal编程技术网

如何制作html<;选择>;php的粘性?

如何制作html<;选择>;php的粘性?,php,html,mysql,Php,Html,Mysql,我一直在尝试使我的select保持粘性,以便它保留当前正在搜索的用户的firstname和lastname的值。我已经尝试了很多方法,包括以不同的方式构造我的php,使脚本位于文件的开头,以if(isset($_POST['submit']){}开头,然后用填充的字段回显html,否则{}回显空白html。我认为下面的方法是我最接近于让它变得黏糊糊的方法,但我仍然无法让它发挥作用 <?php $user_id = 2; $user_firstname = "Soren"; $user_l

我一直在尝试使我的select保持粘性,以便它保留当前正在搜索的用户的firstname和lastname的值。我已经尝试了很多方法,包括以不同的方式构造我的php,使脚本位于文件的开头,以if(isset($_POST['submit']){}开头,然后用填充的字段回显html,否则{}回显空白html。我认为下面的方法是我最接近于让它变得黏糊糊的方法,但我仍然无法让它发挥作用

<?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>

上传文件
选择用户:

非常感谢您的帮助。。。谢谢

您的意思是,在表单提交并重新显示后,您希望表单中已经选择了以前选择的值

很简单:

while(...) {
   if (should be selected) {
       <option selected="selected">...</option>
   } else {
      <option>...</option>
   }
}
while(…){
如果(应选择){
...
}否则{
...
}
}