Remembering Form Data2:36 with Alena Holligan
When a form is not completed correctly, we ask the user to review the form and resubmit. It is much easier on the user when the form saves the data that was entered so the user doesn't have to start from scratch. In addition, when redisplaying user data, we want to make sure we escape output.
Let's set the value of these form fields equal to the user data. 0:00 Currently we are setting the variables for the form fields after the form post. 0:05 That means when we view the form before it's submitted, 0:10 those variables won't exist. 0:13 So we would need to check if those variables are set 0:15 before we could use them in the form value. 0:18 But there's another way. 0:21 Let's set each of these variables to an empty string at the beginning of our 0:22 script, then we know that the variable is set and we can use it even if it's empty. 0:26 We can add each of these on one line, putting them all together. 0:35 $project_id = $title which equals $date which 0:39 equals $time which finally equals our empty string. 0:44 Now let's use the variables to update the form field values. 0:50 As we explored in the previous course, 0:59 it's a good idea to escape output whenever you output user data. 1:01 Be sure to check the notes associated with this video for 1:05 a more detailed explanation. 1:08 We'll use HTML special chars for this. 1:10 So our PHP will echo htmlspecialchars, And our title. 1:15 Then close our php. 1:26 Let's copy that code for $date and $time as well. 1:27 We have to do something a little different for our select. 1:39 After I select value, let's break this up. 1:42 Then we can add a conditional, 1:53 if ($project_id == $item 1:57 Project_id Then 2:02 we're going to echo selected. 2:09 Now let's refresh the browser, resubmitting the form data. 2:16 This time the fields we already filled out are still complete. 2:20 So I can easily choose a project, And submit the form again. 2:24 This time we're redirected to the task list where we see our new task. 2:31
You need to sign up for Treehouse in order to download course files.Sign up