SAVING FORM DATA TO A TEXT FILE

  1. Create a folder in your My PHP Sites folder called Form Data.
  1. Create a new Dreamweaver site pointing to this folder. You will need to add Remote Info and Testing Server information, and create a new folder on the server Form_Data to which you will publish.
  1. You will first need to create an HTML page containing the form. Open a new HTML file and enter the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<html xmlns="

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Enter Details</title>

</head>

<body>

<h3>Enter your details!</h3>

<form action="writeformdata.php" method="post">

<label for="first_name">First Name</label<br />

<input name="first_name" type="text" size="20" id="first_name" /<br />

<label for="last_name">Last Name</label<br />

<input name="last_name" type="text" size="20" id="last_name" /<br />

<label for="email">Email</label<br />

<input name= "email" type="text" size="20" id="email" /<br />

<label for="message">Enter your comments or questions.</label<br />

<textarea name="message" rows="3" cols="20" id="message"</textarea<br />

<input type="submit" name="submitted" value="Go" />

</form>

</body>

</html>

  1. Save the file with the nameenterdetails.html
  1. Publish your page and view the published page in your browser.

  1. Open a new PHP file, and enter the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<?php

if (@$_POST['submitted']) {

$first_name = @$_POST['first_name'];

$last_name = @$_POST['last_name'];

$email = @$_POST['email'];

$msg = @$_POST['message'];

}

$filename = "C:/inetpub/wwwroot/xx/Form_Data/form_data_file.txt";

$form_data = $first_name . "\t" . $last_name . "\t" . $email . "\t" . $msg . "\n";

$newfile = @fopen($filename, "a+") or die("Couldn't open file.");

@fwrite($newfile, $form_data) or die("Couldn't write to file.");

$message = "<p>Your data is being processed...... </p>";

$details = "<p>Your details are as follows:<br />" . $first_name . "<br />" . $last_name . "<br />" . $email . "<br />" . $msg . "<br />";

fclose($newfile);

?>

<html xmlns="

<head>

<title>Adding Form Data to a File</title>

</head>

<body>

<?php echo "$message"; ?>

<?php echo "$details"; ?>

</body>

</html>

  1. Save the file with the name writeformdata.php.
  1. Publish both your pages. View the enterdetails.html page in your browser, and submit your details.

Check the contents of the file form_data_file.txton the server.

What does @fopen argument "a+"mean?

Enter more form data. Has the original form data been overwritten or has the data been appended to the text file?

Adjust the code so that the labels FirstName, LastName, Email and Message are saved to the text file. Your page should display as shown below:

Adjust the code to save the current date to the text file and display the date on the page.

The suggested solution:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

<?php

if (@$_POST['submitted']) {

$first_name = @$_POST['first_name'];

$last_name = @$_POST['last_name'];

$email = @$_POST['email'];

$msg = @$_POST['message'];

}

$filename = "C:/inetpub/wwwroot/xx/Form_Data/form_data_file.txt";

$current_date = date("H:i A dS F Y");

$form_data = $current_date . "\t" . $first_name . "\t" . $last_name . "\t" . $email . "\t" . $msg . "\n";

$newfile = @fopen($filename, "a+") or die("Couldn't open file.");

@fwrite($newfile, $form_data) or die("Couldn't write to file.");

$message = "<p>Your data is being processed...... </p>";

$details = "<p>Your details are as follows:<br />Current Date: " . $current_date . "<br />First Name = " . $first_name . "<br />Last Name = " . $last_name . "<br />Email = " . $email . "<br />Message = " . $msg . "<br />";

fclose($newfile);

?>

<html xmlns="

<head>

<title>Adding Form Data to a File</title>

</head>

<body>

<?php echo "$message"; ?>

<?php echo "$details"; ?>

</body>

</html>

A - Introduction to PHP and MySQL - Saving Form Data to a Text File.docVersion 2

Page 1 of 6