?
Solved

Submit form via jQuery

Posted on 2011-10-11
2
Medium Priority
?
237 Views
Last Modified: 2012-05-12
Im trying to post some form data from one page to another (simple), however in my form which retrives the form data says there is no data coming through, however it is being called.

To try and debug I put the line 'alert($("#frmLogin").serialize());' to hopefully see the data going across, but it just gives me nothing.

			<div style="width:300px; text-align:left;">
				<form action="/" id="frmLogin" action="POST">
				<div style="width:150px; float:left;"><input type="text" id="txtUsername" name="txtUsername" /></div>
				<div style="width:150px; float:left;"><input type="password" id="txtPassword" name="txtPassword" /></div>
				<div style="width:300px; float:left; text-align:center;">
					<div id="divLoginMessage" style="width:300px; float:left; text-align:centre;"></div>
					<div style="width:50px; float:left;"></div>
					<div id="btnLogin" style="width:200px; float:left; text-align:center; background-color:#C8C8C8; cursor:pointer;" onClick="Javascript:attemptLogin();">Login</div>
						
						<script type="text/javascript">
							function attemptLogin() {
								if ($("#btnLogin").text() == "Login")
									{
									$("#txtUsername").attr("disabled", "disabled");
									$("#txtPassword").attr("disabled", "disabled"); 
									$("#btnLogin").text("Logging in....");
									alert($("#frmLogin").serialize());
									$.post("attemptLogin.php", $("#frmLogin").serialize(), 
									function(data) {
										if (data.substring(0,15) == "Login Succeeded")
											{
											window.location.href = "loggedIn.php?AuthID=" + data.substring(16)
											}
										else
											{
											$("#divLoginMessage").html(data);
											$("#txtUsername").removeAttr("disabled");
											$("#txtPassword").removeAttr("disabled"); 
											$("#btnLogin").text("Login");											
											}										
									});
									}
							}
						</script>
					<div style="width:50px; float:left;"></div>
				</div>
				</form>
			</div>

Open in new window


Any ideas what Im doing wrong?
0
Comment
Question by:tonelm54
  • 2
2 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36947810
Because disabled field are not serialized
disable them after the $.post (don't worry, it's an asynchronous ajax call so th code don't wait the end of the ajax call before disable the fields)

test page : http://jsfiddle.net/3aBd4/

0
 
LVL 83

Expert Comment

by:leakim971
ID: 36947819
or save the serialized string before disable the fields : http://jsfiddle.net/3aBd4/1/

<div style="width:300px; text-align:left;">
                <form action="/" id="frmLogin" action="POST">
                <div style="width:150px; float:left;"><input type="text" id="txtUsername" name="txtUsername" /></div>
                <div style="width:150px; float:left;"><input type="password" id="txtPassword" name="txtPassword" /></div>
                <div style="width:300px; float:left; text-align:center;">
                    <div id="divLoginMessage" style="width:300px; float:left; text-align:centre;"></div>
                    <div style="width:50px; float:left;"></div>
                    <div id="btnLogin" style="width:200px; float:left; text-align:center; background-color:#C8C8C8; cursor:pointer;" onClick="Javascript:attemptLogin();">Login</div>
                        
                        <script type="text/javascript">
                            function attemptLogin() {
                                if ($("#btnLogin").text() == "Login")
                                    {
                                    $("#btnLogin").text("Logging in....");
                                    var dd = $("#frmLogin").serialize();
                                    $("#txtUsername").attr("disabled", "disabled");
                                    $("#txtPassword").attr("disabled", "disabled"); 
                                    alert(dd);
                                    $.post("attemptLogin.php", dd, 
                                    function(data) {
                                        if (data.substring(0,15) == "Login Succeeded")
                                            {
                                            window.location.href = "loggedIn.php?AuthID=" + data.substring(16)
                                            }
                                        else
                                            {
                                            $("#divLoginMessage").html(data);
                                            $("#txtUsername").removeAttr("disabled");
                                            $("#txtPassword").removeAttr("disabled"); 
                                            $("#btnLogin").text("Login");                                            
                                            }                                        
                                    });
                                    }
                            }
                        </script>
                    <div style="width:50px; float:left;"></div>
                </div>
                </form>
            </div>

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question