【问题标题】:Run php file when submit button in another html file在另一个 html 文件中提交按钮时运行 php 文件
【发布时间】:2019-10-23 05:53:42
【问题描述】:

我想处理注册过程,在 Google 和 Youtube 的帮助下,我创建了带有切换选项的“登录和注册”页面,但是一旦用户提供注册信息,就无法运行 registration.php 文件在 login.html 文件中。代码如下:

<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 文件?登录选项也是如此。

【问题讨论】:

  • 使用&lt;form action="path_to/registration.php" method="get"&gt;这个。
  • 我建议阅读&lt;form&gt;,特别是actionmethod 属性

标签: php html toggle execute


【解决方案1】:

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

<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 中“捕获”帖子中的数据,您将使用如下内容:

$this->getpost['user'];

或者

$_POST['user'];

【讨论】:

  • 感谢 Mathall 关注了这个
    不知道在哪里放置 $_POST ['用户'];在 register.php 文件中。什么是“用户”?现在提交 register.php 页面打开后,没有在数据库中保存用户详细信息。似乎仍然缺少链接。请帮忙。
  • @RaviPrakashShukla "user" 在这种情况下是您从 html 表单向后端 PHP 发布用户 ID 的字段的“名称”,您必须确保它保存在当然还有数据库等,但这是另一个问题
【解决方案2】:

method属性指定如何发送form-data(form-data发送到action属性中指定的页面)。

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

点击这里了解更多详情w3schools

<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>

根据您的评论进行编辑

用输入类型=“提交”替换按钮 并将@放在php变量之前,这样您就不会收到未定义的错误通知(@用于避免错误通知)


<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

<?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);
        }
    }
?>

【讨论】:

  • 干得好!不需要转义,所以我将其删除。您还应该避免检查prepare() 的返回值。而是启用错误报告。
猜你喜欢
  • 1970-01-01
  • 2022-01-11
  • 2012-01-23
  • 2014-07-03
  • 2014-04-05
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 2018-03-19
相关资源
最近更新 更多