<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script>
$(document).ready(function(){
$("form.userform").change(function() {
$.post("ajaxtest", $("form.userform").serialize(), function( data ) {
if( data.name== "Anatol :-)" )
alert('test');
}, "json");
});
});
</script>
</head>
<body>
<div data-url="panel-fixed-page1" data-role="page" id="panel-fixed-page1" data-title="Menu and Login">
<div data-role="panel" data-position="right" data-position-fixed="true" data-display="overlay" data-theme="a" id="add-form">
<form class="userform">
<h2>Login</h2>
<label for="name">Username:</label>
<input name="name" id="name" value="" data-clear-btn="true" data-mini="true" type="text">
<label for="password">Password:</label>
<input name="password" id="password" value="" data-clear-btn="true" autocomplete="off" data-mini="true" type="password">
<div class="ui-grid-a">
<div class="ui-block-a"><a href="#" data-rel="close" class="ui-btn ui-shadow ui-corner-all ui-btn-b ui-mini">Cancel</a></div>
<div class="ui-block-b"><a href="#" data-rel="close" class="ui-btn ui-shadow ui-corner-all ui-btn-a ui-mini">Save</a></div>
</div>
</form>
</div><!-- /panel -->
</div><!-- /page -->
</body>
</html>
<?php
class Ajaxtest {
public function ajaxtest()
{
$data = array("name" => "");
$data["name"]="Anatol :-)";
echo json_encode($data);
}
}
{"name":"Anatol :-)"}{"name":"Anatol :-)"}if( data.name== "Anatol :-)" )
alert('Hi');
$f3->route('GET /',
function($f3) {
echo \Template::instance()->render('test.html');
}
);
// SET AN ASSOC NAME FIRST THAT YOU`RE JOSON RETURN CAN LOOL LIKE: {"name":"Anatol :-)"}
$f3->route('POST /ajaxtest',
function($f3) {
$arr['name'] = $f3->get('POST.name');
echo json_encode($arr);
}
);header("Content-Type: application/json", true);$f3->route('POST /ajaxtest',
function($f3) {
$arr['name'] = $f3->get('POST.name');
die(json_encode($arr));
}
);Think I am going to give up this.
<?php
class Ajaxtest {
function ajaxtest() {
$f3=Base::instance();
$data = array( "name" => "");
$data["name"]=$f3->get("POST.name");
die(json_encode($data));
}
}
?><div class="controls">
<input type="text" id="first_name" name="first_name" placeholder="firstname" class="form-control input-xlarge" value="{{ @POST.first_name }}" required autofocus />
</div>use Respect\Validation\Validator as v;
class EmployeeController extends Controller {
public function create()
{
if($this->f3->exists('POST.create'))
{
$employee = new Employee($this->db);
$this->f3->set('POST.created_by', $this->f3->get('SESSION.id'));
$employee->set('firstname',$this->f3->get('POST.firstname'));
$employeeValidator = v::attribute('firstname', v::stringType()->length(1,32));
if($employeeValidator->validate($employee)) {
$employee->add();
$this->f3->reroute('/employee');
} else { echo $this->f3->get('POST.firstname'); }
} else
{
$this->f3->set('page_head','Create');
$this->f3->set('view','employee/form.htm');
echo Template::instance()->render('layout.htm');
}
}
<?php
class Employee extends DB\SQL\Mapper {
public function __construct(DB\SQL $db) {
parent::__construct($db,'employees');
}
public function add() {
$this->copyFrom('POST');
$this->save();
}
....
}
public function update()
{
$employee = new Employee($this->db);
if($this->f3->exists('POST.update'))
{
$this->f3->set('POST.updated_by', $this->f3->get('SESSION.id'));
$data = $this->f3->get('POST');
$valid = Validate::is_valid($data, array(
'first_name' => 'required|valid_name',
'last_name' => 'valid_name',
'mobile' => 'phone_number',
'email' => 'valid_email',
'permenant_address'=> 'required|street_address',
'communication_address' => 'street_address',
'parent_name'=> 'valid_name',
'reference_name'=> 'valid_name',
'reference_number'=>'phone_number'
));
if($valid === true) {
$employee->edit($this->f3->get('POST.id'));
$this->f3->reroute('/employee');
} else {
$error = implode('. ',$valid);
\Flash::instance()->addMessage($error, 'warning');
$this->f3->set('page_head','Create');
$this->f3->set('view','employee/form.htm');
echo Template::instance()->render('layout.htm');
}
} else
{
$employee->getByid($this->f3->get('PARAMS.id'));
$this->f3->set('employee',$user);
$this->f3->set('page_head','Update');
$this->f3->set('view','employee/form.htm');
echo Template::instance()->render('layout.htm');
}
}if($valid === true) {
$employee->edit($this->f3->get('POST.id'));
$this->f3->reroute('/employee');
} else {
//$error = implode('. ',$valid);
foreach ($valid->get_errors_array() as $field=>$error) {
#Handle Your errors
\Flash::instance()->addMessage($error,'warning'); # Uncomment if you are using Flash
}
$this->f3->set('page_head','Create');
$this->f3->set('view','employee/form.htm');
echo Template::instance()->render('layout.htm');
}$error = strip_tags($error);EDIT: Problem solved by using PHP's own strip_tags() function before feeding to \Flash.