首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当提交按钮在另一个html文件中时运行php文件

当提交按钮在另一个html文件中时运行php文件
EN

Stack Overflow用户
提问于 2019-10-23 13:53:42
回答 2查看 804关注 0票数 2

我想在注册过程中工作,在谷歌和Youtube的帮助下,我已经创建了“登录和唱歌”页面与切换选项,但无法运行registration.php文件一旦用户提供注册信息在login.html文件。代码如下:

代码语言:javascript
复制
<form id="login" class="input-group">
    <input type="text" class="input-field" placeholder="User Id" required>
    <input type="password" class="input-field" placeholder="Enter Password" required>
    <input type="checkbox" class="check-box"><span>Remember Password</span>
    <button type="submit" class="submit-btn">Sign-In</button>
</form>

<form Id="register" class="input-group">
    <input type="text" class="input-field" placeholder="User Id" required>
    <input type="email" class="input-field" placeholder="Email Id" required>
    <input type="password" class="input-field" placeholder="Enter Password" required>
    <input type="password" class="input-field" placeholder="Confirm Password" required>
    <input type="phone-number" class="input-field" placeholder="Mobile Number" required>
    <input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
    <button type="submit" class="submit-btn">Sign-Up</button>
</form>

如何在login.html文件中点击注册按钮执行registration.php文件?登录选项也是如此。

EN

回答 2

Stack Overflow用户

发布于 2019-10-23 14:02:43

method属性指定如何发送表单数据(表单数据被发送到action属性中指定的页面)。

表单数据可以作为URL变量(使用method="get")或HTTP post事务(使用method="post")发送。

请查看此处了解更多详细信息w3schools

代码语言:javascript
复制
<form id="login" class="input-group" method="POST" action="file_path/login.php">
    <input type="text" class="input-field" placeholder="User Id" required>
    <input type="password" class="input-field" placeholder="Enter Password" required>
    <input type="checkbox" class="check-box"><span>Remember Password</span>
    <input type="submit" class="submit-btn" value="Sign-In">
</form>

<form Id="register" class="input-group" method="POST" action="file_path/register.php">
    <input type="text" class="input-field" placeholder="User Id" required>
    <input type="email" class="input-field" placeholder="Email Id" required>
    <input type="password" class="input-field" placeholder="Enter Password" required>
    <input type="password" class="input-field" placeholder="Confirm Password" required>
    <input type="phone-number" class="input-field" placeholder="Mobile Number" required>
    <input type="checkbox" class="check-box"><span>I agree to the terms & conditions</span>
    <input type="submit" class="submit-btn" value="Sign-Up">
</form>

根据您的评论进行编辑

将按钮替换为输入type="submit“,并将@放在php变量前,这样就不会收到未定义的错误通知(@用于避免错误通知)

代码语言:javascript
复制
<div class="header"> 
    <h2>Register here</h2> 
</div> 
<form method="post" action="register.php"> 
    <?php include('errors.php'); ?> 
    <div class="input-group"> 
        <label>Username</label> 
        <input type="text" name="username" value="<?php echo @$username; ?>"> 
    </div> 
    <div class="input-group"> 
            <label>Email</label> 
            <input type="email" name="email" value="<?php echo @$email; ?>"> 
        </div> 
        <div class="input-group"> 
            <label>Password</label> 
            <input type="password" name="password_1"> 
        </div> 
        <div class="input-group"> 
            <label>Confirm Password</label> 
            <input type="password" name="password_2"> 
        </div>


<div class="input-group"> 
    <label>Mobile number</label> 
    <input type="number" name="mobile" value="<?php echo @$mobile; ?>"> 
</div> 
<div class="input-group"> 
    <input type="submit" class="btn" name="reg_user" value="Sign-Up">
</div> 
</form>

在register.php中创建register.php文件

代码语言:javascript
复制
<?php
$con = mysqli_connect("localhost", "username", "password", "dbname") or trigger_error("Unable to connect to the database");
    if(isset($_POST['reg_user'])){
        $name = $_POST['username']; //here "username" is what you defined in the "name" field of input form
    //define other variables
        //write your own sql query , here is an example
        $query = "INSERT INTO table(name) VALUES(?)";
        $stmt = mysqli_stmt_init($con);
        if(!mysqli_stmt_prepare($stmt,$query)){
            echo "Error";
        }else{
            mysqli_stmt_bind_param($stmt,"s",$name);
            mysqli_stmt_execute($stmt);
        }
    }
?>
票数 0
EN

Stack Overflow用户

发布于 2019-10-23 14:55:45

确保在表单中添加method="POST"action="path/function.php"name="desiredName",如下所示:

代码语言:javascript
复制
<form id="login" class="input-group" method="POST" action="file_path/login.php">
    <input type="text" class="input-field" placeholder="User Id" name ="user" required>
    <input type="password" class="input-field" placeholder="Enter Password" name="password" required>
    <input type="checkbox" class="check-box"><span>Remember Password</span>
    <button type="submit" class="submit-btn">Sign-In</button>
</form>

然后在PHP中,为了“捕捉”post中的数据,您将使用如下代码:

代码语言:javascript
复制
$this->getpost['user'];

代码语言:javascript
复制
$_POST['user'];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58516281

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档