Commit 71edf338 authored by O'Reilly Media, Inc.'s avatar O'Reilly Media, Inc.
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

Chapter 1/database.sql

0 → 100755
+79 −0
Changes for Chapter 1/database.sql: 79 added lines, 0 removed lines.
Original line number Diff line number Diff line
-- phpMyAdmin SQL Dump
-- version 3.5.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 14, 2013 at 12:12 PM
-- Server version: 5.5.24-log
-- PHP Version: 5.3.13

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `ajax`
--

-- --------------------------------------------------------

--
-- Table structure for table `quote`
--

DROP TABLE IF EXISTS `quote`;
CREATE TABLE IF NOT EXISTS `quote` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `quotetext` text NOT NULL,
  `author` varchar(96) NOT NULL,
  `tags` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

--
-- Dumping data for table `quote`
--

INSERT INTO `quote` (`id`, `user_id`, `quotetext`, `author`, `tags`) VALUES
(1, 1, 'Don''t cry because it''s over, smile because it happened.', 'Dr. Seuss', 'life'),
(2, 1, 'You only live once, but if you do it right, once is enough.', 'Mae West', 'life'),
(3, 1, 'To live is the rarest thing in the world. Most people exist, that is all.', 'Oscar Wilde', 'life'),
(4, 1, 'I may not have gone where I intended to go, but I think I have ended up where I needed to be.', 'Douglas Adams', 'life'),
(5, 1, 'A friend is someone who knows all about you and still loves you.', 'Elbert Hubbard', 'friend,life'),
(6, 1, 'If you judge people, you have no time to love them.', 'Mother Teresa', 'love'),
(7, 1, 'We have nothing to fear but fear itself', 'John F Kennedy', ''),
(8, 1, 'If a man does his best, what else is there?', 'Gen. George S. Patton', 'life,victory'),
(9, 1, 'Yesterday''s the past, tomorrow''s the future, but today is a gift. That''s why it''s called the present.', 'Bil Keane', 'life');

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `fullname` varchar(128) NOT NULL,
  `username` varchar(24) NOT NULL,
  `password` varchar(24) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `fullname`, `username`, `password`) VALUES
(1, 'Walter Wimberly', 'demo', 'demo');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Chapter 2/index.htm

0 → 100755
+206 −0
Changes for Chapter 2/index.htm: 206 added lines, 0 removed lines.
Original line number Diff line number Diff line
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Famous Quotes</title>
<link href='http://fonts.googleapis.com/css?family=Crafty+Girls' rel='stylesheet' type='text/css'>
<link href='./style.css' rel='stylesheet' type='text/css'>
</head>

<body>
<div id="container">
	<div id="header">
		<h1 class="title font">Famous Quotes</h1>
		<ul class="nav">
			<li><a href="#" class="" id="btnGetNewerQuotes">Get Newer Quotes</a></li>
			<li><a href="#" class="" id="btnSubmitQuote">Submit Quote</a></li>
			<li><a href="#" class="" id="btnLogin" >Login</a></li>
			<li class="last"><input type="text" id="searchPhrase" /><a href="#" id="btnSearch">Search</a></li>
		</ul>
		<br class="clear">
	</div>
	
	<div id="single" class="highlight">
	</div>
	
	<div id="content">
		<div id="login" class="hide">
		<form method="post" action="login.php">
		<ul class="thirds">
			<li>
				<label for="username">Login Name</label>
				<input type="text" name="username" id="username">
			</li>
			<li>
				<label for="password">Password</label>
				<input type="password" name="password" id="password">
			</li>
			<li>
				<input type="submit" name="submitLogin" id="submitLogin" value="Login" class="submit">
			</li>
		</ul>
		<br class="clear">
		</form>
		</div>
		<div id="addQuote" class="hide">
		<form method="post" action="quote-add.php">
		<ul>
			<li class="full">
				<label for="quotetext">Famous Quote</label>
				<textarea name="quotetext" id="quotetext" cols="45" rows="5"></textarea>
			</li>
			<li>
				<label for="author">Quote From</label>
				<input type="text" name="author" id="author">
			</li>
			<li>
				<label for="tags">Tags</label>
				<input type="text" name="tags" id="tags">
			</li>
			<li>
				<input type="submit" name="submitQuote" id="submitQuote" value="Add" class="submit">
			</li>
		</ul>
		<br class="clear">
		</form>
		</div>
		<div id="quotes"></div>
	</div>
	<div id="footer"></div>
</div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var loggedIn = false;
$(document).ready(function() {
	$('#btnLogin').on('click', function() {
		$('#login').slideDown();
	});
	
	$('#btnSubmitQuote').on('click', function() {
		if(loggedIn == true) {
			$('#addQuote').slideDown();
		} else {
			alert('You must be logged in to access this functionality.');
		}
	});
	
	
	setInterval(function() { $('#single').load('quote-get-single.php'); }, 30000);
	$('#single').load('quote-get-single.php'); 
	
	$('#submitLogin').on('click', function() {
		$.post(
			'login.php', 
			{ 
				'u': $('#username').val(), 
				'p' : $('#password').val()
			},
			function(data) {
				if(data == 'success') {
					alert('You have been logged in.');
					loggedIn = true;
					$('#login').slideUp();
				} else {
					alert('You did not login to the server.');
					loggedIn = false;
				}
			});
		return false;
	});
	
	$('#submitQuote').on('click', function() {
		var ready = true;
		var qt = $('#quotetext').val();
		if( qt.length < 10 ) {
			ready = false;
			alert('You must provide some text to submit for the quote.');
		}
		if($('#author').val() == '') {
			ready = false;
			alert('You must provide the author.');
		}
		if(ready == true) {
			$.post(
				'quote-add.php',
				{
					'quotetext': $('#quotetext').val(),
					'tags': $('#tags').val(),
					'author': $('#author').val()
				}, 
				function (data) {
					if(data == "success") {
						alert("Your quote was added");
					} else {
						var msg = data.split("-");
						if(msg[0] == "fail") {
							alert(msg[1]);
						}
					}
				});
			}
			return false;
	});
	
	$('#btnGetNewerQuotes').on('click', function() {
		loadJSON();
	});
	
	$('#btnSearch').on('click', function() {
		performSearch();
	});
	
	$('#searchPhrase').on('keypress', function(e) {
		if(e.which == 13) {
			e.preventDefault();
			performSearch();
		}
	});
	
	
});

var quoteIndex = 0;
function loadJSON() {
	$.get(
		'quote-get-list-json.php',
		{'start' : quoteIndex},
		function(data) {
			
			var str = buildQuoteDisplay(data);
			
			$('#quotes').prepend(str);
			quoteIndex += 5;
		}
	);
}


function buildQuoteDisplay(quotesStr) {
	var data = $.parseJSON(quotesStr);
	var str = '';
	for( quote in data.quotes) {
		var myQuote = data.quotes[quote];
		str += '<div><p class="quote">' + myQuote.quotetext + '</p>';
		if(myQuote.tags.length > 0) {
			str += '<p class="tags">tags: ';
			for( tag in myQuote.tags ) {
				str += '<span>' + myQuote.tags[tag] + '</span>';
			
				if(tag < myQuote.tags.length - 1) {
					str += ', ';
				}
			}
			str += '</p>';
		}
		str += '<p class="author">' +
			myQuote.author + '</p><p class="clear">&nbsp;</p></div>';
	}
	
	return str;
}

function performSearch() {
}
</script>
</body>
</html>

Chapter 2/style.css

0 → 100755
+121 −0
Changes for Chapter 2/style.css: 121 added lines, 0 removed lines.
Original line number Diff line number Diff line
body, input, textarea {
	font-family: Tahoma, Geneva, sans-serif;
}
#container {
	min-width: 400px;
	max-width: 1000px;
	margin: auto;
	padding: 10px;
}
#content {
	clear: both;
	padding: 5px;
}
.hide {
	display: none;
}
.font {
	font-family: 'Crafty Girls', cursive;
}
.title {
	text-align: center;
	font-size: 2.5em;
}
li label {
	width: 105px;
	margin-right: 10px;
	text-align: right;
	display: inline-block;
}
form {
	clear: both;
	padding: 10px;
	border-radius: 10px;
	border: 1px solid #333;
	background-color: #f3f3f3;
}
form li {
	width: 25%;
	list-style: none;
	float: left;
}
li.full {
	width: 100%;
}
.full textarea {
	width: 58%;
	height: 48px;
}

.submit {
	margin-left: 25px;
	width: 25%;
}
.nav {
	padding: 0;
	margin: 0 5px;
}
.nav li {
	display: inline-block;
	list-style: none;
	float: left;
	margin-right: 10px;
}
.nav li.last {
	float: right;
}
.nav li.last a {
	float: right;
}
.nav li a, .nav li a:visited {
	text-decoration: none;
	font-weight: bold;
	padding: 5px;
	display: block;
	background: #CCC;
	border: 1px solid #333;
	border-radius: 10px;
	color: #009;
}
.clear {
	height: 1px;
	overflow: hidden;
	clear: both;
	margin: 0;
	padding: 0;
}
#quotes div {
	border: 1px solid #aaa;
	clear: both;
	padding: 5px;
	margin: 5px 0;
	border-radius: 5px;
}
.author, .tags {
	width: 48%;
	margin-top: -5px;
	font-size: .9em;
}
.author {
	text-align:right;
	font-style:italic;
	float: right;
}
.tags {
	float: left;
}
.tags span:hover {
	text-decoration: underline;
	cursor: pointer;
}
.highlight {
	border: 1px solid #333;
	background-color: #f3f3f3;
	padding: 5px;
	margin: 10px 5px;
	border-radius: 5px;
}
p {
	padding: 0;
	margin: 5px;
}
 No newline at end of file

Chapter 3/db.php

0 → 100755
+14 −0
Changes for Chapter 3/db.php: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php
session_start();

$hostname = 'localhost';
$database = 'ajax';
$username = 'root';
$password = '';

try {
	$pdb = new PDO('mysql:host=' .$hostname .';dbname=' .$database, $username, $password);
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage();
    die();
}
 No newline at end of file

Chapter 3/index.htm

0 → 100755
+206 −0
Changes for Chapter 3/index.htm: 206 added lines, 0 removed lines.
Original line number Diff line number Diff line
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Famous Quotes</title>
<link href='http://fonts.googleapis.com/css?family=Crafty+Girls' rel='stylesheet' type='text/css'>
<link href='./style.css' rel='stylesheet' type='text/css'>
</head>

<body>
<div id="container">
	<div id="header">
		<h1 class="title font">Famous Quotes</h1>
		<ul class="nav">
			<li><a href="#" class="" id="btnGetNewerQuotes">Get Newer Quotes</a></li>
			<li><a href="#" class="" id="btnSubmitQuote">Submit Quote</a></li>
			<li><a href="#" class="" id="btnLogin" >Login</a></li>
			<li class="last"><input type="text" id="searchPhrase" /><a href="#" id="btnSearch">Search</a></li>
		</ul>
		<br class="clear">
	</div>
	
	<div id="single" class="highlight">
	</div>
	
	<div id="content">
		<div id="login" class="hide">
		<form method="post" action="login.php">
		<ul class="thirds">
			<li>
				<label for="username">Login Name</label>
				<input type="text" name="username" id="username">
			</li>
			<li>
				<label for="password">Password</label>
				<input type="password" name="password" id="password">
			</li>
			<li>
				<input type="submit" name="submitLogin" id="submitLogin" value="Login" class="submit">
			</li>
		</ul>
		<br class="clear">
		</form>
		</div>
		<div id="addQuote" class="hide">
		<form method="post" action="quote-add.php">
		<ul>
			<li class="full">
				<label for="quotetext">Famous Quote</label>
				<textarea name="quotetext" id="quotetext" cols="45" rows="5"></textarea>
			</li>
			<li>
				<label for="author">Quote From</label>
				<input type="text" name="author" id="author">
			</li>
			<li>
				<label for="tags">Tags</label>
				<input type="text" name="tags" id="tags">
			</li>
			<li>
				<input type="submit" name="submitQuote" id="submitQuote" value="Add" class="submit">
			</li>
		</ul>
		<br class="clear">
		</form>
		</div>
		<div id="quotes"></div>
	</div>
	<div id="footer"></div>
</div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var loggedIn = false;
$(document).ready(function() {
	$('#btnLogin').on('click', function() {
		$('#login').slideDown();
	});
	
	$('#btnSubmitQuote').on('click', function() {
		if(loggedIn == true) {
			$('#addQuote').slideDown();
		} else {
			alert('You must be logged in to access this functionality.');
		}
	});
	
	
	setInterval(function() { $('#single').load('quote-get-single.php'); }, 30000);
	$('#single').load('quote-get-single.php'); 
	
	$('#submitLogin').on('click', function() {
		$.post(
			'login.php', 
			{ 
				'u': $('#username').val(), 
				'p' : $('#password').val()
			},
			function(data) {
				if(data == 'success') {
					alert('You have been logged in.');
					loggedIn = true;
					$('#login').slideUp();
				} else {
					alert('You did not login to the server.');
					loggedIn = false;
				}
			});
		return false;
	});
	
	$('#submitQuote').on('click', function() {
		var ready = true;
		var qt = $('#quotetext').val();
		if( qt.length < 10 ) {
			ready = false;
			alert('You must provide some text to submit for the quote.');
		}
		if($('#author').val() == '') {
			ready = false;
			alert('You must provide the author.');
		}
		if(ready == true) {
			$.post(
				'quote-add.php',
				{
					'quotetext': $('#quotetext').val(),
					'tags': $('#tags').val(),
					'author': $('#author').val()
				}, 
				function (data) {
					if(data == "success") {
						alert("Your quote was added");
					} else {
						var msg = data.split("-");
						if(msg[0] == "fail") {
							alert(msg[1]);
						}
					}
				});
			}
			return false;
	});
	
	$('#btnGetNewerQuotes').on('click', function() {
		loadJSON();
	});
	
	$('#btnSearch').on('click', function() {
		performSearch();
	});
	
	$('#searchPhrase').on('keypress', function(e) {
		if(e.which == 13) {
			e.preventDefault();
			performSearch();
		}
	});
	
	
});

var quoteIndex = 0;
function loadJSON() {
	$.get(
		'quote-get-list-json.php',
		{'start' : quoteIndex},
		function(data) {
			
			var str = buildQuoteDisplay(data);
			
			$('#quotes').prepend(str);
			quoteIndex += 5;
		}
	);
}


function buildQuoteDisplay(quotesStr) {
	var data = $.parseJSON(quotesStr);
	var str = '';
	for( quote in data.quotes) {
		var myQuote = data.quotes[quote];
		str += '<div><p class="quote">' + myQuote.quotetext + '</p>';
		if(myQuote.tags.length > 0) {
			str += '<p class="tags">tags: ';
			for( tag in myQuote.tags ) {
				str += '<span>' + myQuote.tags[tag] + '</span>';
			
				if(tag < myQuote.tags.length - 1) {
					str += ', ';
				}
			}
			str += '</p>';
		}
		str += '<p class="author">' +
			myQuote.author + '</p><p class="clear">&nbsp;</p></div>';
	}
	
	return str;
}

function performSearch() {
}
</script>
</body>
</html>
Loading