0

I am trying to track the fields that are left blank on my website form when it is submitted.

The form I am using is used by my sales agents, and since I do not immediately see the submitted info before the sales agent has a chance to modify the submitted information, I am looking to see if they are filling out all of the info on the site instead of adding in more info later in the CRM. This will help me to optimize the form for their use. Currently the form is written using HTML, PHP, jQuery and AJAX.

I have not tried anything yet, as I do not even know where to begin with this. I have not seen it done before.

Let me know if you need to see the markup or if this question needs more clarification. Feel free to point me in the direction of a tutorial if that is easiest.

Thanks for the help!

Allen Levings
  • 142
  • 11
  • PLEASE! search before you ask: jQuery: http://stackoverflow.com/questions/16556968/jquery-form-submit-to-check-empty-fields PHP: http://stackoverflow.com/questions/3190464/php-check-if-any-posted-vars-are-empty-form-all-fields-required – Axel Aug 19 '14 at 23:28
  • Thanks Axel, but these don't work for me because I do not want to prevent the page from being submitted if a required field is empty. I want the form to submit and post, but return a report for any value that was empty. Unless I read those wrong, both of those answers prevent the form from being submitted if the required fields are empty. Correct me if I am wrong. – Allen Levings Aug 20 '14 at 16:41
  • You could serialize the form and send an ajax-request to a server-interface, where the entered data is logged. With that you will be able to see, which fields were left blank. – Tobias Golbs Aug 26 '14 at 17:26

3 Answers3

1

You can check in both PHP or JS. If you want to do this server side so you can save this information simply check the POST results of the form.

if (trim($_POST['myField'])=="") {
     //not filled out. if you have default values, check for it in the if above too.
     mail('your@email.com', 'Subject', 'Your body.');
}
Ali Hamze
  • 1,590
  • 13
  • 26
  • What would happen to this snippet if I was to add a white space in a box I do not wish to enter data in? – Daryl Gill Aug 19 '14 at 23:25
  • 1
    You can use [trim()](http://php.net/manual/en/function.trim.php) to get rid of whitespace at the beginning and end of strings :) – Ali Hamze Aug 19 '14 at 23:33
  • Forgive me, but I am a bit of a noob. How would I implement this, and then have the results returned to me? If I could get the results in a separate email, that would be great! – Allen Levings Aug 20 '14 at 16:48
  • @AllenLevings You can use the PHP [mail()](http://php.net/manual/en/function.mail.php) function to send it as an email. You can also save it to say a [MySQL](http://php.net/manual/en/book.mysqli.php) database if you want to keep this information on the server. – Ali Hamze Aug 20 '14 at 18:20
1

I think that you want to know the way how to check and validate the form information before submit. And as you said that you use jQuery, so I advise that there are 2 solutions to solve the problem at least.

1, write the validate script by yourself.

you can use the following script to check the form data before submit.

jQuery(form).submit(function(){
    // Here you can validate your form data
    // and if the data are incorrect, 
    // you can return false, and the form submission will be cancelled     
});

2, use the jQuery Validation plugin.

The plugin can be got from http://jqueryvalidation.org/, and you can import it with the following script.

<script type="text/script" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/jquery.validate.js"></script>

And then, you only need to add some special attribute in your form.

For example, if you add "required" of your input field, which means the field must be filled with the characters.

<form>
  <input type="text" name="username" required />
</form> 

And then, write the following script to notify the plugin to validate the form before submission.

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("form").validate();
    });
</script>
Zhu Zhaohua
  • 164
  • 3
  • Thanks Zhu, but it looks like this would prevent the form from being submitted if fields are left blank. I want the form to submit and all of the info to post, but just let me know what fields were left blank. Thanks for the answer though! – Allen Levings Aug 20 '14 at 16:46
  • It is easy too if you want to know which fields are blank. the post data variable $_POST is an array in fact, so you can traverse it with the following code : $value){if($value == ""){// Here You can know which key is blank, and deal with it}} ?> – Zhu Zhaohua Aug 20 '14 at 22:33
1

That is what PHP empty() is for:

if (empty(trim($_POST['some_field']))
{
    // Nothing was supplied. 
}

So, you could create an array of 'required' fields like this:

$required = array('this', 'that', 'the', 'other');

...and then loop through them:

$errors = false;

foreach ($required as $field)
{
    $field_value = isset($_POST[$field]) ? trim($_POST[$field]) : null;

    if (empty($field_value))
    {
        $errors[] = 'The "' . $field . '" field was not submitted.';
    }
}

if ($errors)
{
    // Do something now that you know there are missing fields.
    // Here, we're sending an email...
    $subject = 'These fields were not filled out';
    $body    = 'The following errors occurred: ' . implode(', ', $errors); 
    mail('email@example.com', $subject, $body);
} 
TunaMaxx
  • 1,782
  • 12
  • 18
  • So how could I get the errors to be sent to me in an email without it preventing the form from being submitted? – Allen Levings Aug 20 '14 at 16:49
  • Send your mail in the `if ($errors) {}` part. The rest of the code is not affecting the $_POST vars at all, or getting in the way of the submission process. – TunaMaxx Aug 20 '14 at 19:43