dpwqicw157673 2015-03-11 13:56
浏览 43

即使在使用javascript和php提交按钮后,获取所有输入字段并在文本字段中保留值

I have multiple input fields and random input names, what I want to achieve is get the data entered in the text fields but then on clicking on the submit button, the data entered will remain.

Sample code:

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

The problem here is that I don't know what are the 'input names'. I want the values entered by user into this text fields remain. How would I do that? If you don't know what the 'input names' are?

  • 写回答

2条回答 默认 最新

  • douji2520 2015-03-11 14:19
    关注

    Can you use hidden inputs to keep tracks of the names? Like this:

    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.'"/>';
    }
    

    where getValueFor should be a function which checks what you got from GET or POST. For example:

    function getValueFor($x){
        $res = "";
            if (isset($_REQUEST[$x])){
                $name=$_REQUEST[$x];
                $res = $_REQUEST[$name];
            }
        return res;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?