Question: ```
<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
   <table>
      <form name="form" method="POST" action="">
         <tr><td colspan="2" align="center"> Database </tr>
         <tr><td>Servername:</td>
         <td><input type="text" name="servername" value="" ></td></tr>
         <tr><td>Username:</td>
             <td><input type="text" name="username" value="" ></td>
         </tr>
         <tr><td>Password:</td>
             <td><input type="text" name="password" value="" ></td>
         </tr>
         <tr><td>Database Name:</td>
             <td><input type="text" name="dbname" value=""> </td>
         </tr>
         <tr><td colspan="2" align="center"><input type="submit" name="submit" ></td</tr>
      </form>
   </table>
</body>
</html>

```

I want a create a php config file, using the values of the form. I can't understand How can i do this. I'm learner now so please suggest me..

Answer: 
Simple example:

```
// save to file
$post = json_encode($POST);
file_put_contents('config.txt', $post);

// read from file
$post1 = file_get_contents('config.txt');
$p = json_decode($post1);
// show array
print_r($p);
// show username
echo $p['username'];

```

Or php file

```
if (isset($_POST['username'])) {
    $php = '<?php
    $user = "'.$_POST['username'].'";
    $pass = "'.$_POST['pass'].'";
    ?>';
    file_put_contents('config.php', $php);
}

```
to create the config file from the user input, you can create an empty `config.php` file then use `fwrite` and `fopen` to open and write to that file.

```
<?PHP

$errors = "";
if (isset($_POST["submit"])) {
    if (empty($_POST['servername'])) {

        echo "Enter servername";
        $errors++;
    } else {

        $servername = $_POST['servername'];
    }

    if (empty($_POST['username'])) {

        echo "enter username";
        $errors++;
    } else {

        $username = $_POST['username'];
    }

    if (empty($_POST['password'])) {

        echo "enter password";
        $errors++;
    } else {

        $password = $_POST['password'];
    }

    if (empty($_POST['dbname'])) {

        echo "enter database";
        $errors++;
    } else {

        $dbname = $_POST['dbname'];
    }

    if ($errors <= 0) { // no errors

        $string = '<?php 
$dbhost = "' . $servername . '";
$dbuname = "' . $username. '";
$dbpass = "' . $password . '";
$dbname = "' . $dbname . '";
?>';

        $fp = fopen("config.php", "w");
        if (fwrite($fp, $string)) {

            echo "db created";
        }
        fclose($fp);

    }

}

?>

<!DOCTYPE html>
<html>
<head>
   <title></title>
</head>
<body>
   <table>
      <form name="form" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
         <tr><td colspan="2" align="center"> Database </tr>
         <tr><td>Servername:</td>
         <td><input type="text" name="servername" value="" ></td></tr>
         <tr><td>Username:</td>
             <td><input type="text" name="username" value="" ></td>
         </tr>
         <tr><td>Password:</td>
             <td><input type="text" name="password" value="" ></td>
         </tr>
         <tr><td>Database Name:</td>
             <td><input type="text" name="dbname" value="" > </td>
         </tr>
         <tr><td colspan="2" align="center"><input type="submit" name="submit"></td></tr>
      </form>
   </table>
</body>
</html>

```