此表單的目的是更新我的服務器上的文件,並在更改後在表單本身上反映更新後的更改。下面是當前的代碼我有幫助,從幾個用戶:php表單與exec,file_get_contents和爆炸
<?php
$file = "/var/www/html/colors/color.choices";
$contents = file($file, FILE_SKIP_EMPTY_LINES);
$color = explode("=", $contents[0]);
$size = explode("=", $contents[1]);
$shape = explode("=", $contents[2]);
if(!empty($_REQUEST['color_choice'])){
exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /var/www/html/colors/color.choices");
echo 'File color choice has been updated';
}
if(!empty($_REQUEST['size_choice'])){
exec('sed -i '.escapeshellarg('s/size=.*/size='.$_REQUEST['size_choice'].'/g')." /var/www/html/colors/color.choices");
echo 'File size choice has been updated';
}
if(!empty($_REQUEST['shape_choice'])){
exec('sed -i '.escapeshellarg('s/shape=.*/shape='.$_REQUEST['shape_choice'].'/g')." /var/www/html/colors/color.choices");
echo 'File shape choice has been updated';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color_choice">;
<option value="red" <?php if($color[1] == 'red'){?>selected="selected"<?php }?>>red</option>;
<option value="black" <?php if($color[1] == 'black'){?>selected="selected"<?php }?>>black</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="size_choice">;
<option value="large" <?php if($size[1] == 'large'){?>selected="selected"<?php }?>>large</option>;
<option value="small" <?php if($size[1] == 'small'){?>selected="selected"<?php }?>>small</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="shape_choice">;
<option value="round" <?php if($shape[1] == 'round'){?>selected="selected"<?php }?>>round</option>;
<option value="square" <?php if($shape[1] == 'square'){?>selected="selected"<?php }?>>square</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
更新:我已經調整了代碼,似乎幾乎有工作。現在最後的選擇總是按預期工作。因此,在這種情況下,「形狀」值現在可以很好地工作。但是,「顏色」和「尺寸」選項會更新color.choices文件,但更新不會反映在窗體本身上。爲什麼現在最後一個選項適用於表格,但前兩個選項不適用?
這行''color = explode(「=」,trim($ current));'只會在一行上有效。如果你添加新的選項到你的文件中,你需要在分解每一行之前分解它。 – 2013-06-22 03:27:15