I have managed to validate all the inputs and works very well. My only problem is with the selection part which i can't figure out how to validate. would appreciate any help from users good in PHP. Have been going through tutorials the whole day but can't seem to crack it. Would be nice to just add a few lines of code for the selection areas without starting all over for the rest of the form.
<?php
// Set email variables
$email_to = 'correspondent114@gmail.com';
$email_subject = 'Form submission';
// Set required fields
$required_fields = array('firstname', 'lastname', 'email', 'city', 'university', 'course');
// set error messages
$error_messages = array(
'firstname' => 'Please enter your first name to proceed.',
'lastname' => 'Please enter your last name to proceed.',
'email' => 'Please enter a valid Email Address to continue.',
'city' => 'Please enter your city to continue.',
'university' => 'Please enter your university/BS to continue.',
'course' => 'Please enter your major to continue.',
);
// Set form status
$form_complete = FALSE;
// configure validation array
$validation = array();
// check form submittal
if(!empty($_POST)) {
// Sanitise POST array
foreach($_POST as $key => $value) $_POST[$key] = remove_email_injection(trim($value));
// Loop into required fields and make sure they match our needs
foreach($required_fields as $field) {
// the field has been submitted?
if(!array_key_exists($field, $_POST)) array_push($validation, $field);
// check there is information in the field?
if($_POST[$field] == '') array_push($validation, $field);
// validate the email address supplied
if($field == 'email') if(!validate_email_address($_POST[$field])) array_push($validation, $field);
}
// basic validation result
if(count($validation) == 0) {
// Prepare our content string
$email_content = 'Register Form Submission: ' . "\n\n";
// simple email content
foreach($_POST as $key => $value) {
if($key != 'submit') $email_content .= $key . ': ' . $value . "\n";
}
// if validation passed ok then send the email
mail($email_to, $email_subject, $email_content);
// Update form switch
$form_complete = TRUE;
}
}
function validate_email_address($email = FALSE) {
return (preg_match('/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i', $email))? TRUE : FALSE;
}
function remove_email_injection($field = FALSE) {
return (str_ireplace(array("\r", "\n", "%0a", "%0d", "Content-Type:", "bcc:","to:","cc:"), '', $field));
}
?>
<!DOCTYPE html>
<html lang = "en-US">
<head><meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<title>Organisation somalischer Studierender in Deutschland - OSSD e.V.</title>
<link rel = "stylesheet" type = "text/css" href = "css/style.css" media = "all"/>
<meta name = "viewport" content = "width = device-width, initial-scale = "1.0"/>
<script type="text/javascript" src="http://ift.tt/1d7ZI3K"></script>
<script type = "text/javascript" src = "javascript/validation.js"></script>
<script type="text/javascript">
var nameError = '<?php echo $error_messages['firstname']; ?>';
var nameError = '<?php echo $error_messages['lastname']; ?>';
var emailError = '<?php echo $error_messages['email']; ?>';
var cityError = '<?php echo $error_messages['city']; ?>';
var universityError = '<?php echo $error_messages['university']; ?>';
var courseError = '<?php echo $error_messages['course']; ?>';
</script>
</head>
<body>
<a href = "http://www.os-sd.eu/en"><img src = "images/en.gif" class = "en-lang"/></a>
<a href = "http://www.os-sd.eu/de"><img src = "images/de.gif" class = "de-lang"/></a>
<!--<a href = "http://www.os-sd.eu/de"><img src = "images/de.gif" class = "de-page"/></a>
<a href = "http://www.os-sd.eu/en"><img src = "images/en.gif" class = "en-page"/></a>-->
<div class = "main-wrapper"><!--mainContent div-->
<!--beginning of top header wrapper-->
<header role = "banner" class = "header-wrapper">
<div class = "headerMediaWrapper"><!--beginning of headerMediaWrapper-->
<a href = "http://www.os-sd.eu"><img src="images/logo.png" class="logo"/></a>
<a href = "http://ift.tt/g8FRpY" target ="_blank"><img src ="images/facebook.png" class ="social"/></a>
<a href = "http://ift.tt/1Hb5fD3" target = "_blank"><img src="images/googleplus.png" class="social" /></a>
<a href = "http://ift.tt/1OiFtyL" target = "_blank"><img src ="images/twitter.png" class ="social" href = "https://www.twitter.com"/></a>
</div><!--end of headerMediaWrapper-->
<div class = "navContainer"><!--navContainer -->
<div class ="motto"><!--motto-->
<h2></h2>
</div><!--end of motto-->
<nav class = "top-nav" role = "navigation" ><!--beginning of top navigation-->
<ul>
<li><a href = "index.html" class = "current">Home</a></li>
<li><a href = "events.html">Events</a></li>
<li><a href = "board.html">Board</a></li>
<li><a href = "projects.html">Projects</a></li>
<li><a href = "register.php">Register</a></li>
<li><a href = "contact.php">Contact Us</a></li>
</ul>
</nav> <!--End of top navigation-->
</div><!--end of navContainer-->
</header><!--End of header wrapper-->
<div class = "mainContent"><!--mainContent-->
<h2>Register Form</h2>
<form class = "form" action = "" method = "post">
<?php if($form_complete === FALSE): ?>
<p class = "firstname">
<input type = "text" name="firstname" id = "firstname" placeholder = "John" autofocus value = "<?php echo isset($_POST['firstname'])? $_POST['firstname'] : ''; ?>"/>
<label for="name">First Name</label>
<?php if(in_array('firstname', $validation)): ?><div class = "row"><span class="error">
<?php echo $error_messages['firstname']; ?></span></div><?php endif; ?>
</p>
<p class = "lastname">
<input type = "text" name="lastname" id = "lastname" placeholder = "Doe" value = "<?php echo isset($_POST['lastname'])? $_POST['lastname'] : ''; ?>"/>
<label for="name">Last Name</label>
<?php if(in_array('lastname', $validation)): ?><div class = "row"><span class="error">
<?php echo $error_messages['lastname']; ?></span></div><?php endif; ?>
</p>
<p class = "email">
<input type = "text" name="email" id = "email" placeholder = "mail@example.com" value = "<?php echo isset($_POST['email'])? $_POST['email'] : ''; ?>"/>
<label for="email">Email</label>
<?php if(in_array('email', $validation)): ?><div class = "row"><span class="error">
<?php echo $error_messages['email']; ?></span></div><?php endif; ?>
</p>
<p class = "state">
<select id = "state" name = "state" value = "<?php echo isset($_POST['state'])? $_POST['state'] : ''; ?>">
<option value="" selected ="selected">Select... </option>
<option value = "S">Baden-Wuerttemberg</option>
<option value = "M">Bayern</option>
<option value = "B">Berlin</option>
<option value = "P">Brandenburg</option>
<option value = "HB">Bremen</option>
<option value = "HH">Hamburg</option>
<option value = "Wi">Hessen</option>
<option value = "SN" >Mecklenburg-Vorpommern</option>
<option value = "H">Niedersachsen</option>
<option value = "D">Nordrhein-Westfalen</option>
<option value = "MZ">Rheinland-Pflalz</option>
<option value = "SB">Saarland</option>
<option value = "DD">Sachsen</option>
<option value = "MD">Sachsen-Anhalt</option>
<option value = "KI">Schleswig-Holstein</option>
<option value = "EF">Thueringen</option>
</select>
<label for = "state">Bundesland</label>
</p>
<p class = "city">
<input type = "text" name="city" id = "city" placeholder = "Rosenheim" value = "<?php echo isset($_POST['city'])? $_POST['city'] : ''; ?>"/>
<label for="city">City</label>
<?php if(in_array('city', $validation)): ?><div class = "row"><span class="error">
<?php echo $error_messages['city']; ?></span></div><?php endif; ?>
</p>
<p class = "status">
<select id = "status" name = "status" value = "">
<option value="" selected = "selected">Select...</option>
<option value = "S" >Student</option>
<option value = "M">Azubi</option>
<option value = "B">Graduate</option>
</select>
<label for = "status">I am..</label>
</p>
<p class = "university">
<input type = "text" name="university" id = "university" placeholder = "Alma Mater" value = "<?php echo isset($_POST['university'])? $_POST['university'] : ''; ?>"/>
<label for="university">Uni/Berufsschule</label>
<?php if(in_array('university', $validation)): ?><div class = "row"><span class="error">
<?php echo $error_messages['university']; ?></span></div><?php endif; ?>
</p>
<p class = "course">
<input type = "text" name="course" id = "course" placeholder = "Elektrotechnik" value = "<?php echo isset($_POST['course'])? $_POST['course'] : ''; ?>"/>
<label for="course">Major/Ausbildung</label>
<?php if(in_array('course', $validation)): ?><div class = "row"><span class="error">
<?php echo $error_messages['course']; ?></span></div><?php endif; ?>
</p>
<p class = "submit">
<input type = "submit" value = "send">
</p>
</form>
<?php else: ?>
<p style = "font-size: 140%; color:#FFCE00; margin-left:1%; min-height:500px;">Thank you for registering!</p>
<script type = "text/javascript">
setTimeout('ourRedirect()',7000)
function ourRedirect(){
location.href= 'contact.php'
}
</script>
<?php endif; ?>
</div><!--end of mainContent-->
<aside class = "top-sidebar">
<h2>Contact Us</h2>
<em style="color:#FFCE00;"><p>info@os-sd.eu</em></p><p>OSSD e.V.</p><p>Postfach 13 02 09, 65090 Wiesbaden</p>
</aside>
<aside class = "middle-sidebar">
<h2>Cancel Membership</h2>
<p>Cancelling membership is as easy as registering to be a member. simply drop us an e-mail at <em style = "color:#FFCE00;">info@os-sd.eu</em> and we will
confirm the membership cancellation within 7 working days. We take the private information of our members very seriously
and that is why upon membership cancellation all the information that we have collected from you during the registration
process will be permamently erased from our database.</p>
</aside>
<div class = "footer">
<div class = "left-footer">
<ul>
<li><a href = "#">Legal Notice</a></li>
<li><a href = "#">Private Policy</a></li>
<li><a href = "#">Disclaimer</a></li>
</ul>
</div>
<div class = "right-footer">
<ul>
<li><a href = "http://ift.tt/1Hb5hLa" target ="_blank">Facebook</a></li>
<li><a href = "http://ift.tt/1OiFuCS" target ="_blank">Twitter</a></li>
<li><a href = "http://ift.tt/1Hb5hLf"target ="_blank">Google+</a></li>
</ul>
</div>
<div class = "bottom-footer">
<p>Copyright © 2014-2015 <a href = "htt
</div><!--end of mainContent-->
</div><!--End of main page wrapper-->
</body>
</html>
Aucun commentaire:
Enregistrer un commentaire