Created
January 26, 2011 00:03
-
-
Save pifantastic/795975 to your computer and use it in GitHub Desktop.
A simple todo list I'm using for an Intro to jQuery talk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE hmtl> | |
<html> | |
<head><title>My todo list</title></head> | |
<body> | |
<h1>My todo list:</h1> | |
<form> | |
<input type="text" id="item" /> | |
<input type="submit" id="save" value="Save" /> | |
</form> | |
<ul id="list"></ul> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> | |
<script> | |
$(document).ready(function() { | |
// Load todo list from server. | |
$.getJSON('http://localhost/server.php', function(res) { | |
$('#list').html('<li>' + res.join('</li><li>') + '</li>'); | |
}); | |
// Add item to list. | |
$("form").submit(function() { | |
var item = $('#item').val(); | |
$('#list').append('<li>' + item + '</li>'); | |
save(); | |
$('#item').val(''); | |
return false; | |
}); | |
// Remove item from list. | |
$('#list li').live('dblclick', function() { | |
$(this).remove(); | |
save(); | |
}); | |
// Save list to server. | |
function save() { | |
var items = $('#list li').map(function() { | |
return $(this).text(); | |
}).get(); | |
$.ajax({ | |
url: 'http://localhost/server.php', | |
data: { list: items } | |
}); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
// Initialize session if empty. | |
if (!isset($_SESSION['list'])) { | |
$_SESSION['list'] = array(); | |
} | |
// Update list. | |
if (isset($_REQUEST['list'])) { | |
$_SESSION['list'] = $_REQUEST['list']; | |
} | |
// Return list as JSON. | |
header('Content-Type: application/json'); | |
die(json_encode($_SESSION['list'])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment