0

I was trying to add a second form to a javascript/jquery script that I wrote, however, when i implemented and began testing it, when I would try to send the firm form via a jquery post request, it would instead send a get request. I know that this has to do with the second form, because commenting the script out for the second form makes the site work again. Hopefully a pair of fresh eyes can help!(the loadpage function works as well)

The register function seems to be where the problem is:

  //when the user first goes to the page
  $(document).ready(function(){ 
        var user = $("#username");
        var pass = $("#password");
        var submit = $("#submit");

            //both of these methods work, I'm not sure if one is better than the other
    //$("#submitbutton").click(function(event){
    $("#loginform").on('submit', function(event){
    event.preventDefault();
    $('.loginerror').remove();
    $.post("login.php", {username:user.val(), password:pass.val()}, function(data){

        if(!data)
        {
            $("#login").append("<h3 class='loginerror'>Incorrect Username or Password</h3>");

        }

        else
        {
        loadpage();
        }

    }, "json");

});

//if the user clicks the username prompt him with the login div
$("#registerbutton").click(function(event){
    register();
});

});

function register()
{
$("#login").hide("slow");
$("#registerdiv").css("display", "block");

//initiate some variables
var regusername = $("#regusername");
var reg1password = $("#reg1password");
var reg2password = $("#reg2password");
var regemail = $("#regemail");
var regfirstname = $("#regfirstname");
var reglastname = $("#reglastname");

//TODO: check to make sure form is filled out properly
$("#registerform").on('submit', function(event){

    event.preventDefault();
    //send post request
    $.post("register.php", {regusername:regusername.val(), password1:reg1password.val(), password2:reg2password.val(), email:regemail.val(), firstname:regfirstname.val(), lastname:reglastname.val()}, function(data){
        if(!data)
            $("#registerdiv").append("<h3> class='loginerror'>Server error, retry</h3>");
        else
            $("#registerdiv").hide("slow");
            $("#loginiv").slidedown("slow");
    }, "json");

}

And here's the HTML with the two forms:

<body>

<Div id="welcome"> Welcome </div>
<Div id="login">
    <br><br><h2>Welcome to QuotesLib</h2>
    <br><br><br><form id="loginform"> Username:
    <input type = "text" name = "username" id ="username"> <br> Password:
    <input type = "password" name = "password" id = "password"> <br>
    <input type = "submit" name="submitbutton" id="submitbutton" class = "bluebutton"> 
    </form>
    <form><br><br><br>Don't have an account yet?<br><button id="registerbutton" name="registerbutton" href="register.html">Register Now</button>    

</Div>
<Div id="registerdiv">
    <br><br><h2>Sign Up For QuotesLib</h2>
    <br><form id="registerform"> Username:
    <input type ="text" id="regusername"> <br> Password:
    <input type ="password" id="reg1password"> <br> Repeat Password:
    <input type ="password" id="reg2password"> <br> First Name:
    <input type ="text" id="regfirstname"> <br> Last Name:
    <input type ="text" id="reglastname"> <br> email:
    <input type ="text" id="regEmail"> <br>
    <input type ="submit" id ="registersubmitbutton">
    <br> </form>
</Div> 

It would be super helpful if someone could give me an answer to this question!

kinson
  • 23
  • 1
  • 3
  • Did you make sure nobody can just call loadpage()? Make sure you save some session info serverside. – Hidde Aug 18 '12 at 05:17

2 Answers2

0
$("#registerdiv").append("<h3> class='loginerror'>Server error, retry</h3>");

should be

$("#registerdiv").append("<h3 class='loginerror'>Server error, retry</h3>");

see the difference: <h3>... and <h3 ...

Vor
  • 33,215
  • 43
  • 135
  • 193
0

The code has some mistakes,

first,

<form><br><br><br>Don't have an account yet?<br><button id="registerbutton" name="registerbutton" href="register.html">Register Now</button>

You are having a form open tag but not a close tag, the form tag even not needed for this button, and that was the main reason for the problem

second, the end of the JS code,

}, "json");

}

should be

}, "json");
    });
}​

I'm not sure whether you missed while copying,

Third,

The <h3> tag within the append method, though it is not the matter for the problem you are having.

It is better to use some text editors which hi-lights the code open, end tags, braces.

Here the working code

code-jaff
  • 9,230
  • 4
  • 35
  • 56
  • @kinson I'm not using mac, to be frankly. just google. If you feel my answer made sense to question, accept it. http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – code-jaff Aug 18 '12 at 06:31