I am new to the world of coding, PHP as well as XHTML. As my first stab at object oriented coding, I put together a basic registration form that is meant to serve as a means to collect user details.
What this form does not do is the following;
- Capture information is a file or database as I have not yet gained enough experience about databases, sessions, cookies or files.
- I looked at using onchange to populate the days dropdown menu dynamically but despise the form reloading and hence avoided doing so. I am aware I can use AJAX however considering my lack of knowledge, I was hoping someone could advise other alternatives apart from Javascript.
- Sanitize the data using htmlentities or htmlspecialchars.
What I would like
- I would appreciate any constructive criticisms, what I can improve on, what I could have done better
XHTML Code
<?php
require_once('includes/base.php');
require_once('functions.php');
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="" />
<meta name="description" content= "" />
<meta name="author" content="" />
<title>Event Details</title>
<link rel="stylesheet" type="text/css" href="web.css" />
</head>
<body>
<h1>Details</h1>
<p>Please provide us with your details so that we can confirm your booking</p>
<div>
<?php
$eventform = new eventform();
$eventform -> validateform();
?>
<fieldset>
<legend>Your details</legend>
<form name="eventdetails" method="post" action="event.php">
<label for="fname">First Name</label>
<input type="text" name="fname" value="<?php echo isset($_POST['fname']) ? $_POST['fname'] : ''; ?>" tabindex="1" id="fname" />
<label for="lname">Last Name</label>
<input type="text" name="lname" value="<?php echo isset($_POST['lname']) ? $_POST['lname'] : ''; ?>" tabindex="2" id="lname" />
<label for="email">Email</label>
<input type="text" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>" tabindex="3" id="email" />
<label for="cuisine">Cuisine</label>
<?php
$eventform = new eventform();
$eventform -> radiobuttons('cuisine');
?>
<label for="eventperiod">Please select an event day</label>
<?php
$eventform = new eventform();
?>
<select name="eventmonth" tabindex="8" id="eventmonth">
<?php
echo $eventform -> dropdown('months');
?>
</select>
<select name="eventday" tabindex="7" id="eventday">
<?php
echo $eventform -> dropdown('days');
?>
</select>
<select name="eventyear" tabindex="9" id="eventyear">
<?php
echo $eventform -> dropdown('years');
?>
</select>
<label>
<input type="submit" name="register" value="register" tabindex="10" id="register" />
<input type="reset" name="reset" value="reset" tabindex="11" id="reset" />
</label>
</form>
</fieldset>
</body>
</html>
CSS Code
body{
font-family: arial, verdana, sans-serif;
font-size: 0.8em;
font-weight: normal;
font-style: normal;
}
h1 {
}
p {
}
fieldset {
border: 1px solid #D9D9D9;
background-color: #F5F5F5;
}
fieldset legend {
color: #FFFFFF;
background-color: #8B8378;
padding: 5px;
padding-right: 9px;
padding-left: 9px;
}
label {
display: block;
padding: 5px;
}
label.cuisine {
display: inline;
}
Inlcudes - Base
<?php
$parentdirectory = dirname(dirname(__FILE__));
define('BASE_DIRECTORY', $parentdirectory);
ini_set('include_path', $parentdirectory.'/includes/');
?>
Includes - Class
<?php
class eventform {
//Declare data members/properties
private $fname,
$lname,
$email,
$cuisine,
$eventday,
$timezone,
$eventmonth;
//Generate months
public function eventmonths($startingmonth=1, $endingmonth=12) {
//Default timezone
$this->timezone = date_default_timezone_set('UTC');
//Loop through months
$months = range($startingmonth, $endingmonth);
$returnmonths = '';
foreach($months as $key) {
$returnmonths .= date('F', mktime(0, 0, 0, $key)).',';
}
return rtrim($returnmonths, ',');
}
//Generate years
public function eventyears() {
//Get current year
$year = date('Y');
//Loop through years
$startyear = $year;
// $endyear = date('Y', strtotime('+5 years'));
$endyear = date('Y', mktime(0,0,0,12,30,$year+5));
$returnyears = '';
for($startyear; $startyear < $endyear; $startyear=$startyear+1) {
$returnyears .= $startyear.',';
}
return rtrim($returnyears, ',') ;
}
//Loop through days for a specific month and year
public function eventdays($month, $year) {
// return date('t', strtotime($someYear . '-' . $someMonth . '-01'));
// return date('t', strtotime($someYear . '-' . $someMonth ));
// $numberofdays = date('t', strtotime($Year. '-' .$Month));
$numberofdays = date('t', mktime(0,0,0,$month,1,$year));
$startdays = 1;
$returndays = '';
for($startdays; $startdays <= $numberofdays; $startdays=$startdays+1) {
$returndays .= $startdays.',';
}
return rtrim($returndays, ',');
// return substr($returndays, 0, -1);
}
//Define the type of dropdown
public function dropdown($type) {
switch($type) {
case('days'):
$stringdays = $this->eventdays(1, 2011);
$arraydays = explode(',', $stringdays);
foreach($arraydays as $day) {
echo '<option value="'.$day.'">'.$day.'</option>';
}
break;
case('months'):
$stringmonths = $this->eventmonths();
$arraymonths = explode(',', $stringmonths);
foreach($arraymonths as $month) {
echo '<option value="'.date('n', strtotime($month)).'">'.$month.'</option>';
}
break;
case('years'):
$stringyears = $this->eventyears();
$arrayyears = explode(',', $stringyears);
foreach($arrayyears as $key) {
echo '<option value="'.$key.'">'.$key.'</option>';
}
break;
}
}
//Define the type of radiobuttons
public function radiobuttons ($type) {
switch($type) {
case('cuisine'):
$cuisines = array('italian', 'russian', 'malaysian');
$cuisinetype = array_combine(range(1, count($cuisines)), $cuisines);
foreach($cuisinetype as $key => $value) {
if(isset($_POST['register']) && $_POST['register'] == 'register') {
if(isset($_POST['cuisine']) && $_POST['cuisine'] == $value) {
$status = 'checked=checked';
}
else {
$status = '';
}
}
else {
$status = '';
}
echo '<input type="radio" name="cuisine" value="'.$value.'" tabindex="'.$key.'" id="'.$value.'" '.$status.'>',
'<label for="'.$value.'" class="cuisine">'.ucfirst($value).'</label>';
}
break;
}
}
//Validate form
public function validateform() {
if(isset($_POST['register']) && $_POST['register'] == 'register') {
$this->fname = isset($_POST['fname']) ? $_POST['fname'] : '';
$this->lname = isset($_POST['lname']) ? $_POST['lname'] : '';
$this->email = isset($_POST['email']) ? $_POST['email'] : '';
$this->cuisine = isset($_POST['cuisine']) ? $_POST['cuisine'] : '';
$this->eventday = isset($_POST['eventday']) ? $_POST['eventday'] : '';
$errormsg = array();
if(empty($this->fname)) {
$errormsg[0] = 'Please enter your first name';
}
if(empty($this->lname)) {
$errormsg[1] = 'Please enter your last name';
}
if(empty($this->email)) {
$errormsg[2] = 'Please specify an email address';
}
if(empty($this->cuisine)) {
$errormsg[3] = 'Please select a cuisine';
}
if(empty($this->eventday)) {
$errormsg[4] = 'Please select an event day';
}
foreach($errormsg as $key => $value) {
echo $value.'<br />';
}
//Basic email validation
if(!empty($this->email)) {
if(!preg_match('/^[a-zA-Z0-9_\.\-]+[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9_\.\-]+\.[a-zA-Z]{1,6}$/', $this->email)) {
echo 'Please enter a valid email address';
}
}
}
}
}
?>
return
strings prior to a break. My understanding is that a return would stop a function immediately especially if I have grouped case statements? \$\endgroup\$more stuff is added later on
, do you mean other case statements? \$\endgroup\$