Form Email Content

Hi, whenever a user fills out a form and leave certain inputs that
are not required, it still sends that input field in the email. is their
a way not send the empty fields?

this is an example of what I got in the email test I did to myself
I didnt fill in certain fields and they were still sent blank.

You can just set up some if statements to filter out results if they are blank.

Thought so. May I get an example of one? I am not too familiar
with PHP much.

  <input type="text" name="IfYesAllergies" placeholder="If Yes, Allergic to?">

It’s more to do with the PHP code that builds the body of your email message. Could you post your PHP script?

Ill put for download its pretty long thats why.
meddentalinfoengine.php (8.8 KB)

I’m not a PHP person (or a programmer), so what I’m going to suggest will work, but it’s very inefficient. Still, we noobs have to survive somehow!

So, for each item you want to make conditional, you could replace something like this—

$Body .= "Age: ";
$Body .= $PatientAge;
$Body .= "\n";

with this

if ($_POST['PatientAge']) {
    $Body .= "Age: ";
    $Body .= $PatientAge;
    $Body .= "\n";
}

That way, you’ll only see output in the email if something was actually entered in that field.

As I said, though, it’s very inefficient to write this out each time, but I’ll leave it to someone else to make your code more efficient. :slight_smile:

1 Like

I see thank you though!!:smile:

I should have asked if there’s a separate script to handle the $validation. If so, there might be more to it than this. Without it, your form is pretty insecure.

No I don’t I am using HTML5: Form Validation, leaving the work to
the browser.

required="required"

Should I be using JS? or is that a choice now a days?
Is using JS better?

First thing I would try is using something like this to get rid of ~60 lines of code

foreach($_POST as $key => $value) {
  $post_key = $key;
  $$post_key = trim(stripslashes($value));
}

* where are the slashes coming from?
is Trim a custom function or a typo?

1 Like

Not sure I got this from another source.

What is this for?

foreach($_POST as $key => $value) {
  $post_key = $key;
  $$post_key = trim(stripslashes($value));
}

sorry not familiar with PHP much…im a noob

To get rid of the ~60 lines of code that essentially all do the same thing.
for example, instead of
$PatientName = Trim(stripslashes($_POST['PatientName']));
for each $_POST variable, it loops through them all creating a variable from the key and and assigning the “processed” value

1 Like

oh ok I see

so just to be clear I delete all this:

$PatientName = Trim(stripslashes($_POST['PatientName'])); 
$PatientAge = Trim(stripslashes($_POST['PatientAge'])); 
$PhysName = Trim(stripslashes($_POST['PhysName']));  

etc etc…

and replace with this:

foreach($_POST as $key => $value) {
  $post_key = $key;
  $$post_key = trim(stripslashes($value));
}

Yes - provided that timming off the leading and trailing whitespace and removing slashes is all that is needed to ensure that all the fields contain valid values. If that isn’t the case then your original code doesn’t work either (even if you were to fix the typo)

Both of those are handy, but largely useless in terms of preventing malicious injections by spammers.

The exact details of constraint validation aren’t all that clear to me, but AFAICT it’s more for immediate feedback to the user than it is to replace proper validation / sanitization

That is, use it to help honest users, but don’t rely on it alone for protection against malicious users.

1 Like

Any client side validation should be repeated on the server as a minimum (assuming that there is no validation that can’t be done easily in the browser that you only apply on the server).

Some older browsers don’t even recognise the validation from html. It is useful, but not to be relied upon.

Since you are using php to process the form, it would seem natural to use php for validation. Using server side scripting would also get around problems with people not having js enabled.

Use Javascript AND PHP for validation. Javascript to save users the refresh that PHP gives, but obviously plan for JS being off, as Sam mentioned.

1 Like

True, at the time of writing, only Chrome and Opera supported HTML5 validation,
although other browsers are expected to follow. Most developers at the moment
will continue using Javascript to validate forms…