SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form retaining data when data doesn't get inserted into the database

    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:
    Code:
    <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">&nbsp;</div>
            <div class="large-3 columns">&nbsp;</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">&nbsp;</div>
           </div>
         </form>

    The index.php

    PHP Code:
    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();

           }    
       
      } 

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi programmers in the house, I need your help with the problem I posted up there. Thanks in advance.

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    So this will actually require a bit of a change in your app. You can't control what's in the page after a 'back' button is pressed. You can however:

    1) Store the data that is being sent in a session.
    2) Either have the parsing script on the same php script or, on the parsing script, upon error, use header() to send them back to the original page with an error message in the session().
    3) The php script that has your form, will do a check to see if the session data has been populated, and if it has, set the value of each box with the values from the session.

    Code PHP:
    session_start();
     
    if(isset($_SESSION['form'])) {
        //display a friendly error message
        //display form with previous data ie <input value="$_SESSION['form']['foo']"/>
    } else {
        //display normal form
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Location
    Port Harcourt
    Posts
    58
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks K. Wolfe, your reply was really helpful. I've gotten the solution to the problem. I was trying to implement your suggestion before replying. Thanks a lot man.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •