Solved

How to stop the refereshing of form fields

Posted on 2002-06-22
4
183 Views
Last Modified: 2006-11-17
Hello,

I have a web form with quite a few fields (name, address etc). It's posts the data to a mysql database. When the form is filled in and there is a duplicate name, I echo a message to the user that the username already exists. However, the problem I have is with the other fields. All the info they entered (about 20 fields worth) is deleted and they have to retype them.  I want the user not to have to re-fill out all the fields. How can I achieve this?

Thanks in advance.

Rob.
0
Comment
Question by:rround
  • 2
4 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 7100887
Maybe something like this?

<form action="<? echo $PHP_SELF; ?>" method="post">
<table border="0">
<tr><td><p>Firstname:</td><td><input type="text" size="20"  name="firstname" value="<? echo $firstname; ?>"></td></tr><tr><td><p>Lastname:</td><td><input type="text" size="20"  name="lastname" value="<? echo $lastname; ?>"></td></tr>
<tr><td><p>Address:</td><td><input type="text" size="30"  name="address" value="<? echo $address; ?>"></td></tr>


.. and so on
</table>
</form>

Doing this, the form would be posted to the same file, and the previous values is pretyped into the fields(eg: value="<? echo $firstname; ?>"> ).

Batalf
0
 
LVL 5

Accepted Solution

by:
Hamlet081299 earned 100 total points
ID: 7102324
Here's an example which covers this quite well.

It retains data within the entry form, and also allows for the possibility of default values (for example the date).  If the date has been set by the user, it is used, otherwise the default is used.

It also shows how to provide some confirmation.

It does use 3 sources to achieve the result, which I prefer, for maintenance reasons.  If you like to be clever and cram everything into 1 source that shouldn't be too hard.

The entry point into this is "main.php"...

MAIN.PHP

<?php
if ($REQUEST_METHOD == 'POST') {
  // Validate
  $errors = '';

  if (!$message or ($message == '')) {
    $errors .= '* No message entered.<br>';
  }

  if ($errors) {
    include('myentry.php');
  } else {
    include('myconfirm.php');
  }
} else {
  include('myentry.php');
}
?>


MYENTRY.PHP

<html>
<body bgcolor="FFFFCC">
<h1>User Entry Form</h1>

<?php
  if ($errors != '') {
    echo "<p>Please correct the following errors ...<br>$errors</p>\n";
  }
  $date = isset($date) ? $date : strtoupper(date('d M Y'));
?>

<form action="main.php" method="POST">
  <table cellpadding="4" cellspacing="0" border="0">
    <tr>
      <th>Date:</th>
      <td><input type='text' name='date' value='<?=$date?>' size='60'></td>
    </tr>
    <tr>
      <th>Name:</th>
      <td><input type="text" name="name" value='<?=$name?>' size="60"></td>
    </tr>
    <tr>
      <th colspan="2">Message:<br>
      <textarea name="message" rows="10" cols="60"><?=$message?></textarea>
      </th>
    </tr>
    <tr>
      <td colspan="2"><p align="right"><input type="submit" value="SEND"></p></td>
    </tr>
  </table>
</form>
</body>
</html>


MYCONFIRM.PHP

<?php
    // Do the action here!!!
?>

<html>
<body>
<p>Message sent to: "<?=$name?>"</p>
<blockquote>
<pre><?=$message?></pre>
</blockquote>
</body>
0
 

Author Comment

by:rround
ID: 7102565
Thanks Hamlet,

That was what I was looking for. Thanks for your input too Batalf.

Rob.
0
 

Author Comment

by:rround
ID: 7102567
Thanks Hamlet,

That was what I was looking for. Thanks for your input too Batalf.

Rob.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question