Setting Up A Feedback Form In PHP Part 2
In the first example it was all about setting up the form and the php properly, and now our next example is going be all about display errors. Meaning that we will purpose do something to the HTML or the php that will display an error if something is wrong within the PHP script.
Ok so we have our basic feedback form which is simple enough:
<div> <form action="handle_form2.php" method="post"> <p>Name: <select name="title"> <option value="Mr.">Mr.</option> <option value="Mrs.">Mrs.</option> <option value="Ms.">Ms.</option> </select> <input type="text" name="name" size="20" /></p> <p>Email Address: <input type="text" name="email" size="20" /></p> <p>Response: This is... <input type="radio" name="response" value="excellent" /> excellent <input type="radio" name="response" value="okay" /> okay <input type="radio" name="response" value="boring" /> boring</p> <p>Comments: <textarea name="comments" rows="3" cols="30"></textarea></p> <input type="submit" name="submit" value="Send My Feedback" /> </form> </div>
ini_set ('display_errors', 1); // Let me learn from my mistakes!
// This page receives the data from feedback.html.
// It will receive: title, name, email, response, comments, and submit in $_POST.
$title = $_POST['title'];
$name = $_POST['firstname'];
$response = $_POST['response'];
$comments = $_POST['comments'];
// Print the received data:
print "<p>Thank you, $title $name, for your comments.</p>
<p>You stated that you found this example to be '$response' and added:<br />$comments</p>";
Ok, if someone was to fill out the form and hit the submit button they should get the following error: Notice: Undefined index: firstname in /class/CIS-2430-VO01-V09SP/mra10150/week3/handle_form2.php on line 16
So what does this error mean? It would seem that field called firstname has been undefined and it is located on line 16 in the php file handle_form2.php. The object of this is to figure out what is wrong with this and then correct. The first thing is to look at the PHP file and then compare the PHP file with the HTML form and see what is out of place.
Hopefully you will notice that firstname is not even in the HTML file and in fact it was just a small typo on your part and it should be just name instead of firstname in the PHP file. Now once you fix the error the php will run properly once you test it out.
ini_set ('display_errors', 1); // Let me learn from my mistakes!
// This page receives the data from feedback.html.
// It will receive: title, name, email, response, comments, and submit in $_POST.
$title = $_POST['title'];
$name = $_POST['fname'];
$response = $_POST['response'];
$comments = $_POST['comments'];
// Print the received data:
print "<p>Thank you, $title $name, for your comments.</p>
<p>You stated that you found this example to be '$response' and added:<br />$comments</p>";
The above php script is the correct example and so when you try out both php scripts with the form above one of them should work and the other shouldn’t. Though this is a pretty basic in terms of setting up errors you can do a lot more with error and my suggestion would be to search out other examples of setting up error warnings in PHP.













Leave your response!
You must be logged in to post a comment.