Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding dirty php handling for example

  • Loading branch information...
commit 87c59da37bd2801568240ab66496dce44a6a9bef 1 parent ce0c090
@wonkenstein authored
Showing with 174 additions and 27 deletions.
  1. +82 −0 example/_functions.php
  2. +53 −0 example/_handler.php
  3. +39 −27 example/index.php
View
82 example/_functions.php
@@ -0,0 +1,82 @@
+<?php
+/**
+ *
+ * @param unknown_type $value
+ */
+function check_int($value){
+ return is_numeric($value) && (round($value) == $value);
+}
+
+
+/**
+ *
+ * @param unknown_type $value
+ */
+function check_numeric($value){
+ return is_numeric($value);
+}
+
+
+/**
+ *
+ * @param unknown_type $validate
+ * @param unknown_type $values
+ * @return multitype:unknown
+ */
+function validate_form($validate, $values) {
+ $error = array();
+
+ foreach ($validate as $k => $v) {
+ echo $v['validate_function'], ' ', $values[$k] , '<br />';
+ $ret = call_user_func($v['validate_function'], $values[$k]);
+
+
+ if (!$ret) {
+ $error[$k] = $v['error'];
+ }
+ }
+
+ return $error;
+}
+
+
+/**
+ * Pretty awful looking function to get cvalues from form
+ * @param unknown_type $key
+ * @param unknown_type $values
+ * @param unknown_type $is_checked_value
+ * @param unknown_type $selected_type
+ */
+function form_get_value($key, $values, $check_value='', $select=FALSE) {
+ if ($check_value) {
+ if (is_array($values[$key])) {
+ if (in_array($check_value, $values[$key])) {
+ if ($select) {
+ return 'selected="selected"';
+ }
+ else {
+ return 'checked="checked"';
+ }
+ }
+ }
+ else if ($check_value == $values[$key]) {
+ if ($select) {
+ return 'selected="selected"';
+ }
+ else {
+ return 'checked="checked"';
+ }
+ }
+ }
+ else {
+ return 'value="' . $values[$key] . '"';
+ }
+}
+
+
+function form_error_class($key, $errors) {
+ //print_r($errors);
+ if (!empty($errors[$key])) {
+ return 'form-error';
+ }
+}
View
53 example/_handler.php
@@ -0,0 +1,53 @@
+<?php
+include '_functions.php';
+
+// do some data checking
+// check numeric values are numeric
+// do conversions before firing off to the pace calculator
+$time = $_GET['hrs'] . '.' . $_GET['mins'] . '.' . $_GET['secs'];
+$distance = ($_GET['length']) ? $_GET['length'] : $_GET['common_length'];
+$pace = $_GET['pace_hrs'] . '.' . $_GET['pace_mins'] . '.' . $_GET['pace_secs'];
+$pace_type = $_GET['pace_type'];
+
+$form_values = array(
+ 'time' => $time,
+ 'distance' => $distance,
+ 'pace' => $pace,
+ 'pace_type' => $pace_type,
+);
+
+
+$validate = array(
+ 'hrs' => array(
+ 'validate_function' => 'check_int',
+ 'error' => 'Time hrs must be a round number',
+ ),
+ 'mins' => array(
+ 'validate_function' => 'check_int',
+ 'error' => 'Time mins must be a round number',
+ ),
+ 'secs' => array(
+ 'validate_function' => 'check_int',
+ 'error' => 'Time seconds must be a round number',
+ ),
+ 'length' => array(
+ 'validate_function' => 'check_numeric',
+ 'error' => 'Length must be numeric',
+ ),
+ 'pace_hrs' => array(
+ 'validate_function' => 'check_int',
+ 'error' => 'Pace hrs must be a round number',
+ ),
+ 'pace_mins' => array(
+ 'validate_function' => 'check_int',
+ 'error' => 'Pace mins must be a round number',
+ ),
+ 'pace_secs' => array(
+ 'validate_function' => 'check_int',
+ 'error' => 'Pace secs must be a round number',
+ ),
+);
+
+$ERRORS = validate_form($validate, $_GET);
+print_r($ERRORS);
+
View
66 example/index.php
@@ -1,3 +1,4 @@
+<?php include '_handler.php'; ?>
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
@@ -31,15 +32,15 @@
<legend>Type of calculation</legend>
<label for="calculator-type-pace">
- <input type="radio" id="calculator-type-pace" name="calculator-type" value="pace" />
+ <input type="radio" id="calculator-type-pace" name="calculator-type" value="pace" <?php echo form_get_value('calculator-type', $_GET, 'pace'); ?> />
Pace
</label>
<label for="calculator-type-distance">
- <input type="radio" id="calculator-type-distance" name="calculator-type" value="distance" />
+ <input type="radio" id="calculator-type-distance" name="calculator-type" value="distance" <?php echo form_get_value('calculator-type', $_GET, 'distance'); ?> />
Distance
</label>
<label for="calculator-type-time">
- <input type="radio" id="calculator-type-time" name="calculator-type" value="time" />
+ <input type="radio" id="calculator-type-time" name="calculator-type" value="time" <?php echo form_get_value('calculator-type', $_GET, 'time'); ?> />
Time
</label>
</fieldset>
@@ -47,65 +48,76 @@
<fieldset id="time">
<legend>Time</legend>
- <div class="form-element">
+ <div class="form-element <?php echo form_error_class('hrs', $ERRORS) ?>">
<label for="hrs">Hours</label>
- <input type="number" min="0" id="hrs" name="hrs" value="" />
+ <input type="number" min="0" id="hrs" name="hrs" <?php echo form_get_value('hrs', $_GET); ?> />
</div>
- <div class="form-element">
+ <div class="form-element" <?php echo form_error_class('mins', $ERRORS) ?>>
<label for="mins">Mins</label>
- <input type="number" min="0" max="60" id="mins" name="mins" value="" />
+ <input type="number" min="0" max="60" id="mins" name="mins" <?php echo form_get_value('mins', $_GET); ?> />
</div>
- <div class="form-element">
+ <div class="form-element" <?php echo form_error_class('secs', $ERRORS) ?>>
<label for="secs">Secs</label>
- <input type="number" min="0" max="60" id="secs" name="secs" value="" />
+ <input type="number" min="0" max="60" id="secs" name="secs" <?php echo form_get_value('secs', $_GET); ?> />
</div>
</fieldset>
<fieldset id="distance">
<legend>Distance</legend>
- <div class="form-element">
+ <div class="form-element <?php echo form_error_class('length', $ERRORS) ?>">
<label for="length">Length</label>
- <input type="text" id="length" name="length" />
+ <input type="text" id="length" name="length" <?php echo form_get_value('length', $_GET); ?> />
</div>
+ <fieldset id="distance-type">
+ <legend>Distance Type</legend>
+ <label for="distance-type-mile">
+ <input type="radio" id="distance-type-mile" name="distance_type" value="imperial" <?php echo form_get_value('distance_type', $_GET, 'imperial'); ?> />
+ miles
+ </label>
+ <label for="distance-type-km">
+ <input type="radio" id="distance-type-km" name="distance_type" value="metric" <?php echo form_get_value('distance_type', $_GET, 'metric'); ?> />
+ km
+ </label>
+ </fieldset>
<div class="form-element">
<label for="common-lengths">Common distances</label>
<select id="common-lengths" name="common_length">
- <option value="marathon">Marathon</option>
- <option value="half-marathon">Half Marathon</option>
- <option value="1km">1km</option>
- <option value="5km">5km</option>
- <option value="10km">10km</option>
- <option value="1mile">1 mile</option>
- <option value="5miles">5 miles</option>
- <option value="10miles">10 miles</option>
+ <option value="marathon" <?php echo form_get_value('common_length', $_GET, 'marathon', TRUE); ?>>Marathon</option>
+ <option value="half-marathon" <?php echo form_get_value('common_length', $_GET, 'half-marathon', TRUE); ?>>Half Marathon</option>
+ <option value="1km" <?php echo form_get_value('common_length', $_GET, '1km', TRUE); ?>>1km</option>
+ <option value="5km" <?php echo form_get_value('common_length', $_GET, '5km', TRUE); ?>>5km</option>
+ <option value="10km" <?php echo form_get_value('common_length', $_GET, '10km', TRUE); ?>>10km</option>
+ <option value="1mile" <?php echo form_get_value('common_length', $_GET, '1mile', TRUE); ?>>1 mile</option>
+ <option value="5miles" <?php echo form_get_value('common_length', $_GET, '5miles', TRUE); ?>>5 miles</option>
+ <option value="10miles" <?php echo form_get_value('common_length', $_GET, '10miles', TRUE); ?>>10 miles</option>
</select>
</div>
</fieldset>
<fieldset id="pace">
<legend>Pace</legend>
- <div class="form-element">
+ <div class="form-element <?php echo form_error_class('pace_hrs', $ERRORS) ?>">
<label for="pace-hrs">Hours</label>
- <input type="number" min="0" id="pace-hrs" name="pace_hrs" />
+ <input type="number" min="0" id="pace-hrs" name="pace_hrs" <?php echo form_get_value('pace_hrs', $_GET); ?> />
</div>
- <div class="form-element">
+ <div class="form-element <?php echo form_error_class('pace_mins', $ERRORS) ?>">
<label for="pace_mins">Mins</label>
- <input type="number" min="0" max="60" id="pace-mins" name="pace_mins" />
+ <input type="number" min="0" max="60" id="pace-mins" name="pace_mins" <?php echo form_get_value('pace_mins', $_GET); ?> />
</div>
- <div class="form-element">
+ <div class="form-element <?php echo form_error_class('pace_secs', $ERRORS) ?>">
<label for="pace_secs">Secs</label>
- <input type="number" min="0" max="60" id="pace-secs" name="pace_secs" />
+ <input type="number" min="0" max="60" id="pace-secs" name="pace_secs" <?php echo form_get_value('pace_secs', $_GET); ?> />
</div>
<fieldset id="pace-type">
<legend>Pace Type</legend>
<label for="pace-type-mile">
- <input type="radio" id="pace-type-mile" name="pace_type" value="mile" />
+ <input type="radio" id="pace-type-mile" name="pace_type" value="imperial" <?php echo form_get_value('pace_type', $_GET, 'imperial'); ?> />
min/mile
</label>
<label for="pace-type-km">
- <input type="radio" id="pace-type-km" name="pace_type" value="km" />
+ <input type="radio" id="pace-type-km" name="pace_type" value="metric" <?php echo form_get_value('pace_type', $_GET, 'metric'); ?> />
min/km
</label>
</fieldset>
Please sign in to comment.
Something went wrong with that request. Please try again.