PHP表单不工作,无论输入,PHP脚本警报不工作

php form not working no matter input, php script alert not working

本文关键字:PHP 工作 脚本 表单 输入      更新时间:2023-09-26

所以我一直在寻找像"随机重定向回index.php"answers"表单不正确提交"这样的问题,但没有一个问题似乎能解决我的问题,所以我想也许我的问题是一个特定的。(可能包含不止一个错误)。

基本上我有一个登录页面,应该重定向到register.php如果用户名和密码不匹配的任何那些在数据库中,并重定向到index.php如果用户名(电子邮件)和密码匹配。

login。

<?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)警报脚本似乎并不适用于这两种情况2)偶尔当点击login.php页面上的"登录"时,它会停留在同一页面上,有时它会转到index.php,非常不一致,无论您输入的用户名和密码如何。

数据库端所有命名和连接正确。

编辑:php表单问题已经被下面的朋友在评论区解决了。我只是想和大家分享一个解决脚本问题的答案。

    if($check_customer==0){
    echo"<script>alert('Please register first!');window.location='register.php'</script>";
    exit;
}

使用上面的代码解决了这个问题,因为它似乎警告脚本和头是不兼容的,使用窗口。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>