1

I created a register user system and seperate in two page. (registercontrol.php and register.php)

In register.php normally "Submit" button should send data to registercontrol.php but nothing happens when I click the Submit button. It's just refresh the page and do nothing, no data in members table. Am I missing something?

register.php

<?php 

require('../includes/config.php');

//if logged in redirect to members page
if ($user->is_logged_in() ){ 
    header('Location: ../dashboard/index.php'); 
    exit(); 
}

//define page title
$title = 'Demo';

//include header template
require('../layout/header.php');
?>

<input type="text" name="fullname" id="fullname" class="form-control form-control-user" placeholder="Your Name" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['fullname'], ENT_QUOTES); } ?>" tabindex="1" required>

<?php
    //check for any errors
    if (isset($infofn)){
       foreach ($infofn as $infofn){
            echo '<p class="p-3 text-info">'.$infofn.'</p>';
        }
    }                                   
?>

<input id="submit" type="submit" name="submit" value="Create Account" class="btn btn-primary btn-user btn-block" tabindex="6">

registercontrol.php

<?php

require('../includes/config.php');

if(isset($_POST['fullname'])){
    //fullname validation
    $fullname = $_POST['fullname'];

    if (! $user->isValidFullname($fullname)){
        $infofn[] = 'Your name must be alphabetical characters';
    }   
}

//if form has been submitted process it
if(isset($_POST['submit'])){

//if no errors have been created carry on
    if (!isset($infofn)){       

        try {

            //insert into database with a prepared statement
            $stmt = $db->prepare('INSERT INTO members (fullname) VALUES (:fullname)');
            $stmt->execute(array(
                ':fullname' => $fullname
            ));
            $id = $db->lastInsertId('memberID');
            

            //redirect to index page
            header('Location: register.php?action=joined');
            exit;

        //else catch the exception and show the error.
        } catch(PDOException $e) {
            $error[] = $e->getMessage();
        }
    }
}

1 Answer 1

3

You are missing a form element, with it's action and method attributes set to your other script and to be post. In this case action should be registercontrol.php. Without setting the action, the form submits to itself, which in this case is the register.php.

Something like this should solve the issue (added only first and last line, the rest is your code):

<form action="registercontrol.php" method="post">
    <input type="text" name="fullname" id="fullname" class="form-control form-control-user" placeholder="Your Name" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['fullname'], ENT_QUOTES); } ?>" tabindex="1" required>

    <?php
        //check for any errors
        if (isset($infofn)){
            foreach ($infofn as $infofn){
                echo '<p class="p-3 text-info">'.$infofn.'</p>';
            }
        }                                   
    ?>

   <input id="submit" type="submit" name="submit" value="Create Account" class="btn btn-primary btn-user btn-block" tabindex="6">
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.