Sorry for my english! I found the below code on stackoverflow and I just try to add more field (I need 4) without success. My 2 other fields stay blanck on my data.txt. How can I easily add 2 fields ? Thank you in advance for your help.
Form :
<form action="myprocessingscript.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input type="submit" name="submit" value="Save Data">
<?php
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "
";
$ret = file_put_contents('/tmp/mydata.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
else {
die('no post data to process');
}
if 4 input is there, say:-
<input name="field1" type="text" />
<input name="field2" type="text" />
<input name="field3" type="text" />
<input name="field4" type="text" />
then change
if(isset($_POST['field1']) && isset($_POST['field2'])) {
$data = $_POST['field1'] . '-' . $_POST['field2'] . "
";
...
...
}
to
if(isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3']) && isset($_POST['field4']) ) {
$data = "{$_POST['field1']}-{$_POST['field2']}-{$_POST['field3']}-{$_POST['field4']}
";
...
...
}
You need to add the fields to the HTML form too:
<form action="myprocessingscript.php" method="POST">
<input name="field1" type="text" />
<input name="field2" type="text" />
<input name="field3" type="text" />
<input name="field4" type="text" />
<input type="submit" name="submit" value="Save Data">
And, besides adding them to the if statement, add them to the line that gets the data from the $_POST
variable:
$data = $_POST['field1'] . '-' . $_POST['field2'] . '-' . $_POST['field3'] . '-' . $_POST['field4'] . "
";