假设您在表单中有一个选择元素
<form action="" method="post">
<select name ="fruits">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Oranges</option>
<option value="mango">Mangoes</option>
</select>
<input type="submit" name="submit"/>
</form>
如果我理解您正确,您希望在用户单击提交按钮时将所选值显示给用户以将其放在您的 php 代码顶部。
<?php
//This will check if form is submitted then fetch the value from select element else assign null
$value = isset($_POST['submit']) ? $_POST['fruits'] : NULL;
?>
并将所有更改<option>
为
<option value="apple" <?php echo ($value == 'apple') ? 'selected' : ''; ?>>Apple</option>
<option value="banana" <?php echo ($value == 'banana') ? 'selected' : ''; ?>>Banana</option>
<option value="orange" <?php echo ($value == 'orange') ? 'selected' : ''; ?>>Oranges</option>
<option value="mango" <?php echo ($value == 'mango') ? 'selected' : ''; ?>>Mangoes</option>