use PHP function in <input type='submit'> from functions.php to other file

Viewed 22

I have two files

  1. functions.php
<?php

include 'config.php';

function signup(){

    if (isset($_POST['submit'])) {

        $uname = $_POST['uname'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $cpassword = $_POST['cpassword'];

        if($password == $cpassword) {

            $hash = md5($password);

            $insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";

            $result = mysqli_query($con, $insert);

            if ($result) {
                echo '<script>alert("Your account has been successfully created.")</script>';
            }
        }
        else {
            echo '<script>alert("Passwords do not match!")</script>';
        }
    }
}
?>
  1. signup.php
<?php
    include 'functions.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <!-- ===== Iconscout CSS ===== -->
    <link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">

    <!-- ===== CSS ===== -->
    <link rel="stylesheet" href="css/credential.css">

    <title>Sing Up</title>
</head>
<body>
    <div class="container">
        <div class="forms">
            <div class="form signup">
                <span class="title">Sign Up</span>

                <form method="POST" action="functions.php">
                    <div class="input-field">
                        <input type="text" name="uname" placeholder="Enter your full name" required>
                        <i class="uil uil-user"></i>
                    </div>
                    <div class="input-field">
                        <input type="email" name="email" placeholder="Enter your email" required>
                        <i class="uil uil-envelope icon"></i>
                    </div>
                    <div class="input-field">
                        <input type="password" class="password" name="password" placeholder="Create a password" required>
                        <i class="uil uil-lock icon"></i>
                    </div>
                    <div class="input-field">
                        <input type="password" class="password" name="cpassword" placeholder="Confirm a password" required>
                        <i class="uil uil-lock icon"></i>
                        <i class="uil uil-eye-slash showHidePw"></i>
                    </div>

                    <div class="checkbox-text">
                        <div class="checkbox-content">
                            <input type="checkbox" id="termCon">
                            <label for="termCon" class="text">I accepted all <a href="#">Terms and Conditions</a>, <a href="#">Privacy Policy</a> and <a href="#">Cookie Policy</a></label>
                        </div>
                    </div>

                    <div class="input-field button">
                        <input type="submit" value="Sign Up" name="submit">
                    </div>
                </form>

                <div class="login-signup">
                    <span class="text">Already have an account?
                        <a href="#" class="text login-link">Login Now</a>
                    </span>
                </div>
            </div>
            <div class="form login">
                <span class="title">Login</span>

                <form action="#">
                    <div class="input-field">
                        <input type="email" placeholder="Enter your email" required>
                        <i class="uil uil-envelope icon"></i>
                    </div>
                    <div class="input-field">
                        <input type="password" class="password" placeholder="Enter your password" required>
                        <i class="uil uil-lock icon"></i>
                        <i class="uil uil-eye-slash showHidePw"></i>
                    </div>

                    <div class="checkbox-text">
                        <div class="checkbox-content">
                            <input type="checkbox" id="logCheck">
                            <label for="logCheck" class="text">Remember me</label>
                        </div>

                        <a href="#" class="text">Forgot password?</a>
                    </div>

                    <div class="input-field button">
                        <input type="submit" value="Login" name="submit">
                    </div>
                </form>

                <div class="login-signup">
                    <span class="text">Don't have an account?
                        <a href="#" class="text signup-link">Signup Now</a>
                    </span>
                </div>
            </div>

        </div>
    </div>

    <script src="js/credential.js"></script>

</body>
</html>

I want something like this... when I click on <input type="submit" of signup the signup() function from functions.php should work. But I don't know how to do it.

If I remove function signup(){} from functions.php and try without function then in url signup.php is replaced by functions.php and page is blank and no data is inserted in mysql localhost.

In 'config.php' file

<?php
    $con = mysqli_connect("localhost","root","","get-viewed");
?>

Database name, Table name and field name are perfect I have double checked it.

2 Answers

The form action correctly point to function.php and the webserver execute it.

The result is blank because nothing in function.php get executed.

you defined function signup() but you don't call it

add signup(); as last code line, just before php closing tag ?>

Note 1: you can extract the code from the signup function, since it does not add any advantage.

Note 2: if the php closing tag is the last code line in the file (no html follow) you should omit, it is a good practice to avoid unwanted output.

This is a must once you start to use frameworks, otherwise header errors will popup

Thanks for helping me I have solved my question. I updated functions.php

<?php

include 'config.php';

function signup() {

        $uname = $_POST['uname'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $cpassword = $_POST['cpassword'];

        if($password == $cpassword) {

            $hash = password_hash($password, PASSWORD_DEFAULT);

            $insert = "INSERT INTO `users`(`user_name`, `email`, `password`) VALUES ('$uname','$email','$hash')";

            $result = mysqli_query($con, $insert);

            if ($result) {
                echo '<script>alert("Your account has been successfully created.")</script>';
            }
        }
        else {
            echo '<script>alert("Passwords do not match!");location.replace("signup.php");</script>';
        }
    }

function login(){

    if (isset($_POST['login'])) {
        echo '<script>alert("login")</script>';
    }
}

if (isset($_POST['signup'])) {
    signup();
}
else {
    login();
}

Now it is working perfectly as I wanted.

Related