simple-ajax-contact-form

Gesponsorde koppelingen

PHP script bestanden

  1. simple-ajax-contact-form

« Lees de omschrijving en reacties

contact.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple AJAX Contact Form | Paradox-Productions.Net</title>
<script type="text/javascript">
    var http = false;
    if(navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        http = new XMLHttpRequest();
    }
    function checkform(name, email, subject, content) {
        http.abort();
        http.open("GET", "contact.inc.php?name=" + name + "&email=" + email + "&subject=" + subject + "&content=" + content, true);
        http.onreadystatechange=function() {
            if(http.readyState == 4) {
                document.getElementById('ajaxresponse').innerHTML = http.responseText;
            }
            if(http.readyState == 1) {
                document.getElementById('ajaxresponse').innerHTML = '<img src="loading.gif" alt="Loading" />';
            }
        }
        http.send(null);
    }
</script>
<style type="text/css">
body, td {
    font-family: Lucida Grande, Lucida Sans Unicode, sans-serif;
    font-size: 12px;
    color: #444;
}
input, textarea {
    background: #EEE;
    border: 1px solid #DDD;
    padding: 3px;
    color: #666;
    font-size: 12px;
    font-family: Lucida Grande, Lucida Sans Unicode, sans-serif;
    width: 200px;
}
input.button {
    width: 100px;
    border: 1px solid #CCC;
    cursor: pointer;
}
input:hover, textarea:hover {
    border: 1px solid #ECFF9F;
    background: #FEFEFE;
}
input:focus, textarea:focus {
    border: 1px solid #C3DF53;
    background: #FEFEFE;
}
div#ajaxresponse {
    margin-bottom: 10px;
    min-height: 15px;
}
span#error {
    color: #CF5A5A;
}
span#success {
    color: #6D8F1A;
}
</style>
</head>
<body>

<h2>Simple AJAX Contact Form</h1>
<div id="ajaxresponse"></div>
<form action="" method="post">
    <table>
        <tr>
            <td>Name</td>
            <td><input type="text" name="name" id="name" /></td>
           </tr>
        <tr>
            <td>Email</td>
            <td><input type="text" name="email" id="email" /></td>
          </tr>
        <tr>
            <td>Subject</td>
            <td><input type="text" name="subject" id="subject" /></td>
          </tr>
        <tr>
            <td>Message</td>
            <td><textarea cols="" rows="" name="message" id="message"></textarea></td>
          </tr>
        <tr>
            <td><input type="reset" value="Reset Form" class="button" /></td>
            <td><input type="button" value="Send Message" class="button" onClick="checkform(document.getElementById('name').value, document.getElementById('email').value, document.getElementById('subject').value, document.getElementById('message').value)" /></td>
          </tr>
      </table>
</form>
</body>
</html>


contact.inc.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
    /* CONFIG
    ----------*/
    // WEBMASTERS EMAIL

    $target = "[email protected]";
    // WEBMASTERS NAME
    $targetname = "Wouter De Schuyter";
    // ENABLE SUBJECT PREFIX (true or false)
    $subjprefixe = true; // SET TO FALSE TO DISABLE PREFIX
    // SUBJECT PREFIX

    $subjprefix = "[Contact | Paradox-Productions.Net]";
    
    /* VARIABLES
    -------------*/

    $name = $_GET['name'];
    $email = $_GET['email'];
    $subject = $_GET['subject'];
    $content = $_GET['content'];
    
    /* CHECKING DETAILS
    --------------------*/

    if(empty($name) && empty($email) && empty($subject) && empty($content)) {
        echo '<span id="error">Please fill in all fields.</span>';
    }

    elseif(empty($name)) {
        echo '<span id="error">Please enter a name</span>';
    }

    elseif(empty($email)) {
        echo '<span id="error">Please enter an email</span>';
    }

    elseif(empty($subject)) {
        echo '<span id="error">Please enter a subject</span>';
    }

    elseif(empty($content)) {
        echo '<span id="error">Please enter message</span>';
    }

    elseif(!preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $email)) {
        echo '<span id="error">Please enter a valid email.</span>';
    }

    elseif(strlen($name) < 3) {
        echo '<span id="error">Your name can not be smaller than 3 characters.</span>';
    }

    elseif(strlen($email) < 9) {
        echo '<span id="error">Your email can not be smaller than 9 characters.</span>';
    }

    elseif(strlen($subject) < 3) {
        echo '<span id="error">Your subject can not be smaller than 3 characters.</span>';
    }

    elseif(strlen($content) < 20) {
        echo '<span id="error">Your message can not be smaller than 20 characters.</span>';
    }

    else {
        /* EMAIL
        ---------*/

        $headers  = 'MIME-Version: 1.0' . "rn";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
        $headers .= 'To: ' . $targetname . ' <' . $target . '>' . "rn";
        // TARGET EMAIL AS SENDER TO MAKE SURE IT'S NOT IN THE SPAM FOLDER
        $headers .= 'From: ' . $name . ' <' . $target . '>' . "rn";
        $headers .= 'Reply-To: ' . $name . ' <' . $email . '>' . "rn";
        if($subjprefixe == true) {
            $subject = $subjprefix . " " . $subject;
        }

        $message = '
        <html>
        <head>
            <title>'
. $subject . '</title>
            <style type="text/css">
                body, td {
                    font-family: Lucida Grande, Lucida Sans Unicode, sans-serif;
                    font-size: 12px;
                    color: #444;
                    background: #EEE;
                }
            </style>
        </head>
        <body>
            <p>
                <b>Sender:</b> '
. $name . ' <' . $email . '><br />
                <b>Subject:</b> '
. $subject . '<br />
                <b>Message:</b><br />'
. nl2br($content) . '
            </p>
            <p>--<br />Message sent at '
. date("d/m/Y H:i:s") . '</p>
        </body>
        </html>
        '
;
        if(mail($target, $subject, $message, $headers)) {
            echo '<span id="success">Message Sent</span>';
        }

        else {
            echo '<span id="error">Something went wrong while sending the email.</span>';
        }
    }

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.