0

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.

1 Answer 1

0

just add one one js

$(document).on('submit','#form_1',function(e){
    e.preventDefault();
    var frm = $('#form_1');
    $.ajax({
        type:'POST',
        url: 'loginCode.php?type=pdo',
        data:frm.serialize(),
        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:
            }
        },
        error: function(data){
            console.log(data);
        }
    });
})
$(document).on('submit','#form_2',function(e){
    e.preventDefault();
    var frm = $('#form_2');
    $.ajax({
        type:'POST',
        url: 'signupCode.php?type=pdo',
        data:frm.serialize(),
        success:function(data){
            switch($.trim(data)) {
                case "success1":
                        notification("success", "Registro completado");
                    break;
                case "failed1":
                        notification("failed", "Ha ocurrido un error");
                    break;
                default:
            }
        },
        error: function(data){
            console.log(data);
        }
    });
})
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');
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks! that whas very nice! I need to learn more ajax or js haha, thank you very much!!! u're the best dude.

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.