Menu Debugging PHP forms
 

Debugging PHP forms

PHP forms are problematic when it comes to getting them to work the way that you desire.

Over the years that I have been constructing web-based forms I have many observations and tips on what to expect and how to overcome some of the problems that you will encounter. Suffice it to say there is no short answer to getting forms to work correctly and it may be a case of finding the best third party solution to fit your requirements.

If you are like me then you will want to do this all yourself or at least get an insight into why other solutions that you may be trying to get to work are having problems.

Server logs are good ways to see potential problems in forms.

The logs are useful even if the form appears to be working correctly but there are potential problems that you may not be aware.

Background to this page

I was very active in the design and development of various forms for a Village Hall website that I was given the task of administering.

I have taken the online php feedback form offline. I am looking at other things and nobody has used it to date.

If I pickup the development of the forms for the Village Hall I may continue. However, the server log capabiity of the hosting package, TSO, is not as good as the HostingUK one. As such I am not sure that the same notices and warnings are thrown there.

For some reason I put the message construction ($Bindo) before the form???? I think that all I need is some re-ordering.

My plan was to echo the contents of the submitted form at the top of the page and "push" the "filled" form down on the screen. I think that I will have to use a manipulation of the DOM to add this text to a div at the top of the main content.

Top

Undefined indexes

This could be due to the form validation that I adopted

" mod_fcgid: stderr: PHP Notice: Undefined index: name in /var/www/vhosts/tempusfugit.me.uk/httpdocs/tf_feedback.php on line 171, referer: https://tempusfugit.me.uk/mailer_forms.html "

also email

The php validation page, which uses the same method does not generate these warnings

Timezone setting not set

" mod_fcgid: stderr: PHP Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in /var/www/vhosts/tempusfugit.me.uk/httpdocs/tf_feedback.php on line 198, referer: https://tempusfugit.me.uk/tf_feedback.php "

The php Validation that doesn't throw errors:

The code looks pretty much the same apart from the verbose message echoed back to the screen. However, this is where the "Notice" is thrown

The PHP:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $nameErr = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $nameErr = "Only letters and white space allowed"; 
    }
  }

The HTML form:

p> span class="error">* required field /span> /p>
 form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  <input type="submit" name="submit" value="Submit">  

  Name: input type="text" name="name" value=" ?php echo $name;?>">
  span class="error">*  ?php echo $nameErr;?> /span>

The validation function

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
Top

Links

Top

References:

  • Validate form email and URL - https:// www.w3schools.com/php/ php_form_url_email.asp
  • Using PHP_SELF in the action field of a form - http:// form.guide/php-form/ php-form-action-self.html
  • Apache mod_fcgid - https://httpd.apache.org/mod_fcgid/

Site design by Tempusfugit Web Design -