Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding

  • Loading branch information...
commit 614a7e13321033c42e944786e2a9ee9e2816c95c 0 parents
@codepo8 authored
22 filters.php
@@ -0,0 +1,22 @@
+<?php
+/*
+ Sharing validation client and server side
+ Homepage: http://isithackday.com/hacks/validationdemo
+ Copyright (c)2010 Christian Heilmann
+ Code licensed under the BSD License:
+ http://wait-till-i.com/license.txt
+*/
+ $args = array(
+ 'name' => FILTER_SANITIZE_SPECIAL_CHARS,
+ 'message' => FILTER_SANITIZE_SPECIAL_CHARS,
+ 'homepage' => FILTER_SANITIZE_SPECIAL_CHARS,
+ 'done' => FILTER_SANITIZE_SPECIAL_CHARS,
+ 'email' => FILTER_SANITIZE_SPECIAL_CHARS
+ );
+ $_POST = filter_input_array(INPUT_POST, $args);
+ $email = $_POST['email'];
+ $name = $_POST['name'];
+ $homepage = $_POST['homepage'];
+ $message = $_POST['message'];
+ $filtered = true;
+?>
47 form.php
@@ -0,0 +1,47 @@
+<?php
+/*
+ Sharing validation client and server side
+ Homepage: http://isithackday.com/hacks/validationdemo
+ Copyright (c)2010 Christian Heilmann
+ Code licensed under the BSD License:
+ http://wait-till-i.com/license.txt
+*/
+function rendererror($name){
+ global $error;
+ if($error[$name]){
+ echo '<span class="error">'.$error[$name].'</span>';
+ } else {
+ echo '<span class="required">*</span>';
+ }
+}
+$req = $_SERVER['HTTP_X_REQUESTED_WITH'];
+?>
+<?php if ($req == ''){ ?>
+<form action="index.php" method="post" id="mainform">
+<?php } ?>
+ <?php if(sizeof($error)>0){?>
+ <div class="error">The form cannot be sent yet as there were some errors.
+ Please fix them and try again.</div>
+ <?php }?>
+ <div>
+ <label for="name">Name <?php rendererror('name');?></label>
+ <input type="text" name="name" value="<?php echo $name;?>" id="name">
+ </div>
+ <div>
+ <label for="email">Email <?php rendererror('email');?></label>
+ <input type="text" name="email" value="<?php echo $email;?>"
+ id="email">
+ </div>
+ <div>
+ <label for="homepage">Homepage</label>
+ <input type="text" name="homepage" value="<?php echo $homepage;?>"
+ id="homepage">
+ </div>
+ <div>
+ <label for="message">Message <?php rendererror('message');?></label>
+ <textarea name="message" id="message" value="<?php echo $message;?>"><?php echo $message;?></textarea>
+ </div>
+ <div class="bar"><input type="submit" name="done" value="Make it so!"></div>
+<?php if ($req == ''){ ?>
+</form>
+<?php } ?>
45 index.php
@@ -0,0 +1,45 @@
+<?php
+/*
+ Sharing validation client and server side
+ Homepage: http://isithackday.com/hacks/validationdemo
+ Copyright (c)2010 Christian Heilmann
+ Code licensed under the BSD License:
+ http://wait-till-i.com/license.txt
+*/
+include('filters.php');?>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+ <title>Form validation example</title>
+ <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.8.0/build/reset-fonts-grids/reset-fonts-grids.css&2.8.0/build/base/base-min.css">
+<script type="text/javascript" src="http://yui.yahooapis.com/combo?3.2.0/build/yui/yui-min.js"></script>
+<style type="text/css" media="screen">
+input{width:260px;}
+textarea{width:265px;height:10em;}
+label{width:10em;float:left;font-weight:bold;}
+form div{padding:10px 0;overflow:hidden;position:relative;}
+label span{position:absolute;left:405px;}
+.bar input{width:auto;}
+.bar{background:#eee;text-align:right;padding:10px;margin:2em 0;}
+.error{color:#c00;border:2px solid #900;padding:2px;-moz-border-radius:3px;font-weight:bold;}
+#ft{font-size:80%;color:#ccc;text-align:right;}
+#ft a{color:#999;}</style>
+</head>
+<body class="yui-skin-sam">
+<div id="doc" class="yui-t7">
+
+ <div id="hd" role="banner">
+ <h1>Form validation example</h1>
+ </div>
+ <div id="bd" role="main">
+ <?php include('validate.php');?>
+ </div>
+ <div id="ft" role="contentinfo">
+ <p>Written by <a href="http://wait-till-i.com">Christian Heilmann</a></p>
+ </div>
+
+</div>
+<script src="validate.js"></script>
+</body>
+</html>
27 rules.php
@@ -0,0 +1,27 @@
+<?php
+/*
+ Sharing validation client and server side
+ Homepage: http://isithackday.com/hacks/validationdemo
+ Copyright (c)2010 Christian Heilmann
+ Code licensed under the BSD License:
+ http://wait-till-i.com/license.txt
+*/
+$rules = array(
+ 'name' => array(
+ 'error' => 'Please enter a name that is at least 10 characters long',
+ 'pattern' => '/.{10}/i'
+ ),
+ 'message' => array(
+ 'error' => 'Please enter a message at least 20 characters long',
+ 'pattern' => '/.{20}/i'
+ ),
+ 'email' => array(
+ 'error' => 'Please enter a valid email',
+ 'pattern' => '/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/i'
+ )
+);
+if(isset($_GET['json'])){
+ echo 'rules='.json_encode($rules);
+}
+?>
+
1  thanks.php
@@ -0,0 +1 @@
+<h2>Thanks and stuff</h2>
40 validate.js
@@ -0,0 +1,40 @@
+/*
+ Sharing validation client and server side
+ Homepage: http://isithackday.com/hacks/validationdemo
+ Copyright (c)2010 Christian Heilmann
+ Code licensed under the BSD License:
+ http://wait-till-i.com/license.txt
+*/
+YUI().use('io','node',
+ function(Y) {
+ Y.on('submit',function(event){
+ Y.io('validate.php',
+ {
+ method:'POST',
+ data:'name=' + Y.one('#name').get('value') +
+ '&email=' + Y.one('#email').get('value') +
+ '&message=' + Y.one('#message').get('value') +
+ '&done=done'
+ }
+ );
+ event.preventDefault();
+ },'#mainform');
+ var success = function(id,o){
+ if(o.responseText !== undefined){
+ Y.one('#mainform').set('innerHTML',o.responseText);
+ var fieldID = Y.one('#mainform span.error').
+ ancestor('label').
+ get('htmlFor');
+ Y.one('#'+fieldID).focus();
+ }
+ }
+ var failure = function(id,o){
+ if(o.responseText !== undefined){
+ Y.one('#mainform').submit();
+ // failure case
+ }
+ }
+ Y.on('io:success',success);
+ Y.on('io:failure',failure);
+ }
+);
27 validate.php
@@ -0,0 +1,27 @@
+<?php
+/*
+ Sharing validation client and server side
+ Homepage: http://isithackday.com/hacks/validationdemo
+ Copyright (c)2010 Christian Heilmann
+ Code licensed under the BSD License:
+ http://wait-till-i.com/license.txt
+*/
+ if(!isset($filtered)){include('filters.php');}
+ if(!isset($rules)){include('rules.php');}
+
+ if(!isset($_POST['done'])){
+ include('form.php');
+ } else {
+ $error = array();
+ foreach(array_keys($rules) as $r){
+ if(!preg_match($rules[$r]['pattern'],$_POST[$r])){
+ $error[$r] = $rules[$r]['error'];
+ }
+ }
+ if(sizeof($error) > 0){
+ include('form.php');
+ } else {
+ include('thanks.php');
+ }
+ }
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.