所以我有一张表格
<form id="login_form" method="post" action="login.php">
<input type="text" name="email">
<input type="password" name="pass">
<input type="submit" name="submit" value="Log in" onclick="submitFunction()">
</form>
提交表单时,我正在检查用户名和密码是否正确
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "SELECT email,password FROM user_details WHERE email='$email' AND password='$pass'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
}
else {
echo "Incorrect username or password. Please try again!";
}
}
但我也想加载一个新页面,我试图通过javascript
function submitFunction()
{
window.location.href = "new url";
}
我尝试用一个按钮替换提交按钮然后我无法让我的php执行因为我不能使用if(isset($ _ POST ['submit'])),但如果我使用提交按钮然后我不知道如何调用我的javascript函数因为我不能使用onclick可以吗?任何帮助将不胜感激:)
另外我知道我不应该只将密码存储在我的数据库和sql注入中以及所有这些,但我只是想尝试让它工作
答案 0 :(得分:2)
您可以在检查登录后使用PHP加载新页面。除非你有其他原因需要用JavaScript做,否则我觉得用PHP做这件事更有意义,因为根据登录是否成功,你会想做不同的事情。
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
header('Location: new url');
exit;
}
else {
echo "Incorrect username or password. Please try again!";
}
答案 1 :(得分:0)
不确定哪里你想要执行JS代码,如下所示:
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "SELECT email,password FROM user_details WHERE email='$email' AND password='$pass'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
// Output JsvaScript code here?
echo "
<script>
function submitFunction()
{
window.location.href = \"new url\";
}
</script>
";
}
else {
echo "Incorrect username or password. Please try again!";
}
}
这样的东西应该可以工作但是你的代码存在一些安全问题。我假设你只是搞乱和学习。
答案 2 :(得分:0)
这是Ajax的用武之地。我会使用AJAX和Jquery来执行此操作 别忘了包含Jquery。
$("#check").click(function(){
$.ajax({
type: "POST",
url: "file.php",
data: {email: $("#email").val(), pass: $("#pass").val()},
beforeSend: function()
{
$('#check').attr('disabled',true);
},
success : function(response)
{
if(response=="correct"){
setTimeout('window.location.href = "new url"',1000);
}else{
setTimeout('window.location.href = "bad login url"',1000);
}
}
});
return false;
});
&#13;
在您的Php代码文件中.php使用此
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$pass = $_POST['pass'];
$sql = "SELECT email,password FROM user_details WHERE email='$email' AND password='$pass'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION["email"] = $_POST['email'];
echo "correct";
}
else {
echo "Incorrect username or password. Please try again!";
}
}
&#13;
对于Html
<form id="login_form" method="post">
<input type="text" id="email" name="email">
<input type="password" id="pass" name="pass">
<input type="submit" name="submit" value="Log in" id="check">
</form>
&#13;
希望这些信息有所帮助 请学习Ajax,这对这些问题非常有帮助。谢谢