php 无论输入如何,表单都不起作用,php 脚本警报不起作用
php form not working no matter input, php script alert not working
所以我一直在搜索 "randomly redirects back to index.php" 和 "form not submitting correctly" 之类的问题,但是 none 的问题似乎解决了我的问题,所以我想也许我的问题是一个特定的问题。 (可能包含多个错误)。
基本上我有一个登录页面,如果用户名和密码与数据库中的任何一个都不匹配,我应该重定向到 register.php,如果用户名(电子邮件)都重定向到 index.php和密码匹配。
login.php
<?php
include "header.php";
?>
<!--follow from header.php-->
<div class="ui centered grid" id="loginbox">
<div class="ui inverted segment">
<form class="ui inverted form" action="login.php" method="post" enctype="multipart/form-data">
<div class="two fields">
<div class="required field">
<label>Username</label>
<input placeholder="Username" type="text" name="log_email" required/>
</div>
<div class="required field">
<label>Password</label>
<input placeholder="Password" type="text" name="log_password" required/>
</div>
</div>
<button class="ui submit button" name="login" type="submit"><a href="index.php">Log In</a></button>
<button class="ui button"><a href="register.php">Register</a> </button>
</form>
</div>
</div>
<!--continue to footer_form.php-->
<?php
if(isset($_POST['login'])){
$log_email = $_POST['log_email'];
$log_password = $_POST['log_password'];
$sel_log = "select * from customers where email ='$log_email' and password ='$log_password'";
$run_log = mysqli($mysqli,$sel_log);
$check_customer = mysqli_num_rows($run_log);
if($check_customer==0){
echo"<script>alert('Please register first!')</script>";
header("Location: register.php");
exit;
}
if($check_customer>0){
echo"<script>alert('Logged in successfully!')</script>";
header("Location: index.php");
exit;
}
}
include "footer_form.php" ?>
TLDR;
我的问题是:
1) 警报脚本似乎不适用于两种 IF 情况
2) 偶尔点击login.php页面上的"Log In",它停留在同一页面,有时会进入index.php,非常不一致,无论您输入用户名和密码.
数据库端所有命名和连接都是正确的。
编辑:
php表格问题已经被评论区下面的小伙伴解决了。我只是想与您分享一个解决脚本问题的答案。
if($check_customer==0){
echo"<script>alert('Please register first!');window.location='register.php'</script>";
exit;
}
使用上面的代码解决了这个问题,因为它似乎警报脚本和 header 不兼容,使用 window.location 优雅地解决了它。
您的提交按钮转到 index.php:
<button class="ui submit button" name="login" type="submit">
<a href="index.php">Log In</a>
</button>
删除锚点:
<button class="ui submit button" name="login" type="submit">Log In</button>
所以我一直在搜索 "randomly redirects back to index.php" 和 "form not submitting correctly" 之类的问题,但是 none 的问题似乎解决了我的问题,所以我想也许我的问题是一个特定的问题。 (可能包含多个错误)。
基本上我有一个登录页面,如果用户名和密码与数据库中的任何一个都不匹配,我应该重定向到 register.php,如果用户名(电子邮件)都重定向到 index.php和密码匹配。
login.php
<?php
include "header.php";
?>
<!--follow from header.php-->
<div class="ui centered grid" id="loginbox">
<div class="ui inverted segment">
<form class="ui inverted form" action="login.php" method="post" enctype="multipart/form-data">
<div class="two fields">
<div class="required field">
<label>Username</label>
<input placeholder="Username" type="text" name="log_email" required/>
</div>
<div class="required field">
<label>Password</label>
<input placeholder="Password" type="text" name="log_password" required/>
</div>
</div>
<button class="ui submit button" name="login" type="submit"><a href="index.php">Log In</a></button>
<button class="ui button"><a href="register.php">Register</a> </button>
</form>
</div>
</div>
<!--continue to footer_form.php-->
<?php
if(isset($_POST['login'])){
$log_email = $_POST['log_email'];
$log_password = $_POST['log_password'];
$sel_log = "select * from customers where email ='$log_email' and password ='$log_password'";
$run_log = mysqli($mysqli,$sel_log);
$check_customer = mysqli_num_rows($run_log);
if($check_customer==0){
echo"<script>alert('Please register first!')</script>";
header("Location: register.php");
exit;
}
if($check_customer>0){
echo"<script>alert('Logged in successfully!')</script>";
header("Location: index.php");
exit;
}
}
include "footer_form.php" ?>
TLDR; 我的问题是: 1) 警报脚本似乎不适用于两种 IF 情况 2) 偶尔点击login.php页面上的"Log In",它停留在同一页面,有时会进入index.php,非常不一致,无论您输入用户名和密码.
数据库端所有命名和连接都是正确的。
编辑: php表格问题已经被评论区下面的小伙伴解决了。我只是想与您分享一个解决脚本问题的答案。
if($check_customer==0){
echo"<script>alert('Please register first!');window.location='register.php'</script>";
exit;
}
使用上面的代码解决了这个问题,因为它似乎警报脚本和 header 不兼容,使用 window.location 优雅地解决了它。
您的提交按钮转到 index.php:
<button class="ui submit button" name="login" type="submit">
<a href="index.php">Log In</a>
</button>
删除锚点:
<button class="ui submit button" name="login" type="submit">Log In</button>