Reputation: 29
I am working on a php application using slim micro framework
That is my index.php file:
<?php
require 'Slim/Slim.php';
include 'db.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get("/", function () {
echo "<h1>HELLO USER</h1>";
});
//Registration view
$app->post("/reg", function (){
$request = $this->app->request();
$username = $request->post('username');
$password = $request->post('password');
$name = $request->post('name');
$email = $request->post('email');
try {
$sql = "INSERT INTO users (username, hash, name, email) VALUES (:username, :password, :name, :email)";
$s = $this->dbh->prepare($sql);
$s->bindParam("username", $username);
$s->bindParam("hash", $password);
$s->bindParam("name", $name);
$s->bindParam("email", $email);
$s->execute();
} catch(\PDOException $e) {
echo 'Exception: ' . $e->getMessage();
}
}); // Login Function End
$app->get('/updates', function () {
//Display users
});
// run the Slim app
$app->run();
?>
and i have index.html file with ajax cross domain request like this one:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>INDEX</title>
<link href='css/style.css' rel='stylesheet' type='text/css'/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#put").submit(function(event) {
event.preventDefault();
$.ajax({
url: 'http://localhost/slim/reg',
type: 'POST',
crossDomain: true,
data: $("#put").serializeArray(),
success: function(data) {console.log(data); }
});
});
});
</script>
</head>
<body>
<form id="put">
Registration <br/>
username: <input type="text" name="username" id="username"/><br />
password: <input type="password" name="hash" /><br />
name: <input type="text" name="name" /><br />
email: <input type="text" name="email" /><br />
<input type="submit" />
</form>
</body>
</html>
the problem is that when i try and submit the form in my index.html file i always get:
Fatal error: Using $this when not in object context in C:\wamp\www\slim\index.php on line 19
Upvotes: 0
Views: 3154
Reputation: 29
Solution: The full working index.php now look like:
<?php
require 'Slim/Slim.php';
include 'db.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->contentType('application/json');
$app->get('/', 'home');
$app->post('/adduser', 'addUser');
$app->get('/users', 'getUsers');
// Home view
function home() {echo "Welcome to the platform !";}
// Add users
function addUser() {
global $app;
$req = $app->request();
$sql = "INSERT INTO users (username, password, fullname, email) VALUES (:username, :password, :fullname, :email)";
try {
$db = getDB();
$stmt = $db->prepare($sql);
$stmt->bindParam("username", $_POST['username']);
$password = md5($_POST['password']);
$stmt->bindParam("password", $password);
$stmt->bindParam("fullname", $_POST['fullname']);
$stmt->bindParam("email", $_POST['email']);
$stmt->execute();
$db = null;
} catch(PDOException $e) {
echo "Connection Error";
}
}
// Get Users
function getUsers() {
$sql = "SELECT username,fullname,email FROM users ORDER BY id";
try {
$db = getDB();
$stmt = $db->query($sql);
$users = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo '{"users": ' . json_encode($users) . '}';
} catch(PDOException $e) {
//error_log($e->getMessage(), 3, '/var/tmp/php.log');
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
// run the Slim app
$app->run();
?>
Upvotes: 0
Reputation: 5221
Basically the problem is what @VolkerK mentioned.
If you want your closure method to know about variables defined outside the scope of your method you'll want to use the use
keyword.
$app->post("/reg", function () use ($app) {
$request = $app->request();
}
Upvotes: 1
Reputation: 96159
$request = $this->app->request();
This is most likely line #19 and it's not inside an instance method, not even within a class definition. So, there is no instance reference and therefore no $this.
What do you expect $this to be? Apparently you need an app
and an dbh
. What is providing those properties/instances?
app
could be $app = new \Slim\Slim();
. But what is dbh
supposed to be? (a database handle, ok, but what is providing this database connection handle?)
Upvotes: 1