dounangshen6553 2014-01-09 17:17
浏览 82
已采纳

将表单变量作为数组传递

I'm building a multi-page form which I want to commit to the database at the very end of the process, so abandoned forms don't end up littering the data with incomplete junk records, so my initial thought to solve this problem would be to hold the already-submitted data in an array (almost in the same format as it would be had I SELECTed it out of the database), but I seem to be getting pretty confused and ending up with nested arrays and not getting the results I expect when debugging.

So, for example, the first form looks something like this:

<form action="step_2.php" method="post">
    <input type="text" name="textfield1">
    <input type="text" name="textfield2">
    <input type="text" name="options[]">
</form>

(The reason for the array field is since the user can programatically add additional fields, so whilst 'textfield1' & 'textfield2' only has one value, 'options' should be submitted as an array).

What I want to do it just grab the $_POST array and add it to the next field, something like this:

<form action="step_3.php" method="post">
    <input type="hidden" name="form1[]" value="<?=$_POST?>">
    <input type="text" name="textfield3">
    <input type="text" name="options2[]">
</form>

But that doesn't seem to work. Ideally I'd like to end up on the last step of the form either with an array for each form, or a single array organised in a sensible way, e.g.;

[0] => Array
    (
        [textfield1] => Some Text
        [textfield2] => More Text
        [textfield3] => Yet More Text
        [options1] => Array
            (
                [0] => Option A
                [1] => Option B
            )
        [options2] => Array
            (
                [0] => Option E
            )
    )

I'm assuming there's an easier way to do this, but currently my brain is turning to goo trying to wrap a synapse around it.

  • 写回答

1条回答 默认 最新

  • dourao3960 2014-01-09 17:23
    关注

    Consider saving the form data in a $_SESSION variable so you aren't passing it back and forth between pageloads.

    For example:

    if( !isset($_SESSION['formdata'])) $_SESSION['formdata'] = array();
    // to add current form data to the array:
    $_SESSION['formdata'] = array_merge($_SESSION['formdata'],$_POST);
    

    This has the added bonus of still working just fine if the user hits the Back button and resubmits an earlier part of the form (it will overwrite the previously saved values)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳