dongliaoqiang8524 2011-08-18 04:02
浏览 29
已采纳

从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!

  • 写回答

4条回答 默认 最新

  • douhan1992 2011-08-18 04:24
    关注

    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 }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算