Mickey B Mickey B - 3 months ago 6
HTML Question

PHP code submit button only refreshing page

I am working with PHP and an SQL server, before I explain too much farther, the database connection is implemented with the

require 'databaseconnect.php';
The code is meant to register the user. Currently, running the code only refreshes the page, and after spending hours attempting to troubleshoot, I can still not find the error. Any help would be much appreciated. I do not know if I programmed it correctly, so that also may be a source of the problem. My code is as follows:

<?php
session_start();
$message = "";
require 'databaseconnect.php';
if($_POST['register']):
if(!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['confirmPassword']) && !empty($_POST['pin']) && !empty($_POST['preferredName'])):
if($_POST['password'] == $_POST['confirmPassword']):
$sql = "INSERT INTO Users (Email, Password, FirstName, LastName, Type) VALUES (:email, :password, :firstname, :lastname, :type)";
If($_POST['pin'] == "123456"):
$stmt = $conn->prepare($sql);
$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':password', md5($_POST['password']));
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':lastname', $_POST['firstname']);
$stmt->bindParam(':type', $_POST['AcctType']);

if( $stmt->execute() ):
$message = ('<div class="inputlogin alert alert-success" role="alert"><b>You are Registered!</b> You have been registered as a member! <a href = portal.php>Log In!</a></div>');
else:
$message = ('<div class="inputlogin alert alert-danger" role="alert"><b>There has been an issue...</b> While attempting to register you, there was an error with the server,try again later.</a></div>');
endif;
else:
$message = ('<div class="inputlogin alert alert-danger alert-dissmissable" role="alert"><b>Oops!</b> Authentication pin not recognized!</div>');
endif;
else:
$message = ('<div class="inputlogin alert alert-danger alert-dissmissable" role="alert"><b>Uh Oh!</b> Passwords Do not Match!</div>');

endif;

endif;
endif;
$conn = null;
?>
<html>
<head>
</head>
<body>
<div class = "container_main">

<a name="top"></a>

<br><br>
<div class = "mainHeader">
<i class = "fa fa-bars fa-2x mobile_menu" height="100px" widht="100px"></i>


</div>

<div class = "containerDEF">
<center><img class = "fixed loginImg" src = "CSS/images/TroopLogo.png" alt = "DesignLogo" width = "300" height="300px"></center>
<form action = "register.php" method ="POST">
<div class="input-group inputLogIn" id = "FirstName">
<span class="input-group-addon" id="basic-addon1">First Name:</span>
<input type="text" class="form-control" placeholder="ex. Clara" aria-describedby="basic-addon1" name="firstname">
</div>
<div class="input-group inputLogIn" id = "LastName">
<span class="input-group-addon" id="basic-addon1">Last Name:</span>
<input type="text" class="form-control" placeholder="ex. Oswald" aria-describedby="basic-addon1" name="lastname">
</div>
<div class="input-group inputLogIn" id = "Email">
<span class="input-group-addon" id="basic-addon1">Email:</span>
<input type="text" class="form-control" placeholder="ex. cOswald@kyzlet.net" aria-describedby="basic-addon1" name="email">
</div>
<div class="input-group inputLogIn" id = "ConfirmEmail">
<span class="input-group-addon" id="basic-addon1">Confirm Email:</span>
<input type="text" class="form-control" placeholder="ex. cOswald@kyzlet.net" aria-describedby="basic-addon1" name="confirmemail">
</div>
<div class="input-group inputLogin" id = "Pass">
<span class="input-group-addon" id="basic-addon1">Password:</span>
<input type="password" class="form-control" placeholder="ex. RunYouCleverBoy" aria-describedby="basic-addon1" name="password">
</div>
<div class="input-group inputLogin" id = "ConfirmPass">
<span class="input-group-addon" id="basic-addon1">Confirm Password:</span>
<input type="password" class="form-control" placeholder="ex. RunYouCleverBoy" aria-describedby="basic-addon1" name="confirmpassword">
</div>
<div class="input-group inputLogin" id = "RegistrationPin">
<span class="input-group-addon" id="basic-addon1">Registration Pin:</span>
<input type="password" class="form-control" placeholder="******" aria-describedby="basic-addon1" name="pin">
</div>
<div class="input-group inputLogin" id = "RegistrationPin">
<span class="input-group-addon" id="basic-addon1">Account Type:</span>
<select type = "select" name="AcctType" class = "form-control" >
<option value="Scout">Scout</option>
<option value="Parent">Parent</option>
<option value="Leader">Leader</option>
</select>

</div>

<?php
if (!empty($message)):
echo ($message);
endif;
?>
<center><input type = "submit" class = "submitButton" name = "register" value = "Register"></center>

</form>
<center><p>Just want to login? <a href = "portal.php" class = "lightLink" >Login Here</a>. </p>
</center>
</div>
<br><br>




<a href = "#top"><i class = "fa fa-arrow-up up_button fa-2x"></i></a>
</div>




</div>
</div>
<script src = "Scripts/Script_Main.js"></script>

</body>
</html>

Answer

Does $_POST['register'] even come back as true? I believe you need to set it up like this:

if(isset($_POST['register')){
 //do your thing
}

This should also be the case in the first place, to prevent any warnings because $_POST['register'] will not exist on first load.

Also, the fact that you don't have it probably means you turned errors and warnings off? To try this out, please put ini_set('error_reporting', E_ALL); on top of your script. If you have any errors, this should make it clear.

Also, make sure to echo something within your if statement! You don't know whether the page just refreshes or not because if something is wrong within the code for databasing it might never get to setting the $message.

Hope this helps!

Also, I noticed your form action is set to a static page (leaving it empty will automatically use the current page) -- when I was a beginner, I sometimes made the mistake of using a new file to test something out, only to forget to change the action url. This sometimes resulted in me loading in the old (back-up) file instead of the new one with the new testing functions. Just saying.

Comments