Solved

How to stop the refereshing of form fields

Posted on 2002-06-22
4
173 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
spacing 5 30
Use PHP to save or display image returned by JSON call. 20 49
Not needed 13 57
using hash in login 34 13
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now