Javascript Ajax和php,生成一个div
我想在“result”div中显示服务器页面的结果。 这是我的计划: home.phpJavascript Ajax和php,生成一个div,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我想在“result”div中显示服务器页面的结果。 这是我的计划: home.php <script src="myscripts.js"></script> <div id="loginform"> <div id="result"></div> <?php include_once 'core.login.php'; ?> </div> <?php // stuff about
<script src="myscripts.js"></script>
<div id="loginform">
<div id="result"></div>
<?php include_once 'core.login.php'; ?>
</div>
<?php
// stuff about $db variable e other stupid things like $tool = new Tools etc...
$email = $_POST["email"];
$password = $_POST["password"];
// Initializing login process
if (isset($email) or isset($password)) {
$tool->decode($email, $password, $db);
}
?>
<form action="core.login.php" method="POST" id="login">
<fieldset>
<legend>Data login:</legend>
Email:<input id="email" type="email" name="email" placeholder="someone@example.com" required>
<br>
Password:<input id="password" type="password" name="password" required>
<br>
<input type="submit" value="Submit">
</fieldset>
</form>
public function decode($email, $password, $db) {
if ($stmt = $db->prepare("SELECT password FROM users WHERE email = ?")) {
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($password_db);
$stmt->fetch();
if ($stmt->num_rows == 1) {
if ($password == $password_db) {
echo "Success";
} else {
echo "Error";
}
} else {
echo "Email didn't found!";
}
}
}
core.login.php
<script src="myscripts.js"></script>
<div id="loginform">
<div id="result"></div>
<?php include_once 'core.login.php'; ?>
</div>
<?php
// stuff about $db variable e other stupid things like $tool = new Tools etc...
$email = $_POST["email"];
$password = $_POST["password"];
// Initializing login process
if (isset($email) or isset($password)) {
$tool->decode($email, $password, $db);
}
?>
<form action="core.login.php" method="POST" id="login">
<fieldset>
<legend>Data login:</legend>
Email:<input id="email" type="email" name="email" placeholder="someone@example.com" required>
<br>
Password:<input id="password" type="password" name="password" required>
<br>
<input type="submit" value="Submit">
</fieldset>
</form>
public function decode($email, $password, $db) {
if ($stmt = $db->prepare("SELECT password FROM users WHERE email = ?")) {
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($password_db);
$stmt->fetch();
if ($stmt->num_rows == 1) {
if ($password == $password_db) {
echo "Success";
} else {
echo "Error";
}
} else {
echo "Email didn't found!";
}
}
}
没有AJAX如果我正常启动代码,它会给我正确的响应结果(成功或错误),但是当我使用AJAX时,什么也不会发生
更新
好了,伙计们,问题是操作url,我的核心文件在core/core.login.php文件夹中,现在它在result div中显示页面,但是页面核心现在显示以下内容:
致命错误:对第9行网站中的非对象调用成员函数decode()
也许ajax没有像object那样传递变量?请在js文件中尝试:
(Or Document)
$('#loginform').on('submit', '#login', function() {
//AJAX etc..
})
而不是:
$('#login').submit(function() {
//AJAX etc..
}
试试这个js
$(document).ready(function() {
$('#login').submit(function() { // catch the form's submit event
$.ajax({ // create an AJAX call...
data: $("#login").serialize(), // get the form data
type: $(#login).attr('method'), // GET or POST
url: $(#login).attr('action'), // the file to call
success: function(response) { // on success..
$('#result').html(response); // update the DIV
}
});
return false; // cancel original event to prevent form submitting
});
});
你检查控制台有没有错误?观察控制台时,在请求/响应过程中会发生什么。将
error
处理程序添加到ajax调用中,查看错误消息是什么。我认为不会发生任何事情。控制台可能在说些什么。我不太了解ajax的家伙们,我如何检查控制台的错误呢^^“他们正在讨论浏览器中的开发人员控制台。您是否从类中将$tool设置为类。php???$tool=new class()
错误是:在第8行对网站中的非对象调用成员函数decode()