2013-11-25 42 views
1

我有一些数据的文本文件加载输入数据所做的更改,那我传递到一些投入的形式提交从文本文件

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8"> 
</head> 
<body> 
<form action="edit_subjects.php" method="post"> 
    <?php 
    $content = file('../db/test.txt'); 
    foreach($content as $eachLine) { 
     list($subject, $type, $description) = explode('|', $eachLine); 
     echo '<label for="subject">Subject:</label>'; 
     echo '<input type="text" name="subject" id="subject" value="'.$subject.'"><br>'; 
     echo '<label for="type">Type:</label>'; 
     echo '<input type="text" name="type" id="type" value="'.$type.'"><br>'; 
     echo '<label for="description">Description:</label><br>'; 
     echo '<textarea name="description" id="description">'.$description.'</textarea><br>'; 
    } 
    ?> 
    <input type="submit"> 
</form> 
</body> 
</html> 

运行代码给出了这样的输出:

<label for="subject">Subject:</label> 
<input type="text" name="subject" id="subject" value="Subject 1"><br> 
<label for="type">Type:</label> 
<input type="text" name="type" id="type" value="Type1"><br> 
<label for="description">Description:</label><br> 
<textarea name="description" id="description">Description1 
</textarea><br> 

<label for="subject">Subject:</label> 
<input type="text" name="subject" id="subject" value="Subject 2"><br> 
<label for="type">Type:</label> 
<input type="text" name="type" id="type" value="Type2"><br> 
<label for="description">Description:</label><br> 
<textarea name="description" id="description">Description2 
</textarea><br> 

<label for="subject">Subject:</label> 
<input type="text" name="subject" id="subject" value="Subject 3"><br> 
<label for="type">Type:</label> 
<input type="text" name="type" id="type" value="Type3"><br> 
<label for="description">Description:</label><br> 
<textarea name="description" id="description">Description3 
</textarea><br> 

正如你所看到的名字一遍又一遍的去,所以我怎么会单独访问每个组输入,并将其写入文件

+0

你会这样做edit_subjects.php - 或者这是一个自引用形式? – hammus

+0

我明白我会做edit_subjects.php,但我不知道该怎么做 – svendjokumsen

+0

好的一秒钟,我会发布一个解决方案 – hammus

回答

0

这里是edit_subjects一个解决方案.php:

如果打算将表单项打印出来,然后在提交表单时发送它们,则需要更改每个循环的name=值。你可以使用和递增的数字是这样的:

$i = 1; 

foreach($content as $eachLine) { 
     list($subject, $type, $description) = explode('|', $eachLine); 
     echo '<label for="subject">Subject:</label>'; 
     echo '<input type="text" name="subject' . $i .'" id="subject" value="'.$subject.'">br>'; 
     echo '<label for="type">Type:</label>'; 
     echo '<input type="text" name="type' . $i .'" id="type" value="'.$type.'"><br>'; 
     echo '<label for="description">Description:</label><br>'; 
     echo '<textarea name="description' . $i .'" id="description">'.$description.'</textarea><br>'; 
     $i++ 
    } 

OR你可以把你的形式name s转换阵列:

echo '<input type="text" name="subject[]" id="subject" value="'.$subject.'"> 

这将需要你遍历他们edit_subjects.php得到价值。

然后,访问您的窗体值从$ _POST阵列(请记住你应该逃避所有值):

if (isset($_POST['subject']) 
    { 
     $subject = $_POST['subject']; 
     $type = $_POST['type']; 
     //your other form values here... 
     $result = $subject . "," . $type //or however you want to structure it 
     $file = fopen("../db/test.txt", "w"); 
     fwrite($file, $result); 
    } else { 
     echo "POST variables not set!"; 
     die; 
    } 

,因为如果你使用数组访问您的POST值的一个例子是:

if (isset($_POST['subject']) 
    { 
     $file = fopen("../db/test.txt", "w"); 
     $i = 0; 

     foreach($_POST['subject'] as $array) 
     { 
      $result = $_POST['subject'][$i] . "," . $_POST['type'][$i] ."\r\n"; 
      fwrite($file, $result); 
      $i++ 
     } 


    } else { 
     echo "POST variables not set!"; 
     die; 
    } 
+0

事情是输入名称一遍又一遍,这样只会提交最后一组输入 – svendjokumsen

+0

我会把它们全写到一个文件吗? – svendjokumsen

+0

@RHNorskov我不知道你是最后一个问题的意思,请重新说明 – hammus

0

在文件edit_subjects.php你在传递给阅读与$ _ POST参数[“主题”],...然后加入正确的格式($结果)的字符串变量,并将其与下面的代码写回:

$fp = fopen("../db/test.txt", "r+"); 
if (flock($fp, LOCK_EX)) { 
    ftruncate($fp, 0);       
    fwrite($fp, $result); 
    fflush($fp);            
    flock($fp, LOCK_UN);     
} else { 
    echo "Couldn't get the lock!"; 
} 
fclose($fp);