1

When submit a form, I want to write the form inputs to a text file, including a 2 columns of a html table:

function toString()
{

    $filename = 'D:\file_' . time() . '_' . $_SESSION['username'] . '.txt';

    $data = 'grid_blocks=' . $_POST["grid_blocks"] . "\n" .
        'leverettj=' . $_POST["leverettj"] . "\n" .
        'length=' . $_POST["length"] . "\n" .
        'krw='. "\n";

    $file = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);
    if ($file === false) {
        die('There was an error writing this file');
    } else {
        echo "$file bytes written to file";
    }
}

I managed to write the single values to a .txt file, but now I want to write two html table columns also to this file. For example column values: sw1: 0.15, 0.2, 0.3 and krw: 100, 200 ,300 I want to write this values in this format to the txt file: krw=0.15%100++0.2%200++0.3%300

This is my foreach loop:

foreach ($_POST['sw1'] as $key => $value) {
        if (!empty($_POST['sw1'][$key]) && ($_POST['krw'][$key])) {
            $_POST['sw1'][$key];
            $_POST['krw'][$key];
        }
    };

The foreach should be implemented after 'krw='. in the $data variable. Can somebody please point me in the right direction or with examples?

EDIT I've figured it out, this is now my code:

function toString()
{

    $filename = 'D:\file_' . time() . '_' . $_SESSION['username'] . '.txt';
    $tableKrw = "";
    foreach ($_POST['sw1'] as $key => $value) {
        if (!empty($_POST['sw1'][$key]) && ($_POST['krw'][$key])) {
            $sw1=$_POST['sw1'][$key];
            $sw2=$_POST['krw'][$key];
            $tableKrw .= $sw1 .'&'. $sw2.'+';
        }
    };

    $data = 'grid_blocks=' . $_POST["grid_blocks"] . "\n" .
        'leverettj=' . $_POST["leverettj"] . "\n" .
        'length=' . $_POST["length"] . "\n" .
        'krw=' . $tableKrw . "\n";

    $file = file_put_contents($filename, $data, FILE_APPEND | LOCK_EX);

2 Answers2

1

Sure, here is an example of how to foreach() through the $_POST array and write the values to a text file.

<form method="POST">
  First name:<br>
  <input type="text" name="firstname"><br>
  Last name:<br>
  <input type="text" name="lastname">
  <input type="submit">
</form> 

<?php
$output = "";

if (isset($_POST)) {
    foreach ($_POST as $key => $value) {
        $output .= "Key: {$key} Value: {$value} \n";
    }
}

$save_dir = "temp/";
file_put_contents($save_dir . "log.txt", $output);
AgileFox
  • 174
  • 6
  • Thank you for your input, but this is not what I'm looking for, I know this format. To clarify I edited my question. –  Mar 03 '19 at 21:31
0

You need something like this...

$stringToAppend = "";
foreach ($_POST['sw1'] as $key => $value) {
        if (!empty($_POST['sw1'][$key]) && ($_POST['krw'][$key])) {
            $_POST['sw1'][$key];
            $_POST['krw'][$key];
            $stringToAppend .= $_POST['sw1'][$key];
        }
    };

$data .= $stringToAppend;

This will create a new string (you can add any values to the $stringToAppend using the .= operator), and append this string to the end of your $data string.

  • Thank you for your input, but I tried this. My output is then for example: krw=0.15.2.3[]. I edited my question to clarify. –  Mar 03 '19 at 21:34