Skip to content
Advertisement

Why html page does not read the php page

I have a problem when I click on the submit button there is nothing happened and my data is not being inserted into the database.

I’m trying to make a registration page(register2.html) and db_reg.php to sent the data into the database.

but what I figure out that the Html page does not even read the PHP page.

db_reg.php

<?php
session_start();

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'mdff');

// initializing variables
$fullName = $_POST['fullName'];
$email    = $_POST['email'];
$phoneNum = $_POST['phoneNum'];
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
$errors = array(); 


// REGISTER USER
if (isset($_POST['register2'])) {
  // receive all input values from the form
  $fullName = mysqli_real_escape_string($db, $_POST['fullName']);
  $email = mysqli_real_escape_string($db, $_POST['email']);
  $phoneNum = mysqli_real_escape_string($db, $_POST['phoneNum']);
  $password1 = mysqli_real_escape_string($db, $_POST['password1']);
  $password2 = mysqli_real_escape_string($db, $_POST['password2']);

  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($fullName)) { array_push($errors, "Full Name is required"); }
  if (empty($email)) { array_push($errors, "Email is required"); }
  if (empty($phoneNum)) { array_push($errors, "Phone Number is required"); }
  if (empty($password1)) { array_push($errors, "Password is required"); }
  if (empty($password2)) { array_push($errors, "Password repeat is required"); }
  if ($password1 != $password2) {
    array_push($errors, "The two passwords do not match");
  }

  // first check the database to make sure 
  // a user does not already exist with the same username and/or email
  $user_check_query = "SELECT * FROM admin WHERE fullname='$fullName' OR email='$email' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);

  if ($user) { // if user exists
    if ($user['username'] === $username) {
      array_push($errors, "Username already exists");
    }

    if ($user['email'] === $email) {
      array_push($errors, "email already exists");
    }
  }

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
    $password = md5($password_1);//encrypt the password before saving in the database

    $query = "INSERT INTO admin (fullName, email, phoneNum, password1, password2) 
              VALUES('$fullName', '$email', '$phoneNum', '$password1', 'password2')";
    mysqli_query($db, $query);
    $_SESSION['fullName'] = $fullName;
    $_SESSION['success'] = "You are now registered";
    header('location: index.php');
  }
}

register2.html

<!doctype html>
<html class="no-js" lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Sign up MDFF</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" type="image/png" href="assets/images/icon/fire2.png">
</head>

<body>
    <!-- preloader area start -->
    <div id="preloader">
        <div class="loader"></div>
    </div>
    <!-- preloader area end -->
    <!-- login area start -->
    <div class="login-area login-s2">
        <div class="container">
            <div class="login-box ptb--95">
                <form name="register2" action="db_reg.php" method="POST">
                    <div class="login-form-head">
                        <h3>MONITORING & DETECTION FOREST FIRE</h3>
                        <br></br>
                        <h4>Sign Up</h4>
                    </div>
                    <div class="login-form-body">
                        <div class="form-gp">
                            <label for="Inputfullname">Full Name</label>
                            <input type="text" id="Inputfullname" name="fullname" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputEmail1">Email address</label>
                            <input type="email" id="exampleInputEmail1" name="email" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputPhone1">Phone Number</label>
                            <input type="text" id="exampleInputPhone1" name="phoneNum" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputPassword1">Password</label>
                            <input type="password" id="exampleInputPassword1" name="password1" required>

                        </div>
                        <div class="form-gp">
                            <label for="exampleInputPassword2">Confirm Password</label>
                            <input type="password" id="exampleInputPassword2" name="password2" required>

                        </div>
                        <div class="submit-btn-area">
                            <a href="#" class="btn btn-primary btn-md" role="button">Submit   </a>                
                        </div>
                        <div class="form-footer text-center mt-5">
                            <p class="text-muted">Do have an account? <a href="login2.html">Sign in</a></p>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- login area end -->
</body>
</html>

Advertisement

Answer

Your PHP script is waiting for $_POST["register2"] which never arrives.

Here -> make sure your submit button have a name="register2" attribute OR you could add a <input type="hidden" name="register2" somewhere inside your html form

<div class="submit-btn-area">
    <a href="#" class="btn btn-primary btn-md" role="button">Submit</a> 
</div>

I don’t use bootstrap, but I guess the mark-up above would be turned into a submit button, if that’s the case, you could add the name="register 2" attribute to it as follows

<div class="submit-btn-area">
    <a href="#" class="btn btn-primary btn-md" role="button" name="register2">Submit</a> 
</div>

EDIT At the top of your PHP script, you could check what your$_POST variable contains by calling var_dump($_POST) and see if your form data is present.

If they’re not present, you may need to change the markup above to a submit button as follows

<div class="submit-btn-area">
    <input type="submit" name="register2" value="Submit" class="btn btn-primary btn-md" role="button"> 
</div>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement