Site URL
https://tamato.infinityfree.io
Using PHP to Process Forms!
This week was an introduction to using PHP practically on the site. A few weeks ago, one of our assignments was to make a form where the user can input their information. It was essentially a contact form. We used JavaScript to validate the input and make sure the user was entering data in all of the fields provided. I took that same form with the JavaScript code, and used it for this week’s assignment. We essentially took the form project and went one step further using $_GET and $_POST methods in PHP. I wanted to show an example of each on my site, so I made two separate buttons. One that executes the form processing using the $_GET method, the other with the $_POST method. It was a little challenging finding a way to make it work, but I got it! Below is my code for each method.
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["userName"])) { $name = $_POST["userName"]; $phone = $_POST["userPhone"]; $email = $_POST["userEmail"]; <p> Thank you echo $name; for submitting your response! <br><br> We have captured your message and will get back with you shortly at: <br><br> Phone Number: echo $phone; <br><br> or <br><br> Email Address: echo $email; </p> } else { echo "<p>No POST submission received yet.</p>"; }
This is my $_POST method. The beginning of the code is checks whether the form was submitted using the POST method and whether the userName field was provided. I used userName just because it is the first thing the user types in. I did this because in my FixedBox class, I wanted to display a message saying “No POST submission received yet”. Just to give the site a cleaner look. If the user submits the form with all their information, I am setting variables to store their data using $_POST. I am then printing to the screen a message template I made, then calling the variables to fill in the spots to personalize the message.
My $_GET method is below
if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET["userName"])) { <p> Thank you echo $_GET["userName"]; for submitting your response! <br><br> We have captured your message and will get back with you shortly at: <br><br> Phone Number: echo $_GET["userPhone"]; <br><br> or <br><br> Email Address: echo $_GET["userEmail"]; </p> } else { echo "<p>No GET submission received yet.</p>"; }
The $_GET method I used is essentially the same, except I am not storing the data in variables. I still have the if else statement to give the site a cleaner look, but I am calling their information inline with the PHP code. I think the variables give a cleaner look, and keep the code a bit more organized, but I wanted to try both options.
The question we had this week was, what is the difference between $_GET and $_POST.
$_GET takes the user’s information from the form, and stores it up in the URL. This could be handy for search queries, filters, anything where the user wants to potentially bookmark the URL for future use. But it is not good for anything related to sensitive data, such as passwords, credit card information, personal information, etc.
$_POST passes the information through the HTTP request body. Any time you want to refresh the page, the page may warn you that the information needs to be resubmitted. This is because refreshing the page could cause the browser to send the same data to the server again. $_POST is more secure and is better used to handle sensitive information since the information isn’t exposed in the URL. It does not encrypt information though. That is handled by HTTPS.
Looking forward to working with PHP and MySQL next week!
Leave a Reply