Created
November 20, 2011 17:04
-
-
Save jlebensold/1380499 to your computer and use it in GitHub Desktop.
PUTting Data with jQuery
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
home.tpl.php: | |
<html> | |
<head> | |
<title>Names Hello</title> | |
<script type="text/javascript" src="/js/jquery-1.5.1.js"></script> | |
<style> | |
* | |
{ | |
margin: 0 0; | |
padding: 0 0; | |
font-family: helvetica, arial, sans-serif; | |
} | |
body { | |
background: #CCC; | |
} | |
#container | |
{ | |
padding: 20px; | |
margin: 0 auto; | |
width: 600px; | |
background: #FFF; | |
} | |
#names | |
{ | |
width:150px; | |
} | |
</style> | |
<script> | |
$(function() | |
{ | |
$("#names").delegate('.edit','click',function(e) | |
{ | |
$(this).hide() | |
$(this).parent().append('<input type="text" value="'+$(this).text()+'"></input><button class="save">save</button>'); | |
}); | |
$("#names").delegate('button.save','click',function(e) | |
{ | |
$.ajax({type: 'PUT', | |
dataType: 'json', | |
data : { | |
name: $(this).parent().find('input').val() | |
}, | |
url : '/names/'+$(this).parents('li').data('id'), | |
success : function(resp) | |
{ | |
renderNames(resp); | |
} | |
}); | |
e.preventDefault(); | |
}); | |
$('a.list').click(function(e) | |
{ | |
findAll(); | |
e.preventDefault(); | |
}); | |
$("form").submit(function(e) | |
{ | |
$.post("/names", | |
{name: $("#newname").val() }, | |
function(resp) { | |
findAll(); | |
}); | |
e.preventDefault(); | |
}); | |
}); | |
function findAll() | |
{ | |
$.get("/names",function(resp) | |
{ | |
renderNames(resp); | |
},"json"); | |
}; | |
function renderNames(names) | |
{ | |
$("#names").empty(); | |
$.each(names,function(k,v) | |
{ | |
$("#names").append('<li id="name_'+v.id+'" data-id="'+v.id+'"><a class="edit">'+v.name+'</a></li>'); | |
}); | |
} | |
</script> | |
</head> | |
<body> | |
<div id="container"> | |
<h3> Names <a class="list" href="#">Load</a></h3> | |
<ul id="names"></ul> | |
<form class="" id="newnamefrm"> | |
<input type="text" id="newname"></input><input type="submit" value="Add Name" /> | |
</form> | |
</div> | |
</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 | |
require '../Slim/Slim.php'; | |
require '../Name.php'; | |
function json($obj) | |
{ | |
header('Content-Type', 'application/json'); | |
return json_encode($obj); | |
} | |
$app = new Slim(); | |
$app->config(array('templates.path' => '../templates')); | |
$app->get('/names/:id',function($id) { | |
echo json(Name::find($id)); | |
}); | |
$app->get('/names',function() { | |
echo json(Name::findAll()); | |
}); | |
$app->post('/names',function() use ($app) { | |
$n = new Name(null,$app->request()->post('name')); | |
$n->create(); | |
echo '{status: "success" }'; | |
}); | |
$app->put('/names/:id',function($id) use ($app) { | |
$name = Name::find($id); | |
$name->name = $app->request()->put('name'); | |
$name->update(); | |
echo json(Name::findAll()); | |
}); | |
$app->get('/', function() use($app) { | |
$app->render('home.tpl.php'); | |
}); | |
$app->run(); |
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(); | |
if (!isset($_SESSION['names'])) | |
$_SESSION['names'] = array("jane","jim","john","emily","bill","sara"); | |
class Name | |
{ | |
public $id; | |
public $name; | |
public function __construct($id, $name) | |
{ | |
$this->id = $id; | |
$this->name = $name; | |
} | |
public function create() | |
{ | |
$_SESSION['names'][] = $this->name; | |
} | |
public function update() | |
{ | |
$_SESSION['names'][$this->id] = $this->name; | |
} | |
public static function findAll() | |
{ | |
$names = array(); | |
foreach($_SESSION['names'] as $id => $name) | |
$names[] = new Name($id , $name); | |
return $names; | |
} | |
public static function find($id) | |
{ | |
return new Name($id, $_SESSION['names'][$id]); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment