我想获取函数的返回值并将其显示为特定的id。
在我的Class.php中,我有一个名为login的函数,用于验证密码是否正确
<?php
class Class
{
public function login()
{
if($_POST['password'] == Match) {
return 'Correct Password!';
} else {
return 'Incorrect password!';
}
}
}
在我的index.php中我有这个HTML。现在如何在我的html标签中获取我的登录功能的返回值,其ID为check
<?php
require_once 'Class.php';
$class = new Class();
$class->login();
?>
<!DOCTYPE html>
<html>
<head>
<title>SOMETHING</title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<span id="check"></span> <!-- I want to put the returned value here -->
<input type="submit" value="Login">
</form>
</body>
</html>
答案 0 :(得分:1)
<?php
require_once 'Class.php';
$class = new Class();
$returnedValue = $class->login();
?>
<!DOCTYPE html>
<html>
<head>
<title>SOMETHING</title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<span id="check"><?= $returnedValue ?></span>
<input type="submit" value="Login">
</form>
</body>
</html>
答案 1 :(得分:0)
我还没有对下面的代码进行测试,但您可以使用此逻辑来实现目标。
您的index.php文件:
<!DOCTYPE html>
<html>
<head>
<title>SOMETHING</title>
</head>
<body>
<form class="loginform" action="" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<span id="check"></span>
<input type="submit" value="Login" class="Login">
</form>
</body>
</html>
您的Ajax脚本(使用jquery):
$(function() {
$("button.Login").click(function(){
$.ajax({
type: "POST",
url: "data.php",
data: $('form.loginform').serialize(),
success: function(msg){
$('#check').html(msg);
},
});
});
});
你的php文件接收数据(data.php):
<?php
require_once 'Class.php';
$class = new Class();
echo $class->login();
?>