我有多个输入字段和随机输入名称,我想要实现的是获取在文本字段中输入的数据,然后单击提交按钮,输入的数据将保留。
示例代码:
for($i=0;$i<$count1;$i++) {
echo '<input type="text" name="'.$random1.'" value=""/>';
}
for($j=0;$j<$count2;$j++) {
echo '<input type="text" name="'.$random2.'" value=""/>';
}
for($k=0;$k<$count3;$k++) {
echo '<input type="text" name="'.$random3.'" value=""/>';
}
echo '<input type="submit" name="submit" value="submit"/>'
这里的问题是我不知道什么是"输入名称"。我希望用户在此文本字段中输入的值保持不变。我该怎么做?如果您不知道"输入名称"是什么?
您可以使用隐藏的输入来跟踪名称吗?喜欢这个:
for($i=0;$i<$count1;$i++) {
$value=getValueFor("section1-$i");
echo '<input type="text" name="'.$random1.'" value="'.$value.'"/>';
echo '<input type="hidden" name="section1-'.$i.'" value="'.$random1.'"/>';
}
for($j=0;$j<$count2;$j++) {
$value=getValueFor("section2-$j");
echo '<input type="text" name="'.$random2.'" value="'.$value.'"/>';
echo '<input type="hidden" name="section2-'.$j.'" value="'.$random2.'"/>';
}
for($k=0;$k<$count3;$k++) {
$value=getValueFor("section3-$k");
echo '<input type="text" name="'.$random3.'" value="'.$value.'"/>';
echo '<input type="hidden" name="section3-'.$k.'" value="'.$random3.'"/>';
}
其中getValueFor应该是一个函数,用于检查您从GET或POST获得的内容。例如:
function getValueFor($x){
$res = "";
if (isset($_REQUEST[$x])){
$name=$_REQUEST[$x];
$res = $_REQUEST[$name];
}
return res;
}
这应该有效:
if (isset($_POST['random'])) {
foreach ($_POST['random'] as $key => $randomName) {
${"random" . $key . "value"} = $randomName;
}
}
for($i=0;$i<$count1;$i++) {
echo '<input type="text" name="'.$random1.'" value="'.$random1value.'"/>';
echo '<input type="hidden" name="random[]" value="'.$random1.'"/>';
}
for($j=0;$j<$count2;$j++) {
echo '<input type="text" name="'.$random2.'" value="'.$random2value.'"/>';
echo '<input type="hidden" name="random[]" value="'.$random2.'"/>';
}
for($k=0;$k<$count3;$k++) {
echo '<input type="text" name="'.$random3.'" value="'·$random3value.'"/>';
echo '<input type="hidden" name="random[]" value="'.$random3.'"/>';
}
echo '<input type="submit" name="submit" value="submit"/>'`enter code here`;
}