Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    0
    ReputationRep:
    I have a little problem with my contact form, basically atm I need to paste this code on every page before the HTML tags:

    Code:
    <?php
    //If the form is submitted
    if(isset($_POST['submit'])) {
    
       //Check to make sure that the name field is not empty
       if(trim($_POST['contactname']) == '') {
          $hasError = true;
       } else {
          $name = trim($_POST['contactname']);
       }
    
       //Check to make sure that the subject field is not empty
       if(trim($_POST['subject']) == '') {
          $hasError = true;
       } else {
          $subject = trim($_POST['subject']);
       }
    
       //Check to make sure sure that a valid email address is submitted
       if(trim($_POST['email']) == '')  {
          $hasError = true;
       } else if (!eregi("^[A-Z0-9._%-][email protected][A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
          $hasError = true;
       } else {
          $email = trim($_POST['email']);
       }
    
       //Check to make sure comments were entered
       if(trim($_POST['message']) == '') {
          $hasError = true;
       } else {
          if(function_exists('stripslashes')) {
             $comments = stripslashes(trim($_POST['message']));
          } else {
             $comments = trim($_POST['message']);
          }
       }
    
       //If there is no error, send the email
       if(!isset($hasError)) {
          $emailTo = '[email protected]'; //Put your own email address here
          $body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
          $headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
    
          mail($emailTo, $subject, $body, $headers);
          $emailSent = true;
       }
    }
    ?>
    And this is the code for the contact form:

    Code:
    <div id="contact-wrapper">
    
       <?php if(isset($hasError)) { //If errors are found ?>
          <p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
       <?php } ?>
    
       <?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
          <p><b>Email Successfully Sent!</b></p>
          <p>Thank you <strong><?php echo $name;?></strong> for getting in contact with us! Your email was successfully sent and we will be in touch with you soon.</p>
       <?php } ?>
    
       <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="contactform">
          <div>
              <label for="name"><strong>Name:</strong></label>
             <input type="text" size="50" name="contactname" id="contactname" value="" class="required" />
          </div>
    
          <div>
             <label for="email"><strong>Email:</strong></label>
             <input type="text" size="50" name="email" id="email" value="" class="required email" />
          </div>
    
          <div>
             <label for="subject"><strong>Phone Number:</strong></label>
             <input type="text" size="50" name="subject" id="subject" value="" class="required" />
          </div>
    
          <div>
             <label for="message"><strong>Message:</strong></label>
             <textarea rows="5" cols="50" name="message" id="message" class="required"></textarea>
          </div>
           <input type="submit" value="Send Message" name="submit" class="submit" />
       </form>
       </div>
    My question is how could I do it so that the php file is in its own file? So I don't have to manually paste that onto each page with the contact form?

    Any help will be appreciated, thanks
    Offline

    0
    ReputationRep:
    Hey,

    You could try using the include() command.

    http://php.net/manual/en/function.include.php

    Place the php code in a file called X.php and at the top of every html file, just include the php file: e.g. include(X.php);
    Offline

    3
    ReputationRep:
    Aye use include() or require().

    Also you should be cleaning the POST string to make sure it doesn't contain any nasty injection code etc...

    Try adding this function:

    PHP Code:
    function cleanString($string) {
    $string mysql_real_escape_string($string);
    $string strip_tags($string);

    Then you can clean a string by calling the function, for example: $name = cleanString($name);

    That isn't a fully comprehensive example function I've come up with there but it is important that you clean PHP variables.
    • Thread Starter
    Offline

    0
    ReputationRep:
    Ahhh so simple! Thanks a lot for your help!!
    • PS Helper
    Offline

    2
    ReputationRep:
    Hmm.... I always POST to the seperate process.php then do a server-side HTTP redirect. It takes up about 2 lines of PHP (try searching for it)
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Did TEF Bronze Award affect your UCAS choices?
    Useful resources
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.