我一直在研究一个 PHP 表单,它可以更改我服务器上文件中的值。表单正确更改了值,但我需要<option value="round" <?php if($shape == 'round'){?>selected="selected"<?php }?>>Round</option>
表单的一部分始终显示为文件中设置的当前值,目前它没有。
#file contents (the lines the values are on changed on a regular basis)
size=large
color-name=red
shape=round
height=short
weight=heavy
<?php
$file = "/home/user/color.props";
$contents = file($file, FILE_SKIP_EMPTY_LINES);
foreach($contents as $line) {
list($option, $value) = explode('=', $line);
if ($option == 'color-name') {
$color_name = $value;
} elseif ($option == 'shape') {
$shape = $value;
}
}
if(isset($_REQUEST['color_choice'])){
exec('sed -i '.escapeshellarg('s/color-name=.*/color-name='.$_REQUEST['color_choice'].'/g')." /home/user/color.props");
echo 'Color setting has been updated';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color_choice">;
<option value="round" <?php if($shape == 'round'){?>selected="selected"<?php }?>>Round</option>;
<option value="square" <?php if($shape == 'square'){?>selected="selected"<?php }?>>Square</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>