0

I have a form with quite a lot of input fields and checkboxes.

When I submit the form I want to write the form field name and value to a text file.

<input type="text" name="mailHost" value=""/>
<input type="text" name="mailUser" value=""/>
<input type="text" name="mailPass" value=""/>

So with this as an example it would be written to a file as :

mailHost = VALUE
mailUser = VALUE
mailPass = VALUE

For a few form fields it fine doing each on by hand, but is there a function or way to do this for numerous fields ?

And then the same for reading it back ?

Again using the same example above I'd end up with the following when read back :

$mailHost = Value, $mailUser = value etc where the variable name is dynamically created and the value assigned ?

Thanks

1 Answer 1

2

You can iterate through your $_POST array:

foreach ($_POST as $key => $value) {
   file_put_contents('file.txt', $key . " = " . $value . "\n", FILE_APPEND);
}

NOTE

Unchecked checkboxes will not in the $_POST array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - how about reading it back so I get the variable name as the $key ?
Check the file(), expolde() function in PHP manual, and read some about Arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.