Run php file when submit button in another html file

Viewed 1259

I would like to work on registration process for which with the help of Google and Youtube i have created "Sign-in & Sing-up" page togather with toggle option however unable to run registration.php file once user provide registration info at login.html file. Codes are as follows :

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

How to execute registration.php file when Sign-up button clicked at login.html file? Same goes for login option too.

2 Answers

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).

The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").

check here for more details 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>

EDIT according to your comment

replace button with input type="submit" and place @ before the php variable so you wont get undefined error notice(@ is used to avoid error notice)


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

create register.php file and in 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);
        }
    }
?>

Make sure to add method="POST" and action="path/function.php" and name="desiredName"in your forms like this:

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

And then in PHP to "catch" the data from the post, you'll use something like this:

$this->getpost['user'];

Or

$_POST['user'];
Related