I'm trying to make a login and signup form in the same popup window and the problem is, when I press login
button or signup
button, it just closes the popup window.
I want the popup window to stay open even if login
or signup
button is clicked. And I want it to display error or success message within the popup window. The error message does show up in the popup window if there is any error. But the popup window closes after submit, therefore, I have to reopen the popup window and check if there is any error message.
Also, I made a popup window in the header. And the popup window is only visible when show login
button is clicked.
header.php
<input type="button" id="show_login" value="Show Login">
<?php
if(isset($_POST['loginSubmit'])){
$username = $_POST['username1'];
$password = $_POST['password1'];
if(isset($_POST['username1'])){
$_SESSION['username1'] = $username;
header('Location: index.php');
exit;
} else {
$error[] = 'Wrong username or password';
}
}
?>
<div id="popupLogin">
<div class="x">CLOSE</div>
<form id="loginForm" name="login" action="#" method="post">
<div>LOGIN</div>
<div>
<?php
if(isset($error)){
foreach($error as $error){
echo '<p>'.$error.'</p>'; }
}
?>
</div>
<input class="c" name="username1" type="text" placeholder="Username" value="<?php if(isset($error)){ echo $_POST['username1']; } ?>" tabindex="1" />
<input class="c" name="password1" type="password" placeholder="Password"/>
<button id="btnloginSubmit" type="submit" name="loginSubmit">Log in</button>
<div id="btnSignup">Sign Up Here</div>
</form>
<?php
if(isset($_POST['signupSubmit'])){
if(strlen($_POST['username']) < 3){
$error[] = 'Username is too short.';}
if(strlen($_POST['password']) < 3){
$error[] = 'Password is too short.';}
if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$error[] = 'Please enter a valid email address';}
}
?>
<form id="signupForm" name="registration" method="post" action="#">
<div>
<?php
if(isset($error)){
foreach($error as $error){
echo '<p>'.$error.'</p>';}
}
?>
</div>
<input class="c" name="email" type="text" placeholder="Email" value="<?php if(isset($error)){ echo $_POST['email']; } ?>" />
<input class="c" name="username" type="text" placeholder="username" value="<?php if(isset($error)){ echo $_POST['username']; } ?>" />
<input class="c" name="password" type="password" placeholder="Password"/>
<button name="signupSubmit" type="submit">Sign up</button>
<div id="btnLogin">Log in Here</div>
</form>
index.php
<?php include_once('header.php'); ?>
<body>
<p>Welcome!</p>
<script src="jquery-3.1.1.min.js"></script>
<script src="script.js"></script>
</html>
script.js
$(document).ready(function()
{
$("#show_login").click(function(){
showpopup();
});
$(".x").click(function(){
hidepopup();
});
$("#btnSignup").click(function(){
$("#signupForm").css({"visibility":"visible","display":"block"});
$("#loginForm").css({"visibility":"hidden","display":"none"});
});
$("#btnLogin").click(function(){
$("#loginForm").css({"visibility":"visible","display":"block"});
$("#signupForm").css({"visibility":"hidden","display":"none"});
});
});
function showpopup()
{
$("#popupLogin").fadeIn();
$("#popupLogin").css({"visibility":"visible","display":"block"});
$("#signupForm").css({"visibility":"hidden","display":"none"});
}
function hidepopup()
{
$("#popupLogin").fadeOut();
$("#popupLogin").css({"visibility":"hidden","display":"none"});
$("#loginForm").css({"visibility":"visible","display":"block"});
//the code below does not work
$('#signupForm').submit(function(e) {
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax({
url: formURL,
type: "POST",
data: postData,
success: function(data) {
console.log('success!')
}
});
e.preventDefault();
});
}