编写和来自HTML表单编辑PHP配置文件? [英] Writing and editing a PHP config file from a HTML form?

查看:140
本文介绍了编写和来自HTML表单编辑PHP配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨只是寻找一些方向,我有哪几种领域,基本上是我想要做的是保存从表单中输入的PHP配置文件中的数据,也有编辑保存的数据的能力的HTML表单通过访问,并再次提交表单。我想preFER要做到这一点,而无需使用的数据库。

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.

所以这里有一个例子:

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

所以用户输入计算机为关键字,并选择颜色蓝色。我想那么这个数据保存到我的config.php文件作为变量让我的其他网站页面来访问这些数据。如果这一切是在一个数组中呢?

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";

?>

此外,当我回去访问窗体我又可以把它使字段从config.php文件与数据pfilled $ P $?

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?

任何帮助将是非常美联社preciated谢谢!

Any help would be much appreciated thank you!

推荐答案

您可以包括您的配置文件中的主PHP脚本文件:

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

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

和像这样建的形式:

// 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>
<?
?>

终于可以保存在你的表单数据的config.php 文件中使用 fopen()函数的fwrite()的形式提交功能:

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 }

这篇关于编写和来自HTML表单编辑PHP配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆