I'm working on a project. (web)
The first version, has a separate login.php and register.php which login calls a .js to do an ajax validation. the same happends with register.
The second version, the page have a login and register form in just one page like this:
Login and register in one page
The problem is in this new page version, I import (
(for better understanding, In the image enclosed yellow)
My first idea was put a code to ejecute every .js separate(one .js for login and one .js for register). like this:
if( $_POST['submit_login'] ) {
echo "<script src='js/login.js'></script>";
}
else if( $_POST['submit_register'] ) {
echo "<script src='js/register.js'></script>";
}
but the code above does: the first click import the .js, then at the second click to log in/register, load correctly.
The optimal idea is to load every .js where appropriate. The .js at just one click like all sites.
The code for login.js is:
$(function() {
$("form").submit(function(e) {
var datos = $("form").serialize();
$.ajax({
method: "POST",
url: "loginCode.php?type=pdo",
data: datos,
success: function(data) {
switch($.trim(data)) {
case "success":
notification("success", "Usuario válido");
$(location).attr('href','index.php');
break;
case "failed":
notification("failed", "Usuario no válido");
break;
default:
}
}
})
e.preventDefault();
});
});
function notification(type, text) {
$notify = $("<div class='notification " + type + "'></div>").appendTo("#notification_wrapper");
$notify.append("<div class='errorImage'></div><div class='info'>" + text + "</div>");
$notify.fadeIn().delay(3000).fadeOut('fast');
}
And the code for register.js:
$(function() {
$("form").submit(function(e) {
var datos = $("form").serialize();
$.ajax({
method: "POST",
url: "signupCode.php?type=pdo",
data: datos,
success: function(data) {
switch($.trim(data)) {
case "success1":
notification("success", "Registro completado");
break;
case "failed1":
notification("failed", "Ha ocurrido un error");
break;
default:
}
}
})
e.preventDefault();
});
});
function notification(type, text) {
$notify = $("<div class='notification " + type + "'></div>").appendTo("#notification_wrapper");
$notify.append("<div class='errorImage'></div><div class='info'>" + text + "</div>");
$notify.fadeIn().delay(3000).fadeOut('fast');
}
The html form login is quite normal like this:
<form id="form_1" action="" method="post">
<input type="submit" name="submit_login" value="Submit" />
</form>
<form id="form_2" action="" method="post">
<input type="submit" name="submit_register" value="Submit" />
</form>
Hope u can help me, I was trying to do that 1 week ago but this wins me. Sorry if my english is not so good. Thanks u in advance.