I am building a confidentiality form which I would like to submit to a printable page and to two or three email addresses (one being the email address of the person who filled the form out). This means that the language that is above the form needs to be printed out as well. I would also like to put the names into a database so that each time the person logs in that they are not greeted by this form.

Can someone help me or at least direct me to what I should be looking for?

Thanks in advance!


And use PHP mail function to send your email.
http://us2.php.net/function.mail

Hi Langsor,

Thanks so much for helping me. I took a look at this link and it is great information. But I guess my main question is that I would like to know if I can send an email to the address that is filled out in the form. I have been searching and searching for info on this for a few days now, but so far I have gotten no where. I understand that there is a way to authenticate an e-mail address that is filled into a form, but I still have not found any code which puts the e-mail address into my code so that I can send that person a copy of the form.

Is there a way I can do that? Or do I have to figure out a different way to get the form to the client?

Member Avatar for langsor

Hi,

Getting the email address to the mail function is pretty easy if the user enters their email with the form information.

main_form.html

<form action="form_script.php" method="POST">
  <label>Name: <input type="text" name="name" /></label>
  <label>Email: <input type="text" name="email" /></label>
  <label>... some other form values ...</label>
  <input type="submit" value="Submit This Form" />
</form>

form_script.php

<?php
// handle the form values here, database insertion, etc
// ...

// handle the form email here ...
$name = $_POST['name'];
$email = $_POST['email'];
// validate email address with PHP here ...

// here you will need to glue together the many form values into
// some email-able summary for the client and your other recipients
$form = <<<ENDFORM
    This is my summary of the form with submitted values from the form embedded into it.
Post name: {$_POST['name']}
Post email: {$_POST['email']}
This is some more stuff, you can stick these anywhere {$_POST['whatever']} you like
...
ENDFORM;

$recipient1 = "$name <$email>" // client copy
$recipient2 = "Admin <admin@my_domain.com>";
$recipient3 = "Me <me@my_domain.com>";

$subject = "The cool form you just filled out";

$headers = "To: $recipient1"."\r\n"; // send Addressed-To copy to client
$headers .= "From: $recipient2"."\r\n";
//$headers .= "Cc: Carbon-Copy <copied@some_site.com>"."\r\n";
$headers .= "Bcc: $recipient2, $recipient3"."\r\n"; // send Hidden copy to self and ???
$headers .= "Reply-To: $recipient2"."\r\n";
$headers .= "X-Mailer: PHP-Mailer";

mail( "$recipient1,$recipient2,$recipient3", $subject, $form, $headers );

?>

In the above example I did NOT test it and I suspect there are some errors in the way I set up the Bcc and mail( "$recipient1,$recipient2,$recipient3", ... lines, since I don't know if putting all addresses in the mail( "..." portion will hide the addresses in the Bcc portion ??? So you will want to test all this thoroughly.

Otherwise, I hope this gives you an idea of one way you might handle the form and email for your project -- there are, of course, always more than one way to skin a goose ;-)

Cheers

Hi Langsor,

Your example has worked wonderfully. Now, would you be able to help me make a few fields required? I have tried several different ways, all with varying issues in them that did not make them work the right way.
Thanks very much!

Hi Langsor,

Your example has worked wonderfully. Now, would you be able to help me make a few fields required? I have tried several different ways, all with varying issues in them that did not make them work the right way.
Thanks very much!

Figured it out. Thanks!

Member Avatar for langsor

Hi Langsor,

Your example has worked wonderfully. Now, would you be able to help me make a few fields required? I have tried several different ways, all with varying issues in them that did not make them work the right way.
Thanks very much!

Glad it worked.

The fun thing about making fields required is that you probably want to check them on the client (web browser) before submitting the form -- it's easier for the visitor to change fields before it's submitted -- and this requires JavaScript; but you also want to check them on the server (PHP side) since you can't rely on JavaScript since it can be disabled or circumvented too easily. But then again, most people don't do this.

In JavaScript you walk through the form fields and test the ones that are required. If they don't pass the test, you alert the visitor in some manner.

In PHP you do the same thing, and then re-publish the form with the failed fields highlighted in some manner, while keeping the other fields filled in from the first submission.

There are, of course, multiple ways to accomplish all of the above tasks. Here is one simplified way you might do this with both JavaScript and PHP ... it could be built up to be more comprehensive and use better checking tests, but as an example ...

<?php
if ( $_REQUEST['submit'] ) { // don't test just on page load, form was submitted
  $required = array( 'name','email' ); // required fields array
  $errors = array(); // error output array
  foreach ( $required as $field ) { // test for all required fields
    if ( !$_REQUEST[$field] ) { // if not found
      // I'm skipping the lame email test here ... it's not worth not doing it right and I don't have time
      $errors[] = strtoupper( $field ); // push to output
    }
  }
  if ( count( $errors ) ) { // if errors ...
    $warning = '<br /><strong>There was an error with the form submission.</strong><br />Please complete these fields: ';
    $warning .= implode( ', ', $errors );
  }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.warn {
  color: red;
  font-size: 12px;
  font-family: Arial;
}
</style>
<script type="text/javascript">
window.onload = function () { // load the window before looking for form
  var errors = []; // creaate an array for non-valid fields
  var output = document.getElementById('output').firstChild; // grab feedback element
  var form = document.getElementsByTagName('form').item(0); // good if only one form on page
  form.onsubmit = function () { // form submit => do this stuff
    for ( var i = 0; i < form.length; i ++ ) { // walk through all form elements
      var element = form.elements[i]; // create reference to current element
      if ( element.getAttribute('required') ) { // test if required element
        if ( element.name == 'email' ) { // do extra testing on email element
          if ( !/@/.test( element.value ) ) { // pathetic email validation -- just an example
            errors.push( element.name.toUpperCase() ); // push the bad field to errors array
          }
        } else {
          if ( !element.value ) { // test other fields, this is a simplified method for text fields
            errors.push( element.name.toUpperCase() );
          }
        }
      }
    }
    if ( errors.length ) { // if errors ...
      output.nodeValue = 'Please fill out the following fields: ' + errors.join(', ');
      errors = []; // reset errors for next submission attempt
      return false; // do not submit the form yet
    } else {
      output.nodeValue = 'Fields marked with (*) are required';
    }
  };
};
</script>
</head>
<body>
<form method="POST">
<table>
  <tr>
    <td id="output" class="warn" colspan="2">Fields marked with (*) are required<?php print $warning ?></td>
  </tr>
  <tr>
    <td><label for="name">Name <strong class="warn">*</strong></label></td>
    <td><input type="text" required="required" id="name" name="name" value="<?php print $_REQUEST['name'] ?>" /></td>
  </tr>
  <tr>
    <td><label for="email">Email <strong class="warn">*</strong></label></td>
    <td><input type="text" required="required" id="email" name="email" value="<?php print $_REQUEST['email'] ?>" /></td>
  </tr>
  <tr>
    <td><label for="pizza">Pizza</label></td>
    <td><input type="text" required="" id="pizza" name="pizza" value="<?php print $_REQUEST['pizza'] ?>" /></td>
  </tr>
  <tr>
    <td></td>
    <td><input type="submit" name="submit" /></td>
  </tr>
</table>
</form>
</body>
</html>

Due to the line-wraps it's difficult to understand this example ... I would copy the plain-text (Toggle Plain Text link above) and paste the above code into a code-editor with line numbers and syntax highlighting ...

Hope it helps :-)

...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.