Solved

How to stop the refereshing of form fields

Posted on 2002-06-22
4
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

624 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