I want my user to be able to submit a form, and then those form values are stored in a session. The form posts back to itself, so how can I store the data time and time again in a session?
<form action="addPerson.php" method="post">
<input required="required" placeholder="Name" name="personname"/>
<label for="gender">Male or Female?</label>
<select id="gender" name="gender">
<option value="f">Female</option>
<option value="m">Male</option>
</select>
<button type="submit">add person</button>
</form>
So I was thinking:
$_SESSION["personname"] = $_POST['personname'];
$_SESSION["persongender"] = $_POST['gender'] ;
But this would get replaced every time a new form is posted.
$_SESSION['people'][] = array(
'personname' => $_POST['personname'],
'persongender' => $_POST['gender']
);
What about:
$_SESSION["personname"][] = $_POST['personname'];
$_SESSION["persongender"][] = $_POST['gender'] ;
The values are stored in an array that way.
Or:
$_SESSION['persons'][] = array( $_POST['personname'], $_POST['gender']);
I would do it like this.
$_SESSION[] = array("personname" => $_POST['personname'],
"persongender" => $_POST['gender']) ;
This way you get a multidemensional array and you can loop through it if you want to access an old value.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.