Quantcast
Channel: Adobe Community: Message List - PHP Contact Form Help
Viewing all articles
Browse latest Browse all 13

Re: PHP Contact Form Help

$
0
0

Hi Sam2322, I feel your pain. I had exactly the same problem with several forms and their accompanying PHP scripts. It's not a server problem. It's purely to do with the interaction between your form and the PHP script.

 

This is the PHP script I ended up using. It works for me AND I HOPE IT WORKS FOR YOU TOO.  It has a honey trap included to reduce the occurrence of SPAM.

<?php

$name = '';    

$subject = '';   

$email = '';

$message = '';

   

function getIp()

{if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){

    $ip_address=$_SERVER['HTTP_X_FORWARDED_FOR'];

}

 

if (!isset($ip_address)){

        if (isset($_SERVER['REMOTE_ADDR']))   

        $ip_address=$_SERVER['REMOTE_ADDR'];

}

return $ip_address;

}

 

 

//taking the data from form   

 

$name = addslashes(trim($_POST['name']));   

$subject = addslashes(trim($_POST['subject']));   

$email = addslashes(trim($_POST['email']));

$message = addslashes(trim($_POST['message']));

 

//form validation

$errors = array();

$fields = array();

if(!$name) {

    $errors[] = "Please enter your name.";

    $fields[] = "name";

}

$email_pattern = "/^[a-zA-Z0-9][a-zA-Z0-9\.-_]+\@([a-zA-Z0-9_-]+\.)+[a-zA-Z]+$/";

if(!$email) {

    $errors[] = "Please enter your e-mail address.";

    $fields[] = "email";

} else if(!preg_match($email_pattern, $email)) {

    $errors[] = "The e-mail address you provided is invalid.";

    $fields[] = "email";   

}

if(!$subject) {

    $errors[] = "Please choose the subject of your message.";

    $fields[] = "subject";

}

if(!$message) {

    $errors[] = "Please enter your message.";

    $fields[] = "message";

}

 

//preparing mail

if(!$errors) {

    //taking info about date, IP and user agent

    $timestamp = date("Y-m-d H:i:s");

    $ip   = getIp();

    $host = gethostbyaddr($ip);

    $user_agent = $_SERVER["HTTP_USER_AGENT"];

 

    $headers = "MIME-Version: 1.0\n";

    $headers .= "Content-type: text/html; charset=utf-8\n";

    $headers .= "Content-Transfer-Encoding: quoted-printable\n";

    $headers .= "From: $email\n";

 

    $content = 'Subject: '.$subject.'<br>'.

    'Name: '.$name.'<br>'.

    'E-mail: '.$email.'<br>'.

    'Message: '.$message.'<br>'.

    'Time: '.$timestamp.'<br>'.

    'IP: '.$host.'<br>'.

    'User agent: '.$user_agent;   

 

    //sending mail

    $ok = mail("PUT THE REQUIRED EMAIL ADDRESS HERE","Message from ADD YOUR DOMAIN NAME HERE", $content, $headers);

    if($ok) {

        $response['msgStatus'] = "ok";

        $response['message'] = "Thank you for your enquiry. A member of our team will be in touch within 24 business hours.";

    } else {

        $response['msgStatus'] = "error";

        $response['message'] = "An error occured while trying to send your message. Please try again later.";

    }

} else {

    $response['msgStatus'] = "error";

    $response['errors'] = $errors;

    $response['errorFields'] = $fields;

}

 

header('Content-type: application/json');

echo json_encode($response);

$robotest = $_POST['robotest']; 

if($robotest){

  we are dealing with a bot

else{

  we are dealing with a human

}

?>

 

and here is the form that goes with it

 

 

<form action="send-contact.php" method="post" class="contact-form">

 

                <p class="half">

                    <label for="name">Name</label>

                  <input name="name" class="half" id="name">

                </p>

                <!-- The following field is for robots only, invisible to humans: -->

<li class="robotic" id="pot">

  <label for="robotest">Middle Name</label>

  <input name="robotest" id="robotest" class="robotest" type="text" />

</li>

                <p class="half">

                  <label for="email">E-mail</label><input name="email" id="email">

                </p>

                <p>

                    <label for="subject">Topic</label><select name="subject" id="subject">

                        <option value="0">Choose</option>

                        <option value="1">YOU CUSTOMISE THESE</option>

                        <option value="2">YOU CUSTOMISE THESE</option>

                        <option value="3">YOU CUSTOMISE THESE</option>

                        <option value="4">YOU CUSTOMISE THESE</option>

                        <option value="4">YOU CUSTOMISE THESE</option>

                    </select>

                </p>

                <p>

                    <label for="message">Message</label><textarea name="message" id="message" rows="5" cols="20"></textarea>

                </p>

                <p><button name="send" type="submit" value="1">Send message</button></p>

            </form>

 

YOU'LL HAVE TO USE YOUR OWN CSS TO MAKE THE FORM LOOK THE WAY YOU WANT AND INCLUDE THIS BIT OF CODE IN THE CSS (THIS HIDES THE FORM FIELD FROM HUMANS TO FOIL SPAMBOTS)

 

}

.robotic { display:none; }

 

GOOD LUCK!

 



Viewing all articles
Browse latest Browse all 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>