Solved

How to stop the refereshing of form fields

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

778 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