Thread: Contact Form

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Contact Form


    Hi there,
    I am building a website at the moment using Dreamweaver and need to do a form on the contact page. Can anyone help me produce a working form with Php code and a thankyou for contacting us page ?? The form fields are Name:, Email:, Tel:, Message: and the Submit button.
    Any help would be greatly appreciated, as I am pulling out the hair I have left grrrrrrrrr!!

    This post has been edited by Rayb: Today, 01:08 AM
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0
    With out knowing exactly what you want the PHP to do here is a basic example:

    Code:
    <form action="url to the php script .php" method = "POST" >
    
    <label for="name">Name:</label><input type="text" name="name" />
    
    <label for="email">Email:</label><input type="email" name="email" />
    
    <label for="tel">Telephone:</label><input type="text" name="tel" />
    
    <label for="message">Message:</label><textarea name="message"></textarea>
    
    
    </form>
    And here is the php:

    PHP Code:

    <?php

    $name 
    mysql_real_escape_string($_POST['name']);
    $emailmysql_real_escape_string($_POST['email']);
    $telmysql_real_escape_string($_POST['tel']);
    $messagemysql_real_escape_string($_POST['message']);

    // do what you wish with the values.

    ?>

    This is a very basic example but hopefully this gives you an idea of what to do.
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    Welcome to DevShed Forums, raybrown.

    "A little knowledge is a dangerous thing." You will probably want to do some error checking in the PHP code as well.

    @tomVance The "for" attribute of <label> elements should reference the ID of a form field. Your form fields lack IDs.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by Kravvitz
    Welcome to DevShed Forums, raybrown.

    "A little knowledge is a dangerous thing." You will probably want to do some error checking in the PHP code as well.

    @tomVance The "for" attribute of <label> elements should reference the ID of a form field. Your form fields lack IDs.
    Kravviz is right the labels need to relates to the ID attribute for the input elements i missed them out, sorry.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Contact form


    Thanks TomVance

    I want the PHP to send the contents from the input fields in the form and display a thankyou message to say we will be in contact soon, see my coding below, hope you can help or make it simple for me :-)

    Code:
    <form name="contactform" method="post" action="send_form_email.php">
    <table width="450px" align="center">
     <tr>
       <td valign="top">
          <label for="first_name">First Name *</label>
            </td>
            
              <td valign="top">
        <input  type="text" name="first_name" maxlength="50" size="30">
          </td>
           </tr>
           
    <tr>
      <td valign="top">
         <label for="last_name">Last Name *</label>
           </td>  
           <td valign="top">
              <input  type="text" name="last_name" maxlength="50" size="30"> 
               </td>
                </tr>
                
    <tr>
      <td valign="top">
         <label for="email">Email Address *</label>
           </td>
             <td valign="top">  
              <input  type="text" name="email" maxlength="80" size="30"> 
               </td>
                </tr>
                
    <tr> 
     <td valign="top">
        <label for="telephone">Telephone Number</label> 
         </td>
         
    <td valign="top">
       <input  type="text" name="telephone" maxlength="30" size="30">  
       </td>
        </tr>
        
    <tr>
      <td valign="top">
         <label for="comments">Comments *</label> 
          </td>
          
          <td valign="top">
             <textarea  name="comments" maxlength="1000" cols="25" rows="6">
             </textarea>
               </td>
                </tr>
                
                 <tr>  <td colspan="2" style="text-align:center">   
                 <input type="submit" value="Submit">
                   </td> 
                   </tr>
                   
                    </table> </form>
    And here is the php:

    PHP Code:

    <?php
     
    if(isset($_POST['email'])) {
    $email_to "";
    $email_subject "Your email subject line";
    function 
    died($error) {
    // error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo 
    "These errors appear below.<br /><br />";
    echo 
    $error."<br /><br />";
    echo 
    "Please go back and fix these errors.<br /><br />";
    die(); 
                                                                                                            }
     
    // validation expected data exists
    if(!isset($_POST['first_name']) ||
    !isset(
    $_POST['last_name']) ||
    !isset(
    $_POST['email']) ||
    !isset(
    $_POST['telephone']) ||
    !isset(
    $_POST['comments']))
    {     
    died('We are sorry, but there appears to be a problem with the form you submitted.'); } 
    $first_name $_POST['first_name'];
     
    // required 
    $last_name $_POST['last_name'];
     
    // required    
     
    $email_from $_POST['email'];
     
    // required
    $telephone $_POST['telephone'];
     
    // not required
    $comments $_POST['comments'];
    // required
    $error_message ""
    $email_exp '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
    if(!
    preg_match($email_exp,$email_from)) {     $error_message .= 'The Email Address you entered does not appear to be valid.<br />';} 
    $string_exp "/^[A-Za-z .'-]+$/";
    if(!
    preg_match($string_exp,$first_name)) {     $error_message .= 'The First Name you entered does not appear to be valid.<br />';}
    if(!
    preg_match($string_exp,$last_name)) {     $error_message .= 'The Last Name you entered does not appear to be valid.<br />';}
    if(
    strlen($comments) < 2) {     $error_message .= 'The Comments you entered do not appear to be valid.<br />';}
    if(
    strlen($error_message) > 0) {     died($error_message);   }
    $email_message "Form details below.\n\n";
    function 
    clean_string($string) {    $bad = array("content-type","bcc:","to:","cc:","href");
    return 
    str_replace($bad,"",$string);     }
    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";     $email_message .= "Comments: ".clean_string($comments)."\n"; }
      
    ?>
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Given that the OP contains an edit message from another forum, I'm sure nobody is surprised that he's been cross-posting to at least 2 other forums.

    This thread is a spam magnet, so I'm closing it.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo