从HTML表单编写和编辑PHP配置文件?

Hi just looking for some direction, I have a HTML form which has several fields and basically what I am wanting to do is save the data entered from the form to a PHP config file and also have the ability to edit the saved data by accessing and submitting the form again. I would prefer to do this without the use of a database.

So here's an example:

<form method="post" name="config_form">
<div id="field">
<label>Keywords</label>
<br />
<input type="text" name="keyword">
</div>

<br />

<select name="color">
<option value="green">Green</option>
<option value="orange">Orange</option>
<option value="blue">Blue</option>
<option value="red">Red</option>
</select>

</form>

So the user enters 'computer' as the keyword and selects the color 'blue'. I want to then save this data into my config.php file as variables allowing my other website pages to access this data. Should this all be in an array as well?

<?php
//config file
$keyword = "computer";
$color = "blue";

?>

Also when I go back access the form again can I make it so the fields are prefilled with the data from the config.php file?

Any help would be much appreciated thank you!

You can include your configuration file in your main php script file:

// main.php
<? php include("config.php"); ?>

and build the form with something like this:

// main.php
<?php
?>
    <form method="post" name="config_form">
        <div id="field">
            <label>Keywords</label>
            <br />
            <input type="text" name="keyword">
        </div>

        <br />

        <select name="color">
            <option value="green" <? if ($color == "green") echo "SELECTED"; ?> >Green</option>
            <option value="orange" <? if ($color == "orange") echo "SELECTED"; ?> >Orange</option>
            <option value="blue" <? if ($color == "blue") echo "SELECTED"; ?> >Blue</option>
            <option value="red" <? if ($color == "red") echo "SELECTED"; ?> >Red</option>
        </select>

</form>
<?
?>

finally you can save the form data in your config.php file using fopen() and fwrite() functions on form submit:

$key = $_POST["key"];
$color = $_POST["color"];

if ($key != '' && $color != '') {
    $f = fopen('config.php', 'w') or die("can't open file");
    fwrite($f, '<?php $keyword=' . $key . ';$color=' . $color . ';?>');
    fclose($f);
} else { // write default values or show an error message }

You can do this in multiple ways. Best way would be to use a database such as MYSQL. You are asking for persistence and that is what DBs are for. Try this.

$key = $_POST["key"];
$color = $_POST["color"];
mysql_query("INSERT INTO smeTbl VALUES ('1',$key,$color)");

THen in the config file or what ever other file you have you can retrieve these values.

 $query = mysql_query("SELECT * FROM smeTbl WHERE id='1'");
 $fetch = mysql_fetch_array($query);
 $keyword = $fetch["key"];
 $color = $fetch["color"];

This is just an example and you can refine it based on your needs

If you're dedicated to storing this sort of thing in a file, then probably the easiest way is to just store all the data in an array in the form of $keyword => $value then use the serialize() and unserialize() functions to transform them into a format that can be easily stored into and read from a file.

Keep in mind that if there is only one file, then a change made by one user will affect them all, so if that's not acceptable, then you'll need to come up with a way to determine the user and which file to use.

A much better way of doing this is to just store these values in a database. Create a table called options with two fields - option and value - which will store the configuration options. If you want different users to have their own options, then you could add another field - userid (as a foreign key to a users table) - to track which user an option pair applies to.

Further, if there are a predefined set of options a user can set, then you could have fields in the table for each option, with default values, and you can create a row for each user with the specific config options set in a single record for that user.

when you submit the form and you want to store the submitted form's data in one php file,ni must use the file action functions fopen the config.php,and write php code into it. when you display the form,you can fopen the config.php,and use the function "eval" to get data.forgive my english.

//when submit form
$string = '<?php $keyword="computer";$color="blue";?>';
$fp = fopen('config.php', 'w');
fwrite($fp, $string);
fclose($fp);
//when display form
include("config.php");
//so you can use $keyword and $color