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

Time:09-10

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 >
        <div >
            <div >
                <span >Sign Up</span>

                <form method="POST" action="functions.php">
                    <div >
                        <input type="text" name="uname" placeholder="Enter your full name" required>
                        <i ></i>
                    </div>
                    <div >
                        <input type="email" name="email" placeholder="Enter your email" required>
                        <i ></i>
                    </div>
                    <div >
                        <input type="password"  name="password" placeholder="Create a password" required>
                        <i ></i>
                    </div>
                    <div >
                        <input type="password"  name="cpassword" placeholder="Confirm a password" required>
                        <i ></i>
                        <i ></i>
                    </div>

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

                    <div >
                        <input type="submit" value="Sign Up" name="submit">
                    </div>
                </form>

                <div >
                    <span >Already have an account?
                        <a href="#" >Login Now</a>
                    </span>
                </div>
            </div>
            <div >
                <span >Login</span>

                <form action="#">
                    <div >
                        <input type="email" placeholder="Enter your email" required>
                        <i ></i>
                    </div>
                    <div >
                        <input type="password"  placeholder="Enter your password" required>
                        <i ></i>
                        <i ></i>
                    </div>

                    <div >
                        <div >
                            <input type="checkbox" id="logCheck">
                            <label for="logCheck" >Remember me</label>
                        </div>

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

                    <div >
                        <input type="submit" value="Login" name="submit">
                    </div>
                </form>

                <div >
                    <span >Don't have an account?
                        <a href="#" >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.

CodePudding user response:

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

CodePudding user response:

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