Good day all, I have the form shown below but I noticed that whenever the form input couldn’t get inserted in the database, an error message is shown which is ok by me but how will I do it to make the previously filled data appear in their respective fields when I click the back button? Please note that I use $_SESSION and in the index page that processes the form, I used “include” to display the error message. Thanks for your anticipated contribution/solution.
The form:
<form action="index.php" method="post">
<fieldset>
<legend>Personal Information</legend>
<div class="row">
<div class="large-3 columns">Title</div>
<div class="large-3 columns"><input type="text" name="title" id="title" <?php if (isset($title)) { echo 'value="' . htmlentities($title, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
<div class="large-3 columns">First Name</div>
<div class="large-3 columns"><input type="text" name="firstName" id="firstName" <?php if (isset($firstName)) { echo 'value="' . htmlentities($firstName, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
</div>
<div class="row">
<div class="large-3 columns">Last Name</div>
<div class="large-3 columns"><input type="text" name="lastName" id="lastName" <?php if (isset($lastName)) { echo 'value="' . htmlentities($lastName, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
<div class="large-3 columns">Middle Name</div>
<div class="large-3 columns"><input type="text" name="middleName" id="middleName" <?php if (isset($middleName)) { echo 'value="' . htmlentities($middleName, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
</div>
<div class="row">
<div class="large-3 columns">Gender</div>
<div class="large-3 columns"><select name="gender">
<option value="">Select Your Gender</option>
<option value="Male" <?php if(isset($gender) && $gender == 'Male') echo 'selected'; ?> >Male</option>
<option value="Female" <?php if(isset($gender) && $gender == 'Female') echo 'selected'; ?> >Female</option>
</select></div>
<div class="large-3 columns">Marital Status</div>
<div class="large-3 columns"><select name="maritalStatus">
<option value="">Select Your Status</option>
<option value="Married" <?php if(isset($maritalStatus) && $maritalStatus == 'Married') echo 'selected'; ?> >Married</option>
<option value="Single" <?php if(isset($maritalStatus) && $maritalStatus == 'Single') echo 'selected'; ?> >Single</option>
<option value="Widowed" <?php if(isset($maritalStatus) && $maritalStatus == 'Widowed') echo 'selected'; ?> >Widowed</option>
<option value="Separated" <?php if(isset($maritalStatus) && $maritalStatus == 'Separated') echo 'selected'; ?> >Separated</option>
<option value="Divorced" <?php if(isset($maritalStatus) && $maritalStatus == 'Divorced') echo 'selected'; ?> >Divorced</option>
</select></div>
</div>
<div class="row">
<div class="large-3 columns">Date of Birth</div>
<div class="large-3 columns"><input type="text" name="dateOfBirth" id="datePicker" <?php if (isset($dateOfBirth)) { echo 'value="' . htmlentities($dateOfBirth, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
<div class="large-3 columns">Wedding Anniversary</div>
<div class="large-3 columns"><input type="text" name="weddingAnniversary" id="datePicker2" <?php if (isset($weddingAnniversary)) { echo 'value="' . htmlentities($weddingAnniversary, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
</div>
<div class="row">
<div class="large-3 columns">Occupation</div>
<div class="large-3 columns"><input type="text" name="occupation" id="occupation" <?php if (isset($occupation)) { echo 'value="' . htmlentities($occupation, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
<div class="large-3 columns">State of Origin</div>
<div class="large-3 columns"><input type="text" name="stateOfOrigin" id="stateOfOrigin" <?php if (isset($stateOfOrigin)) { echo 'value="' . htmlentities($stateOfOrigin, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
</div>
<div class="row">
<div class="large-3 columns">Nationality</div>
<div class="large-3 columns"><input type="text" name="nationality" id="nationality" <?php if (isset($nationality)) { echo 'value="' . htmlentities($nationality, ENT_COMPAT, 'UTF-8') . '"'; } ?> /></div>
<div class="large-3 columns"> </div>
<div class="large-3 columns"> </div>
</div>
</fieldset>
<div class="row">
<div class="large-7 columns">
<input type="hidden" name="id" <?php if (isset($id)) { echo 'value="' . htmlentities($id, ENT_COMPAT, 'UTF-8') . '"'; } ?> />
<input type="reset" name="button" value="Reset" class="button round"/>
<input type="submit" name="button" value="Register New Member" class="button round"/>
</div>
<div class="large-5 offset-5 columns"> </div>
</div>
</form>
The index.php
if(isset($_POST['button']) && $_POST['button'] == 'Register New Member')
{
$conn = DatabaseManager::getConnection();
try
{
$sql = "INSERT INTO members ( affiliation, title, firstName, middleName, lastName, gender, maritalStatus, occupation, stateOfOrigin, nationality )
VALUES ( :affiliation, :title, :firstName, :middleName, :lastName, :gender, :maritalStatus, :occupation, :stateOfOrigin, :nationality)";
$st = $conn->prepare ( $sql );
$st->bindValue( ":affiliation", $affiliation);
$st->bindValue( ":title", $_POST['title'], PDO::PARAM_STR );
$st->bindValue( ":firstName", $_POST['firstName'], PDO::PARAM_STR );
$st->bindValue( ":middleName", $_POST['middleName'], PDO::PARAM_STR );
$st->bindValue( ":lastName", $_POST['lastName'], PDO::PARAM_STR );
$st->bindValue( ":gender", $_POST['gender'], PDO::PARAM_STR );
$st->bindValue( ":maritalStatus", $_POST['maritalStatus'], PDO::PARAM_STR );
$st->bindValue( ":occupation", $_POST['occupation'], PDO::PARAM_STR );
$st->bindValue( ":stateOfOrigin", $_POST['stateOfOrigin'], PDO::PARAM_STR );
$st->bindValue( ":nationality", $_POST['nationality'], PDO::PARAM_STR );
$st->execute();
$conn = null;
}
catch (PDOException $e)
{
$error = 'There was an error while registering the Member, Please try again later.' . $e->getMessage();
include 'error.html.php';
exit();
}
if($st->rowCount() == 1)
{
$output = 'New Member successfully registered';
include 'output.html.php';
exit();
}
else
{
$error = 'Unable to perform your request now, Please try again later.';
include 'error.html.php';
exit();
}
}