www.webdeveloper.com
Results 1 to 1 of 1

Thread: [RESOLVED] contact form issues - pushing header down

  1. #1
    Join Date
    Jun 2006
    Location
    Boston, MA
    Posts
    383

    resolved [RESOLVED] contact form issues - pushing header down

    hey all. im working on a contact form. i made a dropdown list so you could select a department but for whatever reason its not working and its displaying the option select text outside of the dropdown itself. im fairly new to PHP, can anybody help me out?

    on a side note, originally the script went to a 'thank you' page after submission, but i want to display the message sent message on the page itself. i tried with an echo but it didn't work, any thoughts on that one either?
    PHP Code:
    $success '';
            
    $success .= 'Your message sent!';
            
    //header('Location: contact.php'); 


    PHP Code:
    <?php 
    session_start
    ();

    $errors '';
    $name '';
    $visitor_email '';
    $visitor_telephone '';
    $visitor_subject '';
    $visitor_hear '';
    $user_message '';


        
    $contactAry = array(
        
    'Sales' => 'matthew@gmail.com',
        
    'Advertising' => 'matthew@gmail.com',
        );

        
        
    if(isset(
    $_POST['submit']))
    {
        
        if(!
    array_key_exists($_POST['department'], $contactAry))
        {
        echo 
    "Invalid department";
        }
        else
        {
        
    $your_email $contactAry[$_POST['department']];
        }
        
        
    ///------------Do Validations-------------
        
    if(empty($name)||empty($visitor_email))
        {
            
    $errors .= "\n Please Fill out Required Fields. ";    
        }
        if(
    IsInjected($visitor_email))
        {
            
    $errors .= "\n Bad email entry!";
        }
        if(empty(
    $_SESSION['6_letters_code'] ) ||
          
    strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
        {
        
    //Note: the captcha code is compared case insensitively.
        //if you want case sensitive match, update the check above to
        // strcmp()
            
    $errors .= "\n <div class='captcha-miss'>The captcha code does not match!</div>";
        }
        
        if(empty(
    $errors))
        {
            
    //send the email
            
    $to $your_email;
            
    $subject="Form Submission From Contact Page";
            
    $from $your_email;
            
    $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
            
            
    $body "A user  $name submitted the contact form:\n".
            
    "Name: $name\n".
            
    "Email: $visitor_email \n".
            
    "Telephone: $visitor_telephone \n\n".
            
    "How did you hear about us? $visitor_hear \n\n".
            
    "Subject: $visitor_subject \n\n".
            
    "Message: \n ".
            
    "$user_message\n".
            
    "IP: $ip\n";    
            
            
    $headers "From: $from \r\n";
            
    $headers .= "Reply-To: $visitor_email \r\n";
            
            
    mail($to$subject$body,$headers);
            
            
    $success '';
            
    $success .= 'Your message sent!';
            
    //header('Location: contact.php');
        
    }
    }

    // Function to validate against any email injection attempts
    function IsInjected($str)
    {
      
    $injections = array('(\n+)',
                  
    '(\r+)',
                  
    '(\t+)',
                  
    '(%0A+)',
                  
    '(%0D+)',
                  
    '(%08+)',
                  
    '(%09+)'
                  
    );
      
    $inject join('|'$injections);
      
    $inject "/$inject/i";
      if(
    preg_match($inject,$str))
        {
        return 
    true;
      }
      else
        {
        return 
    false;
      }
    }
    ?>

    HTML Code:
    <div id="contact-form-wrap">
    <?php
    if(!empty($errors)){
    echo "<p class='err'>".nl2br($errors)."</p>";
    }
    ?>
    <div id='contact_form_errorloc' class='err'></div>
    <form method="POST" name="contact_form" 
    action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
    <p>
    <label for='name'><strong>Department</strong></label><br>
    <select name="destemail">
    <?php 
    echo "<select name='department'>\n";
    foreach($contactAry as $department => $your_email)
    {
        echo "<option value='$department'>$department</option>\n";
    }
    echo "</select>\n";
     ?>
    </select>
    </p>
    <p>
    <label for='name'><strong>Name</strong></label><br>
    <input type="text" name="name" value='<?php echo htmlentities($name) ?>' class="contact_input">
    </p>
    <p>
    <label for='email'><strong>Email</strong></label><br>
    <input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>' class="contact_input">
    </p>
    <p>
    <label for='telephone'><strong>Telephone</strong></label><br>
    <input type="text" name="telephone" value='<?php echo htmlentities($visitor_telephone) ?>' class="contact_input">
    </p>
    <p>
    <label for='hear'><strong>How did you hear about us?</strong></label><br>
    <input type="text" name="hear" value='<?php echo htmlentities($visitor_hear) ?>' class="contact_input">
    </p>
    <p>
    <label for='subject'><strong>Subject</strong></label><br>
    <input type="text" name="subject" value='<?php echo htmlentities($visitor_subject) ?>' class="contact_input">
    </p>
    <p>
    <label for='message'><strong>Message</strong></label> <br>
    <textarea name="message" rows=8 cols=30 style="border:none;"><?php echo htmlentities($user_message) ?></textarea>
    </p>
    <p>
    <img src="script/captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br />
    <label for='message'>Enter the code above here :</label><br>
    <input id="6_letters_code" name="6_letters_code" type="text" style="border:none;"><br />
    <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
    </p>
    <input type="submit" value="Submit" name='submit' class="rapidcharge-contact-btn">
    </form>
    <script type="text/javascript">
    var frmvalidator  = new Validator("contact_form");
    //remove the following two lines if you like error message box popups
    frmvalidator.EnableOnPageErrorDisplaySingleBox();
    frmvalidator.EnableMsgsTogether();
    
    frmvalidator.addValidation("name","req","You forgot to enter your name!"); 
    frmvalidator.addValidation("email","req","You forgot to enter your email!"); 
    frmvalidator.addValidation("email","email","Please enter a valid email address"); 
    frmvalidator.addValidation("subject","req","You forgot to enter a subject!"); 
    frmvalidator.addValidation("hear","req","How did you hear about us?");
    frmvalidator.addValidation("message","req","No message? hrmph!"); 
    </script>
    <script type="text/javascript">
    function refreshCaptcha()
    {
    	var img = document.images['captchaimg'];
    	img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
    }
    </script>
    
    </div>
    Last edited by absolutmgd13; 06-13-2012 at 03:19 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles