Sunday, 18 August 2013

PHP form option fields will not display current value from file

PHP form option fields will not display current value from file

I've been working on a PHP form that changes values in a file on my
server. It almost works but in the form fields it does not display the
current data from the file correctly:
<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
$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';
}
?>
<?php echo "$color_name"; ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color_choice">;
<option value="0" <?php if($color_name[1] ==
'0'){?>selected="selected"<?php }?>>Red</option>;
<option value="1" <?php if($color_name[1] ==
'1'){?>selected="selected"<?php }?>>Blue</option>;
<option value="2" <?php if($color_name[1] ==
'2'){?>selected="selected"<?php }?>>Green</option>;
<option value="3" <?php if($color_name[1] ==
'3'){?>selected="selected"<?php }?>>Purple</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
The form changes the values correctly but I need the $color_name[1] values
in the option field to display the updated value after the change has been
made. Currently it doesn't work and I don't understand why. I also see
this error "Notice: Undefined offset: 1.." I'm not sure if that matters or
not. Any help would be greatly appreciated!

No comments:

Post a Comment