Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Contact Form HELP

    I recently downloaded a template for a client and the contact form doesnt come with any PHP or JavaScript to make it actually work... The HTML code is as follows:

    <div class="col span_2_of_3">
    <div class="contact-form">
    <h3>Contact Us</h3>
    <form>
    <div>
    <span><label>NAME</label></span>
    <span><input type="text" value=""></span>
    </div>
    <div>
    <span><label>E-MAIL</label></span>
    <span><input type="text" value=""></span>
    </div>
    <div>
    <span><label>MOBILE.NO</label></span>
    <span><input type="text" value=""></span>
    </div>
    <div>
    <span><label>SUBJECT</label></span>
    <span><textarea> </textarea></span>
    </div>
    <div>
    <span><input type="submit" value="Submit"></span>
    </div>
    </form>
    </div>
    </div>

    Can somebody help me write the code to make this form actually work to send the filled out information to their email address?

    Any and all help is much appreciated!

  • #2
    New Coder
    Join Date
    May 2014
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hello Stone84
    Welcome to the Coding Forums.

    You can use PHP to make the form work.

    You will first need to give every input an "id" attribute, as well as the labels the "for" attribute. The for attribute is just to allow the clicking of the label to activate the textbox.
    You will also need to add the "action" & "method" attribute to the form tag.
    A quick validation test can be used by creating a "dummy" input.
    Code:
    <h3>Contact Us</h3>
    <form method="POST" action="form_send.php">
    <div>
    <span><label for="name">NAME</label></span>
    <span><input type="text" id="name" name="name" value=""></span>
    </div>
    <div>
    <span><label for="email">E-MAIL</label></span>
    <span><input type="text" id="email" id="email" value=""></span>
    </div>
    <div>
    <span><label for="phone">MOBILE.NO</label></span>
    <span><input type="text" id="phone" name="phone" value=""></span>
    </div>
    <div>
    <span><label for="subject">SUBJECT</label></span>
    <span><textarea id="subject" name="subject"> </textarea></span>
    </div>
    <div style="display: none;">
    <span><label for="address">address</label></span>
    <span><input type="text" id="address" name="address">
    </div>
    <div>
    <span><input type="submit" id="submit" value="Submit"></span>
    </div>
    </form>
    Next, you will want to create a new file and name it 'form_send.php'
    Code:
    <?php
    
    $email_to = "your@email.com"; // your email address
    $email_subject = "Form Submission"; // email subject line
    
    $name = $_POST['name'];
    $email = $_POST['email'];
    $number = $_POST['number'];
    $subject = $_POST['subject'];
    $spamtest = $_POST['address'];
    
    // SIMPLE VALIDATION CAN BE USED HERE //
    
    if ($name == "" OR $name == null) {
    echo ('Please enter a valid name!');
    exit;
    }
    if ($email == "" OR $email == null) {
    echo ('Please enter a valid email!');
    exit;
    }
    if ($number == "" OR $number == null) {
    echo ('Please enter a valid number!');
    exit;
    }
    if ($subject == "" OR $subject == null) {
    echo ('Please enter a valid subject!');
    exit;
    }
    // CHECK ANTI-SPAM with $spamtest
    if ($spamtest != "" OR $spamtest != null) {
    exit;
    }
    
    function clean_string($string) {
     $bad = array("content-type","bcc:","to:","cc:");
     return str_replace($bad,"",$string);
    }
    $email_message .= "Full Name: ".clean_string($name)."\r\n";
    $email_message .= "Email: ".clean_string($email)."\r\n";
    $email_message .= "Phone Number: ".clean_string($number)."\r\n";
    $email_message .= "Subject: ".clean_string($subject)."\r\n";
    	
    mail($email_to, $email_subject, $email_message);
    header("Location: http://yoursite.com/formredirectpage");
    die();
    ?>
    You should look into some more in-depth form validation for your form, maybe with javascript.

    Now to get into how this works.
    First, you assign the values from your form to a variable.

    Next, you check to make sure that the values inserted into your form fields are not blank or empty.

    After that, we use the antispam "trick" to trick any automated form bots from sending a form by saying:
    if ($antispam != ""), this is saying if the value of the field IS NOT blank, then exit thr form and dont allow it to send.

    Once that is done, we send the message using the mail() function.

    The header("Location: ") will redirect the user to the page you choose by putting your link in that area.

    That will allow a user to send there form. Change the $email_to variable to your email address and the $email_subject to the subject you want to see in your email client.

    Please feel free to PM me or post more questions here if you have them.

    Hope this helps!
    Last edited by Captshannon; 06-17-2014 at 05:31 AM.

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    45
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Do you have any budget for this form? There are simple, painless services out there that allow for creation with a click of the button. If you want to have a truly custom form but the data being collected in a database try expressdb.com. They give you an ID number to stick in your form action and that's it. The form is 100% in your hands, they gather the data.


  •  

    Tags for this Thread

    Posting Permissions

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