By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,261 Members | 1,674 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,261 IT Pros & Developers. It's quick & easy.

Rebuild form with origional data

P: 3
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:

Expand|Select|Wrap|Line Numbers
  1. <form id="ambercat" name="ambercatlog" method="post" action="ambercat.php">
  2.   <table>
  3.   <tr> <td><b>Subject Title</b></td>
  4.  <td><input size="17" name="query_title" type="text" class="textfield" id="query_title"></td></tr>
  5.  
  6.  <tr><td><b>Description</b></td></tr>
  7.  <tr><td> <textarea cols="34" rows="6" name="Comments" id="description" onBlur="trim('Comments')" onfocus="doClear(this)"></textarea> </td>
  8.   </tr>
  9.  
  10.   <tr><br/><td><input type="submit" name="Submit_query" value="Submit" /></td><br/><br/></tr>
  11.  
  12. <tr><td height="110"> </td> <td align="justify">
  13.  //error messages displayed here  
  14.  <?php
  15.     if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
  16.        // echo '<ul class="err">';
  17.         foreach($_SESSION['ERRMSG_ARR'] as $msg) {
  18.             echo '<li>',$msg,'</li>';
  19.         }
  20.        // echo '</ul>';
  21.         unset($_SESSION['ERRMSG_ARR']);
  22.     }
  23. ?></td></tr>
  24.  
  25.  </table>
  26.  
  27. </form>
And my php script:

Expand|Select|Wrap|Line Numbers
  1. <?
  2.  
  3.     session_start();
  4.     //Array to store validation errors
  5.     $errmsg_arr = array();
  6.  
  7.     //Validation error flag
  8.     $errflag = false;
  9.  
  10.     $title = $_POST['query_title'];
  11.     $description = $_POST['Comments'];
  12.  
  13.  //Input Validations
  14.     if($title == '') {
  15.         $errmsg_arr[] = 'Title missing';
  16.         $errflag = true;
  17.     }
  18.     if($description == '') {
  19.         $errmsg_arr[] = 'Description missing';
  20.         $errflag = true;
  21.     }
  22.  
  23.     //If there are input validations, redirect back to the form
  24.     if($errflag) {
  25.         $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  26.         session_write_close();
  27.         header("location: ambercat_log.php");
  28.         exit();
  29.     }
  30.  //send to database   
  31.  include('connect.php');
  32. @mssql_query("INSERT INTO Customer_Calls (Title, Description)
  33. VALUES ('$title', '$description')");
  34. mssql_close();
  35. $result5 = @mssql_query;
  36.  
  37.     //Check whether the query was successful or not
  38.     if($result5) {
  39.         header("location: ambercat_submit.php");
  40.        }
  41. ?>
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.
Mar 9 '10 #1
Share this Question
Share on Google+
1 Reply


guillermobytes
P: 77
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():
Expand|Select|Wrap|Line Numbers
  1. header("Location : ambercat_log.php?query_title=$title&query_description=$description");
and then handle them from the $_GET global in ambercat_log.php
then put some conditionals in the "value" attribute of the html form like the:
Expand|Select|Wrap|Line Numbers
  1. <input value="<?php (isset[$_GET['query_title']])?$_GET['query_title']:''?>" size="17" name="query_title" type="text" class="textfield" id="query_title"></td></tr>
  2.  
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('Location : asdfasdfsa.php') and use functions with require_once instead.
Mar 9 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.