?
Solved

Saving data in a form after it's been submitted

Posted on 2010-01-11
3
Medium Priority
?
153 Views
Last Modified: 2012-05-08
I built a simple form in HTML and I am using PHP to collect the data and then write it to a text file.  After clicking the submit button, all of the fields are cleared.  Is there a way to change this so that after the submit button is pressed, the fields still remain populated with their data?
0
Comment
Question by:TacomaVA
[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
3 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26287236
OK, assuming that the form resides in a .php script you do this

<?php

if script submitted form then
    capture fields
    writ to file
endif


HTML form

?>

So the trick is that the form's action is the page it resides in. So we would have the following

<?php

// Check for SUBMIT button being pressed
//
if ( isset( $_POST['submit'] ) ) {

     // Collect variables from form. These are the "name" attributes
     // in the HTML input fields
     //
     $value1 = $_POST['field1'];
     $value2 = $_POST['field2'];

     // Write to file
     //
     $fp = fopen("myfile.txt", "w");
     fputs( $fp, "value 1 is $value1 and value2 is $value2");
     fclose( $fp );

}

?>
<html>
    <head>
         .... etc

    <form action='<?php echo $_SERVER['PHP_SELF']; ?>' method='post'>

         .. other stuff
         <input name='field1' type='text' value='' />
         <input name='field2' type='text' value='' />

         <input name='submit' type='submit' value='Process my form' />

    </form>

         .... etc
</html>


There is a bit more to it than that but that is the basics. Hopefuuly you can adapt it easily enough.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 1000 total points
ID: 26287268
hello TacomaVA,

Check this :

<html><head></head><body> 
<?PHP
  echo "<form id="myform" method='POST' action='" . $_SERVER["PHP_SELF"] . "'>";
  $t = isset($_POST[atext])?:$_POST[atext]:"";
  echo "<INPUT type='text' value='" + $t + "' id='atext' name='atext' />";
?>
</form></body></html>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 26288327
Thanks for the points! happy new year!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

771 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