简体   繁体   English

第二个提交按钮在PHP中不起作用

[英]Second submit button not working in php

I have been having problems with the second button not running like the first button. 我遇到了第二个按钮无法像第一个按钮一样运行的问题。 this is the code I have: 这是我的代码:

        <p>
            <form method="POST">
                <input placeholder="Username" type="text" name="username"><br /><br />
                <input placeholder="password" type="password" name="password"><br /><br />
                <input value="Login" type="submit" name="log_In">
            </form>
        </p>
    </div>
    <?php
        if(isset($_POST['log_In'])) {
            #$f_name = $_POST['fname'];
            #$s_name = $_POST['sname'];
            #$stud_Id = $_POST['studId'];
            #$uname = $_POST['uname'];
            #$pass = $_POST['pass'];
            #$rpass = $_POST['rpass'];
            #$email = $_POST['email'];
            #$remail = $_POST['remail'];

            #var_dump($f_name);

            header("Location:home.php");

        }
    ?>
</div>
<div align="right">
    <div>
        <p>
            <h2>Sign Up</h2>
        </p>
        <p>
            <form>
                <input placeholder="Forename" type="text" name="fname" id="Forename"><br /><br />
                <input placeholder="Surname" type="text" name="sname"><br /><br />
                <input placeholder="Student Id" type="text" name="studId"><br /><br />
                <input placeholder="Username" type="text" name="uname"><br /><br />
                <input placeholder="password" type="password" name="pass" min="6" max="32"><br /><br />
                <input placeholder="Re-type password" type="password" name="rpass" min="6" max="32"><br /><br />
                <input placeholder="Email" type="" name="email"><br /><br />
                <input placeholder="Re-type Email" type="remail" name="remail"><br /><br />
                <input value="Sign Up" type="submit" name="sign_Up">
            </form>
        </p>
    </div>
    <?php
        if(isset($_POST['sign_Up'])) {
            header("Location:home.php");
        }
    ?>
</div>

"if(isset($_POST['sign_up'])) {" is not being run and is just refreshing the page and removing all items from the form. “ if(isset($ _ POST ['sign_up'])){”未在运行,只是刷新页面并从表单中删除所有项目。

thanks 谢谢

By default <form> method is GET. 默认情况下, <form>方法是GET。 So if(isset($_POST['sign_Up'])) won't work. 因此, if(isset($_POST['sign_Up']))将不起作用。 Change it to if(isset($_GET['sign_Up'])) . 将其更改为if(isset($_GET['sign_Up']))

Or change your second form tag to: 或将第二个表单标签更改为:

<form method="POST">

Remember not to use header function after generating HTML content, move it to top! 请记住,生成HTML内容后不要使用标题函数,将其移到顶部!

header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. 在发送任何实际输出之前,必须先通过常规HTML标记,文件中的空白行或从PHP调用header()

So it will be better if it is like this: 因此,如果这样的话会更好:

<?php
        if(isset($_POST['log_In']) || isset($_POST['sign_Up'])) {
            header("Location:home.php");
        }
?>
<form method="POST">
    <input placeholder="Username" type="text" name="username">
    <br />
    <br />
    <input placeholder="password" type="password" name="password">
    <br />
    <br />
    <input value="Login" type="submit" name="log_In">
</form>

<div align="right">
    <div>
        <p>
             <h2>Sign Up</h2>
        </p>
        <p>
            <form method="post">
                <input placeholder="Forename" type="text" name="fname" id="Forename">
                <br />
                <br />
                <input placeholder="Surname" type="text" name="sname">
                <br />
                <br />
                <input placeholder="Student Id" type="text" name="studId">
                <br />
                <br />
                <input placeholder="Username" type="text" name="uname">
                <br />
                <br />
                <input placeholder="password" type="password" name="pass" min="6" max="32">
                <br />
                <br />
                <input placeholder="Re-type password" type="password" name="rpass" min="6" max="32">
                <br />
                <br />
                <input placeholder="Email" type="" name="email">
                <br />
                <br />
                <input placeholder="Re-type Email" type="remail" name="remail">
                <br />
                <br />
                <input value="Sign Up" type="submit" name="sign_Up">
            </form>
        </p>
    </div>
</div>

You forgot to add method="post" 您忘记添加method =“ post”

<form method="post">
            <input placeholder="Forename" type="text" name="fname" id="Forename"><br /><br />
            <input placeholder="Surname" type="text" name="sname"><br /><br />
            <input placeholder="Student Id" type="text" name="studId"><br /><br />
            <input placeholder="Username" type="text" name="uname"><br /><br />
            <input placeholder="password" type="password" name="pass" min="6" max="32"><br /><br />
            <input placeholder="Re-type password" type="password" name="rpass" min="6" max="32"><br /><br />
            <input placeholder="Email" type="" name="email"><br /><br />
            <input placeholder="Re-type Email" type="remail" name="remail"><br /><br />
            <input value="Sign Up" type="submit" name="sign_Up">
        </form>

在第二种形式中,您尚未定义方法,如果未定义method,它将接受默认的GET方法,因此可以通过以下方式更改第二种形式的标签:

<form action="" method="POST">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM