Rebuild form with origional data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JamieC
    New Member
    • Mar 2010
    • 3

    Rebuild form with origional data

    Hi, I have a form for customers to enter requests, this form posts to a php script which checks if all fields have been populated and, if so, sends the data to a database. If not all fields have been populated the user gets returned to the form and error messages appear to tell them what was missing.

    My problem is that when the form is displayed with the error messages, all the data the user had entered is gone so they have to fill out the whole form again.

    How do I make the form rebuild with the data entered?

    here is my form:

    Code:
    <form id="ambercat" name="ambercatlog" method="post" action="ambercat.php">
      <table>
      <tr> <td><b>Subject Title</b></td>
     <td><input size="17" name="query_title" type="text" class="textfield" id="query_title"></td></tr>
    
     <tr><td><b>Description</b></td></tr>
     <tr><td> <textarea cols="34" rows="6" name="Comments" id="description" onBlur="trim('Comments')" onfocus="doClear(this)"></textarea> </td>
      </tr>
    
      <tr><br/><td><input type="submit" name="Submit_query" value="Submit" /></td><br/><br/></tr>
     
    <tr><td height="110"> </td> <td align="justify">
     //error messages displayed here  
     <?php
        if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
           // echo '<ul class="err">';
            foreach($_SESSION['ERRMSG_ARR'] as $msg) {
                echo '<li>',$msg,'</li>';
            }
           // echo '</ul>';
            unset($_SESSION['ERRMSG_ARR']);
        }
    ?></td></tr>
    
     </table>
    
    </form>
    And my php script:

    Code:
    <?
    
        session_start();
        //Array to store validation errors
        $errmsg_arr = array();
    
        //Validation error flag
        $errflag = false;
    
        $title = $_POST['query_title'];
        $description = $_POST['Comments'];
        
     //Input Validations
        if($title == '') {
            $errmsg_arr[] = 'Title missing';
            $errflag = true;
        }
        if($description == '') {
            $errmsg_arr[] = 'Description missing';
            $errflag = true;
        }
            
        //If there are input validations, redirect back to the form
        if($errflag) {
            $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
            session_write_close();
            header("location: ambercat_log.php");
            exit();
        }
     //send to database   
     include('connect.php');
    @mssql_query("INSERT INTO Customer_Calls (Title, Description)
    VALUES ('$title', '$description')");
    mssql_close();
    $result5 = @mssql_query;
    
        //Check whether the query was successful or not
        if($result5) {
            header("location: ambercat_submit.php");
           }
    ?>
    There is more fields in the form but i have left these out here to shorten the code a bit, any help on sending the data back to the title and description fields would be very helpful,

    thanks in advance.
    Last edited by Dormilich; Mar 9 '10, 02:40 PM. Reason: Please use [code] tags when posting code
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    #2
    I'm not sure about this, so don't be surprised if it doesn't work. I would try to add some parameters to header():
    Code:
    header("Location : ambercat_log.php[B]?query_title=$title&query_description=$description[/B]");
    and then handle them from the $_GET global in ambercat_log.ph p
    then put some conditionals in the "value" attribute of the html form like the:
    Code:
    <input [B]value="<?php (isset[$_GET['query_title']])?$_GET['query_title']:''?>"[/B] size="17" name="query_title" type="text" class="textfield" id="query_title"></td></tr>
    this is a hack (if it works...) but it is not the good way to do things.
    you could try to do your logic without header('Locatio n : asdfasdfsa.php' ) and use functions with require_once instead.

    Comment

    Working...