【发布时间】:2016-06-13 19:54:03
【问题描述】:
我正在尝试从 HTML 表单 -> ajax -> 实例 -> oop 类文件发送包含用户名、密码等的数据。
但我不确定我的方法是否正确......
它以 index.php
上的表单开头 <!-- Formular for signing up -->
<form method="post">
<div class="form-group">
<label> Username </label>
<input type="text" class="form-control" name="newusername">
</div>
<div class="form-group">
<label> Password </label>
<input type="password" class="form-control" name="newpassword">
</div>
<div class="form-group">
<label> Your club </label>
<input type="text" class="form-control" name="newclub">
</div>
<input type="button" id="btn-reg" class="btn btn-success" value="Sign up!">
</form>
然后它通过我的脚本文件和 ajax
$(document).ready(function () {
console.log('Script loaded...');
$("#btn-reg").on("click", reg);
// Function for registrate of new users
function reg(newusername, newpassword, newclub) {
$.post('classCalling.php', {
newusername: 'newusername',
newpassword: 'newpassword',
newclub: 'newclub'
});
};
});
然后我的数据将转到一个页面,classCalling.php 我在其中实例化我的类
?php
include("class/userClass.php");
include("class/pagesClass.php");
// Creating instance of the class userClass.php
$user = new User();
// Defining variables
$newusername = $_POST['newusername'];
$newpassword = $_POST['newpassword'];
$newname = $_POST['newclub'];
// Password hash
$hashpassword = sha1($newpassword);
$user->newUsers($newusername, $hashpassword, $newname);
?>
最后是我的 OOP 课程,但我还没到这一步
public function newUsers($newusername, $newpassword, $newclub) {
// Using prepared statement to prevent mysql injections.
$stmt = $this->db->prepare("INSERT INTOusers(username,password, club)VALUES(?, ?, ?);");
$stmt->bind_param("sss", $newusername, $newpassword, $newclub);
if($stmt->execute()) {
echo "<h3 class='usercreated'>Användare skapad</h3>";
} else {
echo "<h3 class='usercreated'> Gick ej att skapa användare</h3>";
}
}
我收到这些错误通知:未定义索引:第 13 行 /Applications/MAMP/htdocs/web 2.0/projektet/classCalling.php 中的新用户名
【问题讨论】:
-
你也错过了 probel : "INSERT INTO users(username,pa"
-
该问题与 OOP 无关。您必须研究数据流。
-
将
INSERT INTOusers(更改为INSERT INTO users( -
尝试在您的
classCalling.php中使用var_dump($_POST);打印$_POST
标签: javascript php jquery ajax oop