此代码的问题是当我运行它并尝试在用户名字段中输入john并单击注册时,不会打印hello
。在每种情况下都会打印hello again
,这意味着数据不会发送到process.php文件。原因是$ .post无效
可能出现什么问题?
index.php文件
<!DOCTYPE html>
<html>
<head>
<title>Login Registration System</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<style>
label{
display:block;
}
input{
display:block;
}
</style>
<body>
<h1>Login Form</h1>
<form id="login">
<label>UserName</label>
<input type="text" id="username" name="username" placeholder="username" />
<label>Password</label>
<input type="password" id="pwd" name="password" placeholder="password"/>
<input type="submit" name="submit" value="Login"/>
</form>
<h1>Registration form</h1>
<form id="register">
<label>UserName</label>
<input type="text" id="username" name="username" placeholder="username" />
<label>Password</label>
<input type="password" id="pwd" name="password" placeholder="password" />
<label>Email</label>
<input type="email" id="email" name="email" placeholder="email" />
<input type="submit" name="submit" value="Register" />
</form>
<p id="paragraph"></p>
<script src="script.js"></script>
</head>
</body>
</html>
script.js文件
$(document).ready(function(){
$("#register").submit(function(){
var username=$("#username").val();
var pwd=$("#pwd").val();
var email=$("#email").val();
$.post("process.php",{username:username,pwd:pwd,email:email},function(data){
$("#paragraph").html(data);
});
return false;
});
});
process.php文件
<?php
if(isset($_POST['username']))
{
$username = $_POST['username'];
if($username == "john")
{
echo "hello";
}
else{
echo "hello again";
}
}
?>