我在一個PHP應用程序中使用超薄微架構
這是我的index.php文件的工作:
<?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();
?>
和我有使用Ajax跨域請求index.html文件,像這樣的:
<!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>
的問題是,當我嘗試提交表單在我的index.html文件我總是得到:
Fatal error: Using $this when not in object context in C:\wamp\www\slim\index.php on line 19
你嘗試刪除'$本 - >' –
我做了19行是這樣的:$請求= $ app-> request(); 我在控制檯出現錯誤: 500內部服務器錯誤 – YoRk404