943,928 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1058
  • PHP RSS
Sep 7th, 2008
0

Rather complicated PHP project for novice

Expand Post »
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!
Last edited by bettina702; Sep 7th, 2008 at 2:57 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bettina702 is offline Offline
17 posts
since Aug 2008
Sep 8th, 2008
0

Re: Rather complicated PHP project for novice

Great,

Create a database table for users with a field for if they've gone through the confidentiality page.
http://dev.mysql.com/doc/

Use sessions and or cookies so logged in users can skip the page.
http://us2.php.net/manual/en/book.session.php

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

If you have specific questions I will try and help with them.

Cheers
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Sep 8th, 2008
0

Re: Rather complicated PHP project for novice

Click to Expand / Collapse  Quote originally posted by langsor ...

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bettina702 is offline Offline
17 posts
since Aug 2008
Sep 8th, 2008
0

Re: Rather complicated PHP project for novice

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
html Syntax (Toggle Plain Text)
  1. <form action="form_script.php" method="POST">
  2. <label>Name: <input type="text" name="name" /></label>
  3. <label>Email: <input type="text" name="email" /></label>
  4. <label>... some other form values ...</label>
  5. <input type="submit" value="Submit This Form" />
  6. </form>

form_script.php
php Syntax (Toggle Plain Text)
  1. <?php
  2. // handle the form values here, database insertion, etc
  3. // ...
  4.  
  5. // handle the form email here ...
  6. $name = $_POST['name'];
  7. $email = $_POST['email'];
  8. // validate email address with PHP here ...
  9.  
  10. // here you will need to glue together the many form values into
  11. // some email-able summary for the client and your other recipients
  12. $form = <<<ENDFORM
  13. This is my summary of the form with submitted values from the form embedded into it.
  14. Post name: {$_POST['name']}
  15. Post email: {$_POST['email']}
  16. This is some more stuff, you can stick these anywhere {$_POST['whatever']} you like
  17. ...
  18. ENDFORM;
  19.  
  20. $recipient1 = "$name <$email>" // client copy
  21. $recipient2 = "Admin <admin@my_domain.com>";
  22. $recipient3 = "Me <me@my_domain.com>";
  23.  
  24. $subject = "The cool form you just filled out";
  25.  
  26. $headers = "To: $recipient1"."\r\n"; // send Addressed-To copy to client
  27. $headers .= "From: $recipient2"."\r\n";
  28. //$headers .= "Cc: Carbon-Copy <copied@some_site.com>"."\r\n";
  29. $headers .= "Bcc: $recipient2, $recipient3"."\r\n"; // send Hidden copy to self and ???
  30. $headers .= "Reply-To: $recipient2"."\r\n";
  31. $headers .= "X-Mailer: PHP-Mailer";
  32.  
  33. mail( "$recipient1,$recipient2,$recipient3", $subject, $form, $headers );
  34.  
  35. ?>
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
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008
Sep 13th, 2008
0

Re: Rather complicated PHP project for novice

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bettina702 is offline Offline
17 posts
since Aug 2008
Sep 13th, 2008
0

Re: Rather complicated PHP project for novice

Click to Expand / Collapse  Quote originally posted by bettina702 ...
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bettina702 is offline Offline
17 posts
since Aug 2008
Sep 13th, 2008
0

Re: Rather complicated PHP project for novice

Click to Expand / Collapse  Quote originally posted by bettina702 ...
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 Syntax (Toggle Plain Text)
  1. <?php
  2. if ( $_REQUEST['submit'] ) { // don't test just on page load, form was submitted
  3. $required = array( 'name','email' ); // required fields array
  4. $errors = array(); // error output array
  5. foreach ( $required as $field ) { // test for all required fields
  6. if ( !$_REQUEST[$field] ) { // if not found
  7. // I'm skipping the lame email test here ... it's not worth not doing it right and I don't have time
  8. $errors[] = strtoupper( $field ); // push to output
  9. }
  10. }
  11. if ( count( $errors ) ) { // if errors ...
  12. $warning = '<br /><strong>There was an error with the form submission.</strong><br />Please complete these fields: ';
  13. $warning .= implode( ', ', $errors );
  14. }
  15. }
  16. ?>
  17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  18. <html xmlns="http://www.w3.org/1999/xhtml">
  19. <head>
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  21. <title>Untitled Document</title>
  22. <style type="text/css">
  23. .warn {
  24. color: red;
  25. font-size: 12px;
  26. font-family: Arial;
  27. }
  28. </style>
  29. <script type="text/javascript">
  30. window.onload = function () { // load the window before looking for form
  31. var errors = []; // creaate an array for non-valid fields
  32. var output = document.getElementById('output').firstChild; // grab feedback element
  33. var form = document.getElementsByTagName('form').item(0); // good if only one form on page
  34. form.onsubmit = function () { // form submit => do this stuff
  35. for ( var i = 0; i < form.length; i ++ ) { // walk through all form elements
  36. var element = form.elements[i]; // create reference to current element
  37. if ( element.getAttribute('required') ) { // test if required element
  38. if ( element.name == 'email' ) { // do extra testing on email element
  39. if ( !/@/.test( element.value ) ) { // pathetic email validation -- just an example
  40. errors.push( element.name.toUpperCase() ); // push the bad field to errors array
  41. }
  42. } else {
  43. if ( !element.value ) { // test other fields, this is a simplified method for text fields
  44. errors.push( element.name.toUpperCase() );
  45. }
  46. }
  47. }
  48. }
  49. if ( errors.length ) { // if errors ...
  50. output.nodeValue = 'Please fill out the following fields: ' + errors.join(', ');
  51. errors = []; // reset errors for next submission attempt
  52. return false; // do not submit the form yet
  53. } else {
  54. output.nodeValue = 'Fields marked with (*) are required';
  55. }
  56. };
  57. };
  58. </script>
  59. </head>
  60. <body>
  61. <form method="POST">
  62. <table>
  63. <tr>
  64. <td id="output" class="warn" colspan="2">Fields marked with (*) are required<?php print $warning ?></td>
  65. </tr>
  66. <tr>
  67. <td><label for="name">Name <strong class="warn">*</strong></label></td>
  68. <td><input type="text" required="required" id="name" name="name" value="<?php print $_REQUEST['name'] ?>" /></td>
  69. </tr>
  70. <tr>
  71. <td><label for="email">Email <strong class="warn">*</strong></label></td>
  72. <td><input type="text" required="required" id="email" name="email" value="<?php print $_REQUEST['email'] ?>" /></td>
  73. </tr>
  74. <tr>
  75. <td><label for="pizza">Pizza</label></td>
  76. <td><input type="text" required="" id="pizza" name="pizza" value="<?php print $_REQUEST['pizza'] ?>" /></td>
  77. </tr>
  78. <tr>
  79. <td></td>
  80. <td><input type="submit" name="submit" /></td>
  81. </tr>
  82. </table>
  83. </form>
  84. </body>
  85. </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 :-)

...
Reputation Points: 30
Solved Threads: 36
Posting Whiz
langsor is offline Offline
389 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: display the data into listbox
Next Thread in PHP Forum Timeline: php/java bridge on ubuntu hardy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC