Het onderstaande contactform werkt fantastisch, en werkt zelfs met een spam protector. Ik heb al van alles in het script aangepast, dat ging goed.
Het lukt me alleen niet om een field NIET required te maken. standaard zijn alle velden dat wel. het lijkt me een simpele handeling maar het lukt me niet.
iemand ideeën?
Alvast bedankt!
Het lukt me alleen niet om een field NIET required te maken. standaard zijn alle velden dat wel. het lijkt me een simpele handeling maar het lukt me niet.
iemand ideeën?
PHP:
<?php
session_start();
/* Email address where the messages should be delivered */
$to = '######@######.nl';
/* From email address, in case your server prohibits sending emails from addresses other than those of your
own domain (e.g. email@yourdomain.com). If this is used then all email messages from your contact form will appear
from this address instead of actual sender. */
$from = '';
/* This will be appended to the subject of contact form message */
$subject_prefix = 'Vraag / Informatie';
/* Form width in px or % value */
$form_width = '70%';
/* Form background color */
$form_background = '#FFFFFF';
/* Form border color */
$form_border_color = '#CCCCCC';
/* Form border width */
$form_border_width = '1px';
/* Form border style. Examples - dotted, dashed, solid, double */
$form_border_style = 'solid';
/* Form cell padding */
$cell_padding = '5px';
/* Form left column width */
$left_col_width = '25%';
/* Form font size */
$font_size = '12px';
/* Empty/Invalid fields will be highlighted in this color */
$field_error_color = '#FF0000';
/* Thank you message to be displayed after the form is submitted. Can include HTML tags. Write your message
between <!-- Start message --> and <!-- End message --> */
$thank_you_message = <<<EOD
<!-- Start message -->
<center>
<p>Bedankt voor het sturen van het bericht. <br>
We zullen ons best doen u zo snel mogelijk te beantwoorden.<br /><br />Met vriendelijke groet,<br /><br />Centrum voor Luistertherapie Eindhoven</p><br /></center><br /><br /><br /><br /><br /><br /><br />
<!-- End message -->
EOD;
/* URL to be redirected to after the form is submitted. If this is specified, then the above message will
not be shown and user will be redirected to this page after the form is submitted */
/* Example: $thank_you_url = 'http://www.yourwebsite.com/thank_you.html'; */
$thank_you_url = '';
/*******************************************************************************
* Do not change anything below, unless of course you know very well
* what you are doing :)
*******************************************************************************/
$voornaam = array('Voornaam*','voornaam',NULL,NULL);
$achternaam = array('Achternaam*','achternaam',NULL,NULL);
$geslacht = array('Geslacht','geslacht',NULL,NULL);
$adres = array('Adres','adres',NULL,NULL);
$postcode = array('Postcode','postcode',NULL,NULL);
$woonplaats = array('Woonplaats','woonplaats',NULL,NULL);
$telefoonnummer = array('Telefoonnummer*','telefoonnummer',NULL,NULL);
$email = array('Emailadres*','email',NULL,NULL,NULL);
$subject = array('Onderwerp*','subject',NULL,NULL);
$message = array('Bericht','message',NULL,NULL);
$code = array('Code','captcha_code',NULL,NULL,NULL);
$error_message = '';
if (!isset($_POST['submit'])) {
showForm();
} else { //form submitted
$error = 0;
if(!empty($_POST['voornaam'])) {
$voornaam[2] = clean_var($_POST['voornaam']);
if (function_exists('htmlspecialchars')) $voornaam[2] = htmlspecialchars($voornaam[2], ENT_QUOTES);
}
else {
$error = 1;
$voornaam[3] = 'color:#FF0000;';
}
if(!empty($_POST['achternaam'])) {
$achternaam[2] = clean_var($_POST['achternaam']);
if (function_exists('htmlspecialchars')) $achternaam[2] = htmlspecialchars($achternaam[2], ENT_QUOTES);
}
else {
$error = 1;
$achternaam[3] = 'color:#FF0000;';
}
if(!empty($_POST['geslacht'])) {
$geslacht[2] = clean_var($_POST['geslacht']);
if (function_exists('htmlspecialchars')) $geslacht[2] = htmlspecialchars($geslacht[2], ENT_QUOTES);
}
else {
$error = 1;
$geslacht[3] = 'color:#FF0000;';
}
if(!empty($_POST['adres'])) {
$adres[2] = clean_var($_POST['adres']);
if (function_exists('htmlspecialchars')) $adres[2] = htmlspecialchars($adres[2], ENT_QUOTES);
}
else {
$error = 1;
$adres[3] = 'color:#FF0000;';
}
if(!empty($_POST['postcode'])) {
$postcode[2] = clean_var($_POST['postcode']);
if (function_exists('htmlspecialchars')) $postcode[2] = htmlspecialchars($postcode[2], ENT_QUOTES);
}
else {
$error = 1;
$postcode[3] = 'color:#FF0000;';
}
if(!empty($_POST['woonplaats'])) {
$woonplaats[2] = clean_var($_POST['woonplaats']);
if (function_exists('htmlspecialchars')) $woonplaats[2] = htmlspecialchars($woonplaats[2], ENT_QUOTES);
}
else {
$error = 1;
$woonplaats[3] = 'color:#FF0000;';
}
if(!empty($_POST['telefoonnummer'])) {
$telefoonnummer[2] = clean_var($_POST['telefoonnummer']);
if (function_exists('htmlspecialchars')) $telefoonnummer[2] = htmlspecialchars($telefoonnummer[2], ENT_QUOTES);
}
else {
$error = 1;
$telefoonnummer[3] = 'color:#FF0000;';
}
if(!empty($_POST['email'])) {
$email[2] = clean_var($_POST['email']);
if (!validEmail($email[2])) {
$error = 1;
$email[3] = 'color:#FF0000;';
$email[4] = '<strong><span style="color:#FF0000;">Ongeldig e-mailadres</span></strong>';
}
}
else {
$error = 1;
$email[3] = 'color:#FF0000;';
}
if(!empty($_POST['subject'])) {
$subject[2] = clean_var($_POST['subject']);
if (function_exists('htmlspecialchars')) $subject[2] = htmlspecialchars($subject[2], ENT_QUOTES);
}
else {
$error = 1;
$subject[3] = 'color:#FF0000;';
}
if(!empty($_POST['message'])) {
$message[2] = clean_var($_POST['message']);
if (function_exists('htmlspecialchars')) $message[2] = htmlspecialchars($message[2], ENT_QUOTES);
}
else {
$error = 1;
$message[3] = 'color:#FF0000;';
}
if(empty($_POST['captcha_code'])) {
$error = 1;
$code[3] = 'color:#FF0000;';
} else {
include_once "securimage.php";
$securimage = new Securimage();
$valid = $securimage->check($_POST['captcha_code']);
if(!$valid) {
$error = 1;
$code[3] = 'color:#FF0000;';
$code[4] = '<strong><span style="color:#FF0000;">Code verkeerd ingevoerd!</span></strong>';
}
}
if ($error == 1) {
$error_message = '<span style="font-weight:bold;font-size:90%;">Vul of verbeter de velden die in het rood zijn weergegeven!</span>';
showForm();
} else {
if (function_exists('htmlspecialchars_decode')) $voornaam[2] = htmlspecialchars_decode($voornaam[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $achternaam[2] = htmlspecialchars_decode($achternaam[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $geslacht[2] = htmlspecialchars_decode($geslacht[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $adres[2] = htmlspecialchars_decode($adres[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $postcode[2] = htmlspecialchars_decode($postcode[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $woonplaats[2] = htmlspecialchars_decode($woonplaats[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $telefoonnummer[2] = htmlspecialchars_decode($telefoonnummer[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $subject[2] = htmlspecialchars_decode($subject[2], ENT_QUOTES);
if (function_exists('htmlspecialchars_decode')) $message[2] = htmlspecialchars_decode($message[2], ENT_QUOTES);
$message = "$voornaam[0]: $voornaam[2]\r\n$achternaam[0]: $achternaam[2]\r\n$geslacht[0]: $geslacht[2]\r\n$adres[0]: $adres[2]\r\n$postcode[0]: $postcode[2]\r\n$woonplaats[0]: $woonplaats[2]\r\n$telefoonnummer[0]: $telefoonnummer[2]\r\n$email[0]: $email[2]\r\n\r\n$message[0]:\r\n$message[2]\r\n";
if (!$from) $from_value = $email[2];
else $from_value = $from;
$headers = "From: $from_value" . "\r\n";
$headers .= "Reply-To: $email[2]" . "\r\n";
mail($to,"$subject_prefix - $subject[2]", $message, $headers);
if (!$thank_you_url) {
include $header_file;
echo $GLOBALS['thank_you_message'];
echo "\n";
include $footer_file;
}
else {
header("Location: $thank_you_url");
}
}
} //else submitted
function showForm()
{
global $voornaam, $achternaam, $geslacht, $adres, $postcode, $woonplaats, $telefoonnummer, $email, $subject, $message, $code, $form_width, $form_background, $form_border_color, $form_border_width, $form_border_style, $cell_padding, $left_col_width, $font_size;
?>
<div style="position:absolute; top: 640px; left: 30px"><span style="color:#F00"><? echo $GLOBALS['error_message']; ?></span></div>
<?
echo <<<EOD
<form method="post" class="cForm">
<table style="width:{$form_width}; background-color:{$form_background}; border:{$form_border_width} {$form_border_style} {$form_border_color}; padding:10px; font-size:{$font_size};" class="contactForm">
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding}; font-size:90%; font-weight:bold;">Alle velden gemarkeerd met een * zijn verplicht.</td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$voornaam[3]}">{$voornaam[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$voornaam[1]}" value="{$voornaam[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$achternaam[3]}">{$achternaam[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$achternaam[1]}" value="{$achternaam[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$geslacht[3]}">{$geslacht[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><select name="{$geslacht[1]}" value="{$geslacht[2]}"><option value="Kies geslacht">Kies geslacht</option><option value="Man">Man </option><option value="Vrouw">Vrouw </option></select>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$adres[3]}">{$adres[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$adres[1]}" value="{$adres[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$postcode[3]}">{$postcode[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$postcode[1]}" value="{$postcode[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$woonplaats[3]}">{$woonplaats[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$woonplaats[1]}" value="{$woonplaats[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$telefoonnummer[3]}">{$telefoonnummer[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$telefoonnummer[1]}" value="{$telefoonnummer[2]}" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$email[3]}">{$email[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$email[1]}" value="{$email[2]}" /> {$email[4]}</td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$subject[3]}">{$subject[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$subject[1]}" value="{$subject[2]}" size="40" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$message[3]}">{$message[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><textarea name="{$message[1]}" cols="40" rows="6">{$message[2]}</textarea></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding};"> </td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><img id="captcha" src="securimage_show.php" alt="CAPTCHA Image" /></td>
</tr>
<tr>
<td style="width:{$left_col_width}; text-align:left; vertical-align:top; padding:{$cell_padding}; font-weight:bold; {$code[3]}">{$code[0]}</td>
<td style="text-align:left; vertical-align:top; padding:{$cell_padding};"><input type="text" name="{$code[1]}" size="10" maxlength="5" /> {$code[4]}
<br /><br />(Vul aub de tekst uit het plaatje hierboven in. De tekst is niet hoofdletter gevoelig.)<br />
<a href="#" onclick="document.getElementById('captcha').src = 'securimage_show.php?' + Math.random(); return false">Klik hier als u de tekst niet goed kan lezen.</a>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:left; vertical-align:middle; padding:{$cell_padding};"><br /><br /><input type="submit" name="submit" value="Verzenden" style="border:1px solid #999;background:#E4E4E4;margin-top:5px;" /></td>
</tr>
</table>
</form>
EOD;
}
function clean_var($variable) {
$variable = strip_tags(stripslashes(trim(rtrim($variable))));
return $variable;
}
/**
Email validation function. Thanks to http://www.linuxjournal.com/article/9585
*/
function validEmail($email)
{
$isValid = true;
$atIndex = strrpos($email, "@");
if (is_bool($atIndex) && !$atIndex)
{
$isValid = false;
}
else
{
$domain = substr($email, $atIndex+1);
$local = substr($email, 0, $atIndex);
$localLen = strlen($local);
$domainLen = strlen($domain);
if ($localLen < 1 || $localLen > 64)
{
// local part length exceeded
$isValid = false;
}
else if ($domainLen < 1 || $domainLen > 255)
{
// domain part length exceeded
$isValid = false;
}
else if ($local[0] == '.' || $local[$localLen-1] == '.')
{
// local part starts or ends with '.'
$isValid = false;
}
else if (preg_match('/\\.\\./', $local))
{
// local part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
{
// character not valid in domain part
$isValid = false;
}
else if (preg_match('/\\.\\./', $domain))
{
// domain part has two consecutive dots
$isValid = false;
}
else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
{
// character not valid in local part unless
// local part is quoted
if (!preg_match('/^"(\\\\"|[^"])+"$/',
str_replace("\\\\","",$local)))
{
$isValid = false;
}
}
if ($isValid && function_exists('checkdnsrr'))
{
if (!(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))) {
// domain not found in DNS
$isValid = false;
}
}
}
return $isValid;
}
?>
Alvast bedankt!