Manipulating Data Submitted via a Form (Part II)
Checking if the form has been submitted
Before we try to do anything as far as manipulating data, we first need to check to see if the form has actually been submitted. If we don't do this, our script (if it were completed without this test) would write blank lines to our text file every time someone visited the page. That wouldn't be good. Trust me.
We can do this using an if statement to test a condition, and if the condition is met, we then proceed with processing the data submitted in the form. But what can we test to see if the form has been submitted?
Notice that the submit button in the form has a value.
- <input type="submit" name="submit" value="send" />
If this button is pressed, the $_POST['submit'] variable will hold the value "send". If the form has not been submitted, the variable will not be set.
Thus, in this case, we can use the function isset(). If we pass it $_POST['submit'], it will return true if the variable is set (the form has been submitted) or it will return false if $_POST['submit'] has not been set (the form has not been submitted).
Add the following PHP block to the top of your PHP file:
- <?php
if (isset($_POST['submit'])) {
}
?>
Now that we've checked $_POST['submit'], we know the form has been submitted, and we can write the code to output the comment to a text file.
Writing the data to a file
The process for writing to a file isn't particularly complicated. We will follow these simple steps:
- Open the file
- Write to the file
- Close the file
1. Open the file:
To open the file, we will use the function fopen(). To use this function, we will pass it two strings. The first is the name of the file that we wish to open, and the second is the mode with which we want to open it. There are several different fopen modes, but the one we will be using in this example is the 'append mode'. In this mode, fopen will write any new data to the end of the file, not overwriting what already exists in the file. To use fopen in append mode, the second argument we will pass to it is "a" (including the quotation marks because the second argument is a string just like the first).
The fopen function will return a reference to the text file. We will need this reference if we plan on writing to the file, so make sure to assign the result of this function to a variable (such as fp).
- $fp = fopen("comments.txt", "a");
2. Write to the file:
Now that the file we wish to write to is open, and we have a reference to it, we can tell our script to write to it. In this case, we will use the fwrite() function.
We pass this function two arguments, the first being the reference to our file ($fp), which tells fwrite which file to write to. The second argument that we pass to the function is the data we wish to write to the file.
The end result is something like the following:
- fwrite($fp, $_POST['from'] . "\t" . $_POST['email'] . "\t" . $_POST['subject'] . "\t" . $_POST['message'] . "\n");
3. Close the file
When we are done with the file we are working with, it is important to close that file. If we don't, we could potentially receive strange errors when attempting to access it. PHP has a function specifically for this purpose called fclose(). All we need to do is pass this function our file pointer. This tells the function which file to close.
- fclose($fp);
The final result will look something like this:
- <?php
- //PHP Comment form for Techranger PHP & MySQL Techtime
- //Check to see if the form has been submitted
- if (isset($_POST['submit'])) {
- //open, write to, and close the file
- $fp = fopen("comments.txt", "a");
- $written = fwrite($fp, $_POST['from'] . "\t" . $_POST['email'] . "\t" . $_POST['subject'] . "\t" . $_POST['message'] . "\n");
- fclose($fp);
- //open, write to, and close the file
- }
- ?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>Comment Form</title>
- </head>
- <body>
- <h1>Comment Form</h1>
- <form action="comment_form.php" method="post">
- <p>
- <label for="from">From:</label><br />
- <input type="text" name="from" id="from" /><br /><br />
- <label for="email">E-mail Address:</label><br />
- <input type="text" name="email" id="email" /><br /><br />
- <label for="subject">Subject:</label><br />
- <input type="text" name="subject" id="subject" /><br /><br />
- <label for="message">Message:</label><br />
- <textarea name="message" id="message" rows="6" cols="75"></textarea><br />
- <input type="submit" name="submit" value="send" />
- </p>
- </form>
- </body>
- </html>
Provide the user with an error message if something went wrong
What is going to happen if our form can't write to our comments.txt file? Well, the hard, painful truth is that in most cases, nothing will happen. The user will think they submitted their comment successfully, and go about their happy lives never knowing that the important information they submitted never reached you.
We can amend this problem by making sure that the fwrite function actually wrote something to the file. To do this, we need to assign the fwrite statement that we wrote earlier to a variable. Fwrite() will return false if it did not write anything to the file, or if it did write to the file, it will return size of the data written. This is perfect for an if..else statement.
Change the line with fwrite to the following:
- $written = fwrite($fp, $_POST['from'] . "\t" . $_POST['email'] . "\t" . $_POST['subject'] . "\t" . $_POST['message'] . "\n");
Then add this below the block of code that writes to the file (make sure that this is still inside the if statement that we wrote originally because we do not want it to execute if our form was not submitted):
- if ($written == FALSE)
- {
- $message = "<p>There was an error recording your comment. If the problem persists, please contact the site administrator.</p>";
- else
- {
- $message = "<p>Comment recorded.</p>";
So, now that we have an error message stored in a variable, we can output it inside our HTML.
Add the following line below our h1 header:
- <?php echo("$message"); ?>
The completed form should look something like this:
- <?php
- //PHP Comment form for Techranger PHP & MySQL Techtime
- //Check to see if the form has been submitted
- if (isset($_POST['submit'])) {
- //open, write to, and close the file
- $fp = fopen("comments.txt", "a");
- $written = fwrite($fp, $_POST['from'] . "\t" . $_POST['email'] . "\t" . $_POST['subject'] . "\t" . $_POST['message'] . "\n");
- fclose($fp);
- //If a line is not written, give error message else give success message
- if ($written == FALSE) {
- $message = "<p>There was an error recording your comment. If the problem persists, please contact the site administrator.</p>";
- //open, write to, and close the file
- } else {
- $message = "<p>Comment recorded.</p>";
- }
- "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
- <title>Comment Form</title>
- <p>
- <label for="from">From:</label><br />
- <input type="text" name="from" id="from" /><br /><br />
- <label for="email">E-mail Address:</label><br />
- <input type="text" name="email" id="email" /><br /><br />
- <label for="subject">Subject:</label><br />
- <input type="text" name="subject" id="subject" /><br /><br />
- <label for="message">Message:</label><br />
- <textarea name="message" id="message" rows="6" cols="75"></textarea><br />
- <input type="submit" name="submit" value="send" />
- </p>