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