duanhan5230 2013-11-22 12:13
浏览 49
已采纳

使用表单保存多个会话变量

I am working on a form with fields for dimensions where a customer can fill these fields in and submit them and they will be saved in session variables. So far I succeeded 1 form, but after saving 1 time the data of the fields it needs to be possible for the costumer that he can fill in a form again (for dimensions) and still another and another etc etc.

(I started the session in top of my header)

The form:

<form method="POST">

   <label>A:</label>
   <input name="wz_saving_a" type="text" />

   <label>B:</label>
   <input name="wz_saving_b" type="text" />

   <input name="wz_submit_saving_1" type="submit" class="add_button" value="Add"  />

</form>

PHP for saving data in $_SESSION:

if(isset($_POST['wz_submit_saving_1'])) :

    // Save submit
    $_SESSION['wz_submit_saving_1'] = $_POST['wz_submit_saving_1'];

    // Save wz_saving_a in session
    $_SESSION['wz_saving_a'] =  $_POST['wz_saving_a'];

    // Save wz_saving_b in session
    $_SESSION['wz_saving_b'] = $_POST['wz_saving_b'];

endif;

After submit I show the submitted data to the costumer like:

<?php if(isset($_SESSION['wz_submit_saving_1'])) : ?>

<div id="wz_config_1" class="wz_config">

<ul>
   <li>Your dimensions:</li>
   <li>A: <?php if(isset($_SESSION['wz_saving_a'])) : echo $_SESSION['wz_saving_a']; endif; ?> mm</li>
   <li>B: <?php if(isset($_SESSION['wz_saving_b'])) : echo $_SESSION['wz_saving_b']; endif; ?> mm</li>
</ul>   

<?php endif; ?>  

So this works for 1 submit and if I submit the form the session variables of the first will be refreshed by the new data, but now I need something to do so the costumer can add multiple dimensions sets and save in the Session.

My idea was to change every name of a field by _1 _2 _3 after each form submit. But I don't know how to fix this so I hope someone can give me some advice.

I can give the url of my example if you want?

Thanks!

  • 写回答

1条回答 默认 最新

  • duanrong0738 2013-11-22 12:21
    关注

    You can use multidimensional session arrays:

    $_SESSION['wz_saving_b'][$_POST['wz_saving_b']] = $_POST['wz_saving_b'];
    

    Or, just use [] to add new keys, but you are going to have duplicated values

    $_SESSION['wz_saving_b'][] = $_POST['wz_saving_b'];
    

    Let's say the user types in wz_saving_b the following things:

    1, 2, 3

    <?php
    session_start();
    ?>
    <form method="POST" action="">
    
       <label>A:</label>
       <input name="wz_saving_a" type="text" />
    
       <label>B:</label>
       <input name="wz_saving_b" type="text" />
    
       <input name="wz_submit_saving_1" type="submit" class="add_button" value="Add"  />
    
    </form>
    <?php
    if(isset($_POST['wz_submit_saving_1'])):
        $_SESSION['wz_saving_b'][$_POST['wz_saving_b']] = $_POST['wz_saving_b'];
    ?>
    <div id="wz_config_1" class="wz_config">
    
    <ul>
       <li>Your dimensions:</li>
       <li>B: <?php if(isset($_SESSION['wz_saving_b'])): foreach($_SESSION['wz_saving_b'] as $k => $v) { echo "$v "; } endif; ?> mm</li>
    </ul>
    <?php endif; ?>
    
    
    <?php
    var_dump($_SESSION);
    ?>
    

    Output:

        Your dimensions:
        B: 1 2 3 mm
    
    array (size=1)
      'wz_saving_b' => 
        array (size=3)
          1 => string '1' (length=1)
          2 => string '2' (length=1)
          3 => string '3' (length=1)
    

    The requested abstraction:

    <?php
    session_start();
    ?>
    <form method="POST" action="">
    
       <label>A:</label>
       <input name="wz_saving_a" type="text" />
    
       <label>B:</label>
       <input name="wz_saving_b" type="text" />
    
       <label>C:</label>
       <input name="wz_saving_c" type="text" />
    
       <label>D:</label>
       <input name="wz_saving_d" type="text" />
    
       <input name="wz_submit_saving_1" type="submit" class="add_button" value="Add"  />
    
    </form>
    <?php
    if(isset($_POST['wz_submit_saving_1'])) {
        foreach($_POST as $key => $value) {
            if($key != 'wz_submit_saving_1') {
                $_SESSION[$key][] = $value;
            }
        }
    }
    ?>
    <div id="wz_config_1" class="wz_config">
    
    <ul>
       <li>Your dimensions:</li>
       <?php foreach($_SESSION as $k => $v): ?>
        <?php foreach($v as $saving => $wz): ?>
       <li><?= strtoupper(substr($k, 10));?> : <?=$wz;?> mm</li>
        <?php endforeach; ?>
       <?php endforeach; ?>
    </ul>
    
    
    <?php
    var_dump($_SESSION);
    ?>
    

    I did some random inputs here with some numbers. The output is:

        Your dimensions:
        A : 1 mm
        A : 6 mm
        A : 5 mm
        B : 1 mm
        B : 6 mm
        B : 5 mm
        C : 4 mm
        C : 8 mm
        C : 5 mm
        D : 4 mm
        D : 7 mm
        D : 5 mm
    
    array (size=4)
      'wz_saving_a' => 
        array (size=3)
          0 => string '1' (length=1)
          1 => string '6' (length=1)
          2 => string '5' (length=1)
      'wz_saving_b' => 
        array (size=3)
          0 => string '1' (length=1)
          1 => string '6' (length=1)
          2 => string '5' (length=1)
      'wz_saving_c' => 
        array (size=3)
          0 => string '4' (length=1)
          1 => string '8' (length=1)
          2 => string '5' (length=1)
      'wz_saving_d' => 
        array (size=3)
          0 => string '4' (length=1)
          1 => string '7' (length=1)
          2 => string '5' (length=1)
    

    Based on the provided code, this should work, I tried to input one time 1 and 2 mm on position x, and then 4 and 5 mm on position y, the output was:

    Rechte sparing 1
    
            Formaat van de sparing:
            A: 1 mm
            B: 2 mm
            Positionering van de sparing:
            x
    
    Rechte sparing 1
    
            Formaat van de sparing:
            A: 4 mm
            B: 5 mm
            Positionering van de sparing:
            y
    

    Code:

    <?php
    
    if(isset($_POST['wz_submit_saving_1'])) :
    
        $straight_saving = array(
            'wz_str_saving' => $_POST['wz_str_saving'],
            'wz_saving_a' => $_POST['wz_saving_a'],
            'wz_saving_b' => $_POST['wz_saving_b'],
            'wz_submit_saving_1' => $_POST['wz_submit_saving_1']
        );
    
        $_SESSION['straight_saving'][] = $straight_saving;
    
    endif;
    
    
    if(isset($_SESSION['straight_saving'])) : 
    
        foreach($_SESSION['straight_saving'] as $sav) {
    
    ?>
    
    <div class="wz_config">
    
        <h3>Rechte sparing 1</h3>
    
    
        <ul>
            <li>
                <ul>
                    <li>Formaat van de sparing:</li>
                    <li>A: <?php echo $sav['wz_saving_a']; ?> mm</li>
                    <li>B: <?php echo $sav['wz_saving_b']; ?> mm</li>
                </ul>
            </li>
    
            <li>
                <ul>
                    <li>Positionering van de sparing:</li>
                    <li><?php echo $sav['wz_str_saving']; ?></li>
                </ul>
            </li>
        </ul>
    
        <div class="clear"></div>
    
    </div><!--End wz_config_1-->
    
    <?php } endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来