1

I have a form to submit my contact info. and i'm fetching form fields using php like:

if(isset($_post['submit']))
{
//submit cantact info
}
else
{
//bad user
}

my problem is: if the user is failed to submit form the "form values should not clear"....

but form values are clearing on click submit button..! so anyone know how to prevent clear form values?

ShivarajRH
  • 902
  • 12
  • 24
  • `value = $_post['fieldname']` – Rikesh Sep 20 '11 at 11:24
  • check this - > http://stackoverflow.com/questions/5198304/how-to-keep-form-values-after-post – Rikesh Sep 20 '11 at 11:26
  • 1
    Clarification: Did you mean: User submits the form and you validate the input date. If a validation error occurs, you present an error message and the form again to the user and then the fields should contain the entered values? – Alexander Sulfrian Sep 20 '11 at 11:28

3 Answers3

1

you have to populate these fields manually.

the general idea stands for using POST/Redirect/GET method

after receiving POST data you have to check it and raise error flag
and in case of some errors you have to show the same form back, with filled inputs and corresponding error messages.

here is a rough example:

<?  
$err = array();
if ($_SERVER['REQUEST_METHOD']=='POST') {  

  //performing all validations and raising corresponding errors
  if (empty($_POST['name']) $err[] = "Username field is required";  
  if (empty($_POST['text']) $err[] = "Comments field is required";  

  if (!$err) {  
    // if no errors - saving data 
    // and then redirect:
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
  }  else {
    // all field values should be escaped according to HTML standard
    foreach ($_POST as $key => $val) {
      $form[$key] = htmlspecialchars($val);
    }
} else {
  $form['name'] = $form['comments'] = '';  
}
include 'form.tpl.php';
?>  

and then in the form.tpl.php template make it like this:

<? if ($err): ?>
  <? foreach($err as $e): ?>
<div class="err"><?=$e?></div>
  <? endforeach ?>
<? endif ?>
<form>
  <input type="text" name="name" value="<?=$form['name']?>">
  <textarea name="comments"><?=$form['comments']?></textarea>
  <input type="submit">
</form>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

You have to (manually) put the submitted values in the form elements. Example:

<input type="text" name="username" value="<?=( isset( $_POST['username'] ) ? $_POST['username'] : '' )?>" />
Rijk
  • 11,032
  • 3
  • 30
  • 45
0

You need to keep track of the form values. One suggestion is to setup an array of default values that is used when presenting the form markup.

On POST, you then merge the post data. For example

$formData = array(
    'foo' => '',
    'bar' => 'default value',
    'baz' => ''
);

if ('POST' == $_SERVER['REQUEST_METHOD') {
    $formData = array_merge($formData, $_POST);

    // do validation, handle success
}

Then, in HTML

<input name="foo" value="<?php echo htmlspecialchars($formData['foo']) ?>">
<!-- etc -->
Phil
  • 157,677
  • 23
  • 242
  • 245
  • ya u r right. i just storing post values and displaying in field. i'm not using your array_merge method. and also not using htmlspecialchars() methods... – ShivarajRH Sep 20 '11 at 11:33