Enter Strandman
Enter Strandman

Reputation: 339

PHP How to stop variable from being rewritten when form resubmitted

I have 2 forms, the first obtains the ID of a person in table of a database. The second gets a name and/or address to update for that ID. I am losing the value of ID when the page is refreshed. Below is how I have declared and posted a value to the id. How can I stop this from being rewritten when the next form is submitted and the page refreshed? (the session_start() is at the top of the document)

 $_SESSION['ID'] = $_POST['id'];
 $id = $_SESSION['ID']; 

Form 1:

 <form method="post">
    <p style="margin-top: 70px;">Please type the ID of the person you wish to add to change their data</p>
    <p style="margin-bottom: 0px;">ID</p>
    <input style="color:black" type="text" name="id" placeholder="10001">
    <input style="color:lightblue;background-color: rgb(80,80,80);margin-top: 7px;" type="submit" value="Submit">
</form>

Form 2:

 <form method="post">
    <p>New Information for Customer with ID entered above</p>
    <input style='color:black;' type="text" name="newName" placeholder="New Name">
    <input style="color:black;" type="text" name="newAddress" placeholder="New Address">
    <input style="color:lightblue;background-color: rgb(80,80,80);margin-top: 7px;" type="submit" value="Submit">
</form>

Upvotes: 0

Views: 71

Answers (1)

Philipp Maurer
Philipp Maurer

Reputation: 2505

Since both forms are missing the action attribute for the form i assume, that both are on the same page. This leads to the problem, that you can only submit one form at a time. The moment you submit the second form the $_POST data will be set to the fields available in that form and therefore $_POST['id'] is removed. Since you seem to always call $_SESSION['ID'] = $_POST['id']; you will rewrite it with NULL and therefore delete the entry. You could instead check if the id was really submitted:

if (array_key_exists('id', $_POST)) {
    $_SESSION['ID'] = $_POST['id'];
}
$id = $_SESSION['ID'];

Like this your session will keep the id.

Alternatively, because you do not have any fields in your second form, that need to load data from the specified user, you can also merge the input fields to your first form. Like this, the id will always be submitted again and kept, because it is part of the submitted form.

<form method="post">
    <p style="margin-top: 70px;">Please type the ID of the person you wish to add to change their data</p>
    <p style="margin-bottom: 0px;">ID</p>
    <input style="color:black" type="text" name="id" placeholder="10001">
    <p>New Information for Customer with ID entered above</p>
    <input style='color:black;' type="text" name="newName" placeholder="New Name">
    <input style="color:black;" type="text" name="newAddress" placeholder="New Address">
    <input style="color:lightblue;background-color: rgb(80,80,80);margin-top: 7px;" type="submit" value="Submit">
</form>

In an addition you should submit the given values back to the user, so the form keeps the fields:

<input style="color:black" type="text" name="id" value="<?= $id ?? '' ?>" placeholder="10001">
<input style="color:black;" type="text" value="<?= array_key_exists('newAddress', $_POST) ? $_POST['newAddress'] : '' ?>" name="newAddress" placeholder="New Address">
...

Upvotes: 2

Related Questions