使用表单保存多个会话变量 [英] Save multiple session variables with a form

查看:58
本文介绍了使用表单保存多个会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个表格,其中包含维度字段,客户可以在其中填写并提交这些字段,并将其保存在会话变量中.到目前为止,我成功完成了1个表格,但是在节省了1次字段数据之后,对于客户而言,他可以再次填写表格(针对维度),再填写另一个表格,等等.

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)

表格:

<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>

用于在$ _SESSION中保存数据的PHP:

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; ?>  

因此,这适用于1个提交,如果我提交表单,则第一个会话变量将被新数据刷新,但是现在我需要执行一些操作,以便客户可以添加多个维度集并保存在会话中.

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.

我的想法是在提交每个表单后,将字段的每个名称更改为_1 _2 _3.但是我不知道如何解决这个问题,所以我希望有人能给我一些建议.

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?

谢谢!

推荐答案

您可以使用多维会话数组:

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'];

假设用户在wz_saving_b中输入以下内容:

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

123

<?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);
?>

输出:

    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)


所请求的抽象:

<?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)


根据提供的代码,这应该可行,我尝试在位置x上输入一次12 mm,然后在位置y上输入45 mm,输出为:


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

代码:

<?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; ?>

这篇关于使用表单保存多个会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆