我一直在研究改變服務器上文件中的值的PHP表單。表單正確地更改了值,但我需要表單的<option value="round" <?php if($shape == 'round'){?>selected="selected"<?php }?>>Round</option>
部分始終顯示在文件中設置的當前值,但目前沒有。PHP表單選項字段將不會顯示文件中的當前值
#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>
你也可以粘貼文件的內容嗎? – jexx2345
完成。我正在使用:$ color_name = explode(「=」,trim($ contents [2]));但文件中值的行總是在變化,所以它不能很好地工作。 – jcrane
您正在訪問'$ color_name'就像一個數組,但基於您的代碼,我不認爲它會包含多個值,因此'$ color_name [1]'將始終爲空...... – JLewkovich