The HTML delivers a form with an input field and submit button. It also contains a jQuery call to the POST handler. Here, I pass the method within the class which will be called to handle the data. Then I pass the data. In this case, the contents of the input field. Finally, An alert catches any data coming back from the class.
The POST handler accepts the POST request as and converts it into an object. It checks to see if a method has been called and that the method exists. If so, the method is invoked. If not, an error message is returned.
Enjoy!
The HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>Post test</title>
<style>
body
{
font-family:Sans-serif;
line-height:1.5em;
}
</style>
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
google.load("jqueryui", "1");
</script>
</head>
<body>
<form>
<label for="myname">Type your name</label>
<input type="text" name="myname" id="myname" />
<button type="submit">Submit</button>
</form>
<script>
(function()
{
$('form').submit(function()
{
$.post('posthandler.php',
{
method:'getContent',
myname:$(this).find('#myname').val()
}, function(data)
{
alert(data)
});
return false;
});
})();
</script>
</body>
</html>
The Class
<?php
class posthandler
{
private $postObject;
function __construct($p)
{
$this->postObject = (object) $p;
if($this->postObject->method && (method_exists($this, $this->postObject->method)))
{
$evalStr = '$this->'.$this->postObject->method.'();';
eval($evalStr);
}
else
{
echo 'Invalid method supplied';
}
}
function getcontent()
{
echo $this->postObject->myname;
}
}
new posthandler($_POST);
?>