Advertisement
gitlez

YA: Simple Contact Form Processing

Sep 26th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. <?php
  2. /*
  3.     Answer Script For Yahoo Question:
  4.     http://answers.yahoo.com/question/index?qid=20110924135432AAWKmca
  5. */
  6.  
  7. $fileName = 'file.txt'; // The name of the file, as to where the data will be saved.
  8. $template = '
  9. <div style="border: 1px dotted #999;padding: 8px;">
  10.    <p><span style="font-style: italic;">Name</span>: <span style="font-weight: bold">{NAME}</span></p>
  11.    <p><span style="font-style: italic;">Email</span>: <span style="font-weight: bold">{EMAIL}</span></p>
  12.    <p><span style="font-style: italic;">Message</span>:<br /> <span style="font-weight: bold">{MESSAGE}</span></p>
  13. </div>
  14. '; // Template of how you want the message to be displayed.
  15. $showForm = true; // Whether or not to show the form
  16.  
  17. if(isset($_POST['submit'])){ // The Form Has been Submitted
  18.     $name = $_POST['vardas'];
  19.     $email = $_POST['mail'];
  20.     $message = $_POST['tekstas'];
  21.     if(strlen($name) === 0 || strlen($email) === 0 || strlen($message) === 0){
  22.         echo 'All fields are required.<br />';
  23.     }else{
  24.         $templateFills = Array(
  25.         '{NAME}' => $name,
  26.         '{EMAIL}' => $email,
  27.         '{MESSAGE}' => $message
  28.         );
  29.         $string = file_get_contents($fileName) or '';
  30.         $string .= str_replace( array_keys($templateFills), array_values($templateFills), $template);
  31.         file_put_contents($fileName, $string);
  32.         echo $string;
  33.         $showForm = false;
  34.     }
  35. }
  36.  
  37. if($showForm){
  38. echo '
  39. <form action="" method="post" name="forma">
  40.    <p>
  41.        Vardas<br />
  42.        <input type="text" name="vardas" value="' . $_POST['vardas'] . '" />
  43.    </p>
  44.    <p>
  45.        E-mail<br />
  46.        <input type="text" name="mail" value="' . $_POST['mail'] . '" />
  47.    </p>
  48.    <p>
  49.        Tekstas<br />
  50.        <textarea cols="30" rows="8" name="tekstas">' . $_POST['tekstas'] . '</textarea>
  51.    </p>
  52.    <br/>
  53.    <input value="Spausk" type="submit" name="submit" />
  54. </form>
  55. ';
  56. }
  57. ?>
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement