2016-03-03 89 views
0

對不起,我的英語!我在stackoverflow上找到了下面的代碼,我只是嘗試添加更多的字段(我需要4)而沒有成功。我的兩個其他領域保持blanck我的data.txt。我怎樣才能輕鬆添加2個字段?預先感謝您的幫助。PHP寫txt文件與多個領域

形式:

<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'] . "\n"; 
$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'); 
} 
+0

我只在這裏看到2個輸入字段?你在哪裏添加了其他兩個?在形式? – Rizier123

+0

其中「其他2個領域」? – syck

+0

oups,我複製/粘貼的原代碼。我試圖加2場在形式和PHP文件,這樣的:「如果(isset($ _ POST [ '字段1'])&& isset($ _ POST [ '域2']))&& isset($ _ POST [ '字段3' ]))&& isset($ _ POST [ '字段4'])){ – Francky

回答

0

如果4輸入在那裏,說: -

<input name="field1" type="text" /> 
<input name="field2" type="text" /> 
<input name="field3" type="text" /> 
<input name="field4" type="text" /> 

然後更改

if(isset($_POST['field1']) && isset($_POST['field2'])) { 
    $data = $_POST['field1'] . '-' . $_POST['field2'] . "\n"; 
    ... 
    ... 
} 

if(isset($_POST['field1']) && isset($_POST['field2']) && isset($_POST['field3']) && isset($_POST['field4'])) { 
    $data = "{$_POST['field1']}-{$_POST['field2']}-{$_POST['field3']}-{$_POST['field4']} \n"; 
    ... 
    ... 
} 
+0

謝謝你,這是正確的。 – Francky

0

您需要的字段添加到HTML表單太:

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

而且,除了將它們添加到if語句,將其添加到從獲取的數據線$_POST變量:

$data = $_POST['field1'] . '-' . $_POST['field2'] . '-' . $_POST['field3'] . '-' . $_POST['field4'] . "\n";