smeijer3
%Europe/Berlin %376 %2008, 10:02
Ik kan basis HTML en CSS, maar daar houdt het een beetje op. Nou had ik een werkend contactformulier gevonden met verplichte velden, maar als ik gewoon op verzenden duw zonder iets in te vullen, wordt het formulier ook verzonden. Iemand die weet hoe ik die verplichte velden dus echt verplicht moet maken?
Dit is de code:
<?php
/****SET THE MAX CHARS FOR EACH MESSAGE***************/
//it is recommended not to set the max too high, to prevent extremely long messages
// from stalling your server
$EMAIL_MAX = 2500;
$SMS_MAX = 120;
/************************************************** ***/
//function for stripping whitespace and some chars
function cleanUp($str_to_clean, $newlines, $spaces){
//build list of whitespace chars to be removed
$bad_chars = array('\r', '\t', ';');
//if newlines are false, add that to the list of bad chars
if(!$newlines){array_push($bad_chars, '\n');}
//if spaces are false, strip them too
if(!$spaces){array_push($bad_chars, ' ');}
$str_to_clean_a = str_replace($bad_chars, '', $str_to_clean);
$str_to_clean_b = strip_tags($str_to_clean_a);
return $str_to_clean_b;
}
//function to check for valid email address pattern
function checkEmail($email) {
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {return false;}
return true;
}
//function to check for valid url pattern
function checkURL($url) {
if(!eregi("^http:\/\/", $url)) {return false;}
return true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contact</title>
<style type="text/css">
/* BASIC STYLES */
body{font-family: 'helvetica',Trebuchet, Tahoma, sans-serif;color:#222;font-size:11px;}
fieldset{margin:0;padding:0;border:0;}
label{width:250px; display:block;}
.txt_input{width:250px; display:block;}
textarea{height:80px; width:250px;}
input{display:block;}
.req{color:#666666;font-size:100%;}
#form_errors{color:#333333; display:none;}
#form_thanks{color:#333333; display:none;}
</style>
<script type="text/javascript">
v_fields = new Array('sender_naam','sender_e-mail','sender_subject','sender_message');alert_on = true;thanks_on = true; thanks_message = "Uw bericht is verzonden. Wij zullen u zo spoedig mogelijk antwoorden.";
function validateForm(){
//alert(v_fields);
//init errors
var err = "";
//start checking fields
for(i=0;i<v_fields.length;i++){
//store the field value
var _thisfield = eval("document.contact."+v_fields[i]+".value");
//check the field value
if(v_fields[i] == "sender_naam"){
if(!isAlpha(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_onderwerp"){
if(!isAlpha(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_email"){
if(!isEmail(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_url"){
if(!isURL(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_telefoon"){
if(!isPhone(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_bericht"){
if(!isText(_thisfield)){ err += "Vul de verplichte velden in\n";}
}
}//end for
if(err != ""){
if(alert_on){
alert("The following errors have occurred\n"+err);
}else{
showErrors(err);
}
return false;
}
return true;
}
//function to show errors in HTML
function showErrors(str){
var err = str.replace(/\n/g,"<br />");
document.getElementById("form_errors").innerHTML = err;
document.getElementById("form_errors").style.display = "block";
}
//function to show thank you message in HTML
function showThanks(str){
var tym = str.replace(/\n/g,"<br />");
document.getElementById("form_thanks").innerHTML = tym;
document.getElementById("form_thanks").style.display = "block";
}
function isemail(str){
if(str == "") return false;
var regex = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
return regex.test(str);
}
function isText(str){
if(str == "") return false;
return true;
}
function isURL(str){
var regex = /[a-zA-Z0-9\.\/:]+/
return regex.test(str);
}
// returns true if the number is formatted in the following ways:
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhone(str){
var regex = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
return regex.test(str);
}
// returns true if the string contains A-Z, a-z or 0-9 or . or # only
function isAddress(str){
var regex = /[^a-zA-Z0-9\#\.]/g
if (regex.test(str)) return true;
return false;
}
// returns true if the string is 5 digits
function isZip(str){
var regex = /\d{5,}/;
if(regex.test(str)) return true;
return false;
}
// returns true if the string contains A-Z or a-z only
function isAlpha(str){
var regex = /[a-zA-Z]/g
if (regex.test(str)) return true;
return false;
}
// returns true if the string contains A-Z or a-z or 0-9 only
function isAlphaNumeric(str){
var regex = /[^a-zA-Z0-9]/g
if (regex.test(str)) return false;
return true;
}
</script>
<?php
if(isset($_POST["submitForm"])){
$_name = cleanUp($_POST["sender_naam"], false, true);
$_email = cleanUp($_POST["sender_email"], false, false);
$_subject = cleanUp($_POST["sender_onderwerp"], false, true);
$_message = cleanUp($_POST["sender_bericht"], true, true);
$_body = "Dit bericht is verstuurd via de contactpagina op www.blabla.nl\n\n";
if($_name){
$_body .= "Naam: $_naam\n\n";
}
if($_email){
$_body .= "E-mail: $_email\n\n";
}
if($_url){
$_body .= "Url: $_url\n\n";
}
if($_phone){
$_body .= "Telnr.: $_telefoon\n\n";
}
if($_message){
//check length of body, reduce to max chars
if(strlen($_message) > $EMAIL_MAX){$_message= substr($_message, 0, $EMAIL_MAX);}else{$_message = $_message;}
if(strlen($_message) > $SMS_MAX){$_message2 = substr($_message, 0, $SMS_MAX);}else{$_message2 = $_message;}
}
//store the recipient(s)
$_to = array();
//now get the recipient(s)
$_to[] = "sabrina.meijer@gmail.com, xxsabjexx@hotmail.com";
//define the subject
if(!$_subject){$_subject = "Bericht www.blabla.nl";}
if(!$_name){$_name = "CONTACT";}
if(!$_email){$_email = $_name;}
//set the headers
$_header = "From: $_naam < $_email >" . "\r\n" .
"Reply-To: ".$_email."\r\n" .
"Super-Simple-Mailer: supersimple.org";
//we can send up to 2 emails (EMAIL and/or SMS)
if(count($_to) > 2){ $_to = array_slice($_to,0,2);}
for($i=0;$i<count($_to);$i++){
//get the correct message, based on where it is delivering to
if(strstr($_to[$i],"teleflip.com")){$_text = $_body.$_message2;}else{$_text = $_body.$_message;}
//send the email(s)
mail($_to[$i], $_subject, $_text, $_header);
}
echo "<script type=\"text/javascript\">window.onload = function(){showThanks(thanks_message);}</script>";
}
?>
</head>
<body>
<div id="container">
<div class="contact">
<fieldset style="float:left;">
<p id="form_errors"></p>
<p id="form_thanks"></p>
<form name="contact" action="<?=$_SERVER['PHP_SELF']?>" method="post" onSubmit="return validateForm();">
<label>Naam <span class="req"><font color="ff6666">*</font></span></label>
<input type="text" class="txt_input" name="sender_naam" />
<label>E-mail <span class="req"><font color="ff6666">*</font></span></label>
<input type="text" class="txt_input" name="sender_email" />
<label>Onderwerp <span class="req"><font color="ff6666">*</font></span></label>
<input type="text" class="txt_input" name="sender_onderwerp" />
<label>Bericht <span class="req"><font color="ff6666">*</font></span></label>
<textarea name="sender_message"></textarea>
<input type="submit" name="submitForm" value="Verzend" />
</form>
</fieldset>
</div>
Dit is de code:
<?php
/****SET THE MAX CHARS FOR EACH MESSAGE***************/
//it is recommended not to set the max too high, to prevent extremely long messages
// from stalling your server
$EMAIL_MAX = 2500;
$SMS_MAX = 120;
/************************************************** ***/
//function for stripping whitespace and some chars
function cleanUp($str_to_clean, $newlines, $spaces){
//build list of whitespace chars to be removed
$bad_chars = array('\r', '\t', ';');
//if newlines are false, add that to the list of bad chars
if(!$newlines){array_push($bad_chars, '\n');}
//if spaces are false, strip them too
if(!$spaces){array_push($bad_chars, ' ');}
$str_to_clean_a = str_replace($bad_chars, '', $str_to_clean);
$str_to_clean_b = strip_tags($str_to_clean_a);
return $str_to_clean_b;
}
//function to check for valid email address pattern
function checkEmail($email) {
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {return false;}
return true;
}
//function to check for valid url pattern
function checkURL($url) {
if(!eregi("^http:\/\/", $url)) {return false;}
return true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contact</title>
<style type="text/css">
/* BASIC STYLES */
body{font-family: 'helvetica',Trebuchet, Tahoma, sans-serif;color:#222;font-size:11px;}
fieldset{margin:0;padding:0;border:0;}
label{width:250px; display:block;}
.txt_input{width:250px; display:block;}
textarea{height:80px; width:250px;}
input{display:block;}
.req{color:#666666;font-size:100%;}
#form_errors{color:#333333; display:none;}
#form_thanks{color:#333333; display:none;}
</style>
<script type="text/javascript">
v_fields = new Array('sender_naam','sender_e-mail','sender_subject','sender_message');alert_on = true;thanks_on = true; thanks_message = "Uw bericht is verzonden. Wij zullen u zo spoedig mogelijk antwoorden.";
function validateForm(){
//alert(v_fields);
//init errors
var err = "";
//start checking fields
for(i=0;i<v_fields.length;i++){
//store the field value
var _thisfield = eval("document.contact."+v_fields[i]+".value");
//check the field value
if(v_fields[i] == "sender_naam"){
if(!isAlpha(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_onderwerp"){
if(!isAlpha(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_email"){
if(!isEmail(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_url"){
if(!isURL(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_telefoon"){
if(!isPhone(_thisfield)){ err += "Vul de verplichte velden in\n";}
}else if(v_fields[i] == "sender_bericht"){
if(!isText(_thisfield)){ err += "Vul de verplichte velden in\n";}
}
}//end for
if(err != ""){
if(alert_on){
alert("The following errors have occurred\n"+err);
}else{
showErrors(err);
}
return false;
}
return true;
}
//function to show errors in HTML
function showErrors(str){
var err = str.replace(/\n/g,"<br />");
document.getElementById("form_errors").innerHTML = err;
document.getElementById("form_errors").style.display = "block";
}
//function to show thank you message in HTML
function showThanks(str){
var tym = str.replace(/\n/g,"<br />");
document.getElementById("form_thanks").innerHTML = tym;
document.getElementById("form_thanks").style.display = "block";
}
function isemail(str){
if(str == "") return false;
var regex = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
return regex.test(str);
}
function isText(str){
if(str == "") return false;
return true;
}
function isURL(str){
var regex = /[a-zA-Z0-9\.\/:]+/
return regex.test(str);
}
// returns true if the number is formatted in the following ways:
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhone(str){
var regex = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
return regex.test(str);
}
// returns true if the string contains A-Z, a-z or 0-9 or . or # only
function isAddress(str){
var regex = /[^a-zA-Z0-9\#\.]/g
if (regex.test(str)) return true;
return false;
}
// returns true if the string is 5 digits
function isZip(str){
var regex = /\d{5,}/;
if(regex.test(str)) return true;
return false;
}
// returns true if the string contains A-Z or a-z only
function isAlpha(str){
var regex = /[a-zA-Z]/g
if (regex.test(str)) return true;
return false;
}
// returns true if the string contains A-Z or a-z or 0-9 only
function isAlphaNumeric(str){
var regex = /[^a-zA-Z0-9]/g
if (regex.test(str)) return false;
return true;
}
</script>
<?php
if(isset($_POST["submitForm"])){
$_name = cleanUp($_POST["sender_naam"], false, true);
$_email = cleanUp($_POST["sender_email"], false, false);
$_subject = cleanUp($_POST["sender_onderwerp"], false, true);
$_message = cleanUp($_POST["sender_bericht"], true, true);
$_body = "Dit bericht is verstuurd via de contactpagina op www.blabla.nl\n\n";
if($_name){
$_body .= "Naam: $_naam\n\n";
}
if($_email){
$_body .= "E-mail: $_email\n\n";
}
if($_url){
$_body .= "Url: $_url\n\n";
}
if($_phone){
$_body .= "Telnr.: $_telefoon\n\n";
}
if($_message){
//check length of body, reduce to max chars
if(strlen($_message) > $EMAIL_MAX){$_message= substr($_message, 0, $EMAIL_MAX);}else{$_message = $_message;}
if(strlen($_message) > $SMS_MAX){$_message2 = substr($_message, 0, $SMS_MAX);}else{$_message2 = $_message;}
}
//store the recipient(s)
$_to = array();
//now get the recipient(s)
$_to[] = "sabrina.meijer@gmail.com, xxsabjexx@hotmail.com";
//define the subject
if(!$_subject){$_subject = "Bericht www.blabla.nl";}
if(!$_name){$_name = "CONTACT";}
if(!$_email){$_email = $_name;}
//set the headers
$_header = "From: $_naam < $_email >" . "\r\n" .
"Reply-To: ".$_email."\r\n" .
"Super-Simple-Mailer: supersimple.org";
//we can send up to 2 emails (EMAIL and/or SMS)
if(count($_to) > 2){ $_to = array_slice($_to,0,2);}
for($i=0;$i<count($_to);$i++){
//get the correct message, based on where it is delivering to
if(strstr($_to[$i],"teleflip.com")){$_text = $_body.$_message2;}else{$_text = $_body.$_message;}
//send the email(s)
mail($_to[$i], $_subject, $_text, $_header);
}
echo "<script type=\"text/javascript\">window.onload = function(){showThanks(thanks_message);}</script>";
}
?>
</head>
<body>
<div id="container">
<div class="contact">
<fieldset style="float:left;">
<p id="form_errors"></p>
<p id="form_thanks"></p>
<form name="contact" action="<?=$_SERVER['PHP_SELF']?>" method="post" onSubmit="return validateForm();">
<label>Naam <span class="req"><font color="ff6666">*</font></span></label>
<input type="text" class="txt_input" name="sender_naam" />
<label>E-mail <span class="req"><font color="ff6666">*</font></span></label>
<input type="text" class="txt_input" name="sender_email" />
<label>Onderwerp <span class="req"><font color="ff6666">*</font></span></label>
<input type="text" class="txt_input" name="sender_onderwerp" />
<label>Bericht <span class="req"><font color="ff6666">*</font></span></label>
<textarea name="sender_message"></textarea>
<input type="submit" name="submitForm" value="Verzend" />
</form>
</fieldset>
</div>