繁体   English   中英

从文本文件为多个输入类型文本字段创建值

[英]Creating value to several input type text fields from text file

我有100个输入类型的文本框,并希望从txt文件显示每个框的值。 我无法在PHP中弄清楚,并认为在JavaScript中可能会更容易。 但是我不熟悉JavaScript。 我的文本字段显示为:

<input type="text" size="13" name="contacts[]" id="contact0">
<input type="text" size="13" name="contacts[]" id="contact1">
<input type="text" size="13" name="contacts[]" id="contact2">
<input type="text" size="13" name="contacts[]" id="contact3">

因此,我需要从contacts.txt中逐行添加人名的文本框添加值。 显示为:

<?php include 'includethis.php' ?>
<input type="text" size="13" name="contacts[]" id="contact0" value="David">
<input type="text" size="13" name="contacts[]" id="contact1" value="Erick">
<input type="text" size="13" name="contacts[]" id="contact2" value="John">
<input type="text" size="13" name="contacts[]" id="contact3" value="Frank">

这是includethis.php文件,该文件写入所有名称以将名称标签替换为index.php

$filename = 'pics.txt';
$handle = fopen($filename, 'r');
$datain = fread($handle, filesize($filename));
$names_array = explode("\n", $datain);

$count = 0;
$counter = 0;
foreach($names_array as $show){
if($count < 4)
{
echo '<img src="images/'.$show.'">';
$count++;

}
else
{

$count = 0;   
echo '<br><input type="text" size="13" name="contacts[]" id="contact'.$counter++.'"><input type="text" size="13" name="contacts[]" id="contact'.$counter++.'"><input type="text" size="13" name="contacts[]" id="contact'.$counter++.'">';
echo '<br>'.'<img src="images/'.$show.'">';
$count++;
}
}
$filename = 'pics.txt';
$handle = fopen($filename, 'r');
$datain = fread($handle, filesize($filename));
$names_array = explode("\n", $datain);
//contacts.txt
$filename2 = "contacts.txt";
$contact_data = file_get_contents($filename2);
$contact_array = explode("\n",$contact_data);
$contact_max = count($contact_array) - 2;
$count = 0;
$counter = 0;
foreach($names_array as $show){
if($count < 4) {
echo '<img src="images/'.$show.'">';
}
else
{
echo '<br>';
for($y=0;$y<3;$y++) {
  if($counter < $contact_max) {
    $contact = (isset($contact_array[$counter]))?$contact_array[$counter]:"";
    echo '<input type="text" size="13" name="contacts[]" value="'.$contact.'" id="contact'.$counter.'" />';
    $counter++;
  }
}

echo '<br>'.'<img src="images/'.$show.'">';
$count=0;
}
$count++;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM