Solved

How to stop the refereshing of form fields

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

20 Experts available now in Live!

Get 1:1 Help Now