How to Test Submissions from Forms that Use the POST Method

An important part of developing online forms is testing. One area of testing I've always wanted to better with is making sure input types like radio buttons can't accept values which are not allowed. In this week's post, we go over my primary method of testing. That way we can discuss a faster way next time.

Simple Form

Let's say you have a page named "myform.php" that contains the following form:

<form method="post" action="myform.php">
     <p>Were there any transmission problems?</p>
     <div><label><input type="radio" name="transmissionProblems" value="Yes"> Yes</label></div>
     <div><label><input type="radio" name="transmissionProblems" value="No"> No</label></div>
     <div><label><input type="radio" name="transmissionProblems" value="N/A"> N/A</label></div>
     <div><input type="submit" name="submit" value="Submit"></div>
</form>

Then on the same page you have a simple validation script. The script makes sure the transmissionProblems radio button can only be set to "Yes", "No", or "N/A".

<?php
//IF THE FORM WAS SUBMITTED
if($_SERVER['REQUEST_METHOD'] == 'POST') {
     //DECLARE VALID RESPONSES
     $validResponses = array('Yes', 'No', 'N/A');
 
     //IF INVALID RESPONSE
     if(!in_array($_POST['transmissionProblems'], $validResponses)) {
          print 'Unknown response to "Were there any transmission problems?"';
 
     //ELSE...VALID RESPONSE
     } else {
          print 'Thanks for responding';
     }
}
?>

<form method="post" action="myform.php">
     <p>Were there any transmission problems?</p>
     <div><label><input type="radio" name="transmissionProblems" value="Yes"> Yes</label></div>
     <div><label><input type="radio" name="transmissionProblems" value="No"> No</label></div>
     <div><label><input type="radio" name="transmissionProblems" value="N/A"> N/A</label></div>
     <div><input type="submit" name="submit" value="Submit"></div>
</form>

Now it's easy to make sure the valid responses work, but how do you test the invalid responses. Since the form uses the POST method, the data will be passed behind the scenes. There doesn't seem to be a quick way to inject bad values like you can with the GET method.

Solution

One method for testing is to hard code the values to test. For example, you could overwrite the POST variable by adding the following line:

<?php
//IF THE FORM WAS SUBMITTED
if($_SERVER['REQUEST_METHOD'] == 'POST') {
     //DECLARE VALID RESPONSES
     $validResponses = array('Yes', 'No', 'N/A');
 
 
     $_POST['transmissionProblems'] = 'BAD VALUE';
 
 
     //IF INVALID RESPONSE
     if(!in_array($_POST['transmissionProblems'], $validResponses)) {
          //...
?>

Of course, it's a bit cumbersome to change the hard-coded value every time you want to test a different value. To speed things along, you could utilize the advice posted in "Setting up a Makeshift Test Environment for Experimenting with PHP Code".

Related Posts

4 Comments

  • #4 Patrick Nichols on 05.13.15 at 3:43 pm

    @Marc – I developed a simple script in the past using JavaScript to test a form that had around 200 input fields. The only goal of the script was to populate the form fields with a unique value that I could verify once submitted. That way I could make sure all the fields were named properly and the values came through as expected.

    However, I haven't put much thought into coding something which automates the full testing process. Having a solution that could submit a form multiple times with different types of bad values would be very beneficial.

    Thanks again for your feedback!

  • #3 Marc Towler on 05.11.15 at 6:12 pm

    Either would work well, personally I prefer doing it as PHP. Depending on how you set up your code there are different ways to do this.
    One example, if you had one class that "assisted" you with creating forms then utilising something as simple as unit testing would be an awesome solution.
    If you were doing it in a similar manner to the above then hard-coding the valid responses is a route that I would go down, utilising JS for a bit of real time feedback for the user is a very good route though

  • #2 Patrick Nichols on 04.08.15 at 6:00 pm

    @Marc – Thanks for the suggestion! What would you use to write the script (PHP, JavaScript, etc.)?

  • #1 marc towler on 04.06.15 at 4:52 pm

    hey,

    i know that you present a solution here, but what about just pushing a simple script that would make several attempts with bad data?

Leave a Comment