doushi3322 2013-06-10 05:43
浏览 87

PHP回显和表单值

I've completed a form (albeit not very well) and am currently trying to work out a 'confirmation' page in php that displays what the user will print after it's filled out and submitted. I've declared all the variables in php for every input field on the form - however, I have a section of the form that dynamically replicates and increments it's class numbers in jQuery. For reference:

$('#replicate').click(function(){
    var $cloned = $('.container').clone();
    $cloned.find('input').val('');
    $cloned.appendTo($('.emptyContainer'));
    var container = $(".emptyContainer div").length;
    var containerNumber = container + 1;
    var containerClass = 'container' + containerNumber;
    $(".emptyContainer .container1").attr("class", containerClass);

So what the above does is that when the user hits the replicate button the container with all the input fields, well, replicates - and the new container that appears has a class number of '.container2', '.container3', etc. for as many times as the user needs. The input ids and names within each new container also increment accordingly. I've gone about declaring all the php variables for the first container - so if the first input id is "firstname1" the php variable is $firstname1 on my confirmation page.

What I'm trying to figure out, is how do I go about echoing out each new input on the confirmation page when "#firstname2", "#firstname3", get generated? Would that be something like a for loop? Excuse my ignorance, never done much in php.

  • 写回答

1条回答 默认 最新

  • dongziya9863 2013-06-10 05:50
    关注

    You would use a foreach loop against either $_GET or $_POST depending on the method you used.

    Assuming you POSTed the form you could do something like.

    foreach($_POST as $fieldName=>$fieldValue){
          echo $fieldName." = ".$fieldValue."<br/>";
    }
    

    A preferred method for this though would be when constructing the names of your fields use a name like myfield[1] instead of myfield1.

    PHP will turn the square bracket notation into an array.

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大