jQuery Serialize

I have a modal popup that I can get to work in a less than ideal way.  I need some help with the jquery serialize coding.

The form (simplified) essentially looks like this:

<form id="logForm">
        <div id="essentialModal" class="modal current">
	      <div id="loginDiv">
	          <div><input class="loginBox" id="urserNameText" type="text" autocomplete="off"></div>
	          <div><span><input class="loginBox" id="passwordText" type="password" autocomplete="off"></span></div>
	          <div onclick="loginBtn();"><img src="spotLog/images/login_btn.png"></div>
		    </div>
   		</div>
</form>

Open in new window


Ideally, when the login button is pressed, this would go as a "post" request to a form called, dotheajax.php.  I have it working with this javascript function, but I think there is a much better way to do this with a serialize command instead.

function loginBtn() {
	var un = document.getElementById('urserNameText').value;
	var pw = document.getElementById('passwordText').value;
	if (un > "" && pw > "") {
		var url = "dotheajax.php?a=login&un="+un+"&pw="+pw;
		$( "#loginDiv" ).load( url );
       }
} 

Open in new window


This code ends up sending the username and password as part of the string.  I then use a GET command to grab the data.  Like I said this works, but as I get into more complex form that I want to send, it gets really cumbersome to have to make sure I encode every field I want to send. I know people talk about using jQuery Serialize to do this, and I've tried a bunch of different things but cannot get it to work.

The last part of the function "$("#loginDiv").load(url) is what repaints my modal screen to show the results of the login attempt.

What I'd like to do is something like this:
function loginBtn() {
	var formSubmit = $(this).serialize();
	var url = "dotheajax.php?a=login&str=formSubmit;
		$( "#loginDiv" ).load( url );
} 

Open in new window


So how do I get my post variables to send.

Bottom line, what's the best way to do this.
Paul KonstanskiProject SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
function loginBtn() {
	params = $("myform").serialize();
	.ajax({
		type: "POST",
		url: url,
		data: params,
		success: function(data){
			$("#loginDiv").html(data);
		}
	});
}

Open in new window

0
Paul KonstanskiProject SpecialistAuthor Commented:
Thanks GaryC123: I had to make a few changes because it was giving me script errors, and the form is talking with the php page via the ajax call, but it is NOT sending the post variables.  Here's how I had to change the form to get it to work.

function loginBtn() {
	var url = "dotheajax.php?a=login";
	params = $("#logForm").serialize();
	  $.ajax({
		type: "POST",
		url: url,
		data: params,
		success: function(data){
			$("#loginDiv").html(data);
		}
	});
}

Open in new window


 And on the dotheajax.php form I do this:

print_r($_GET); print '<hr />';
print_r($_POST); print '<hr />';
$_POST['test'] = "test of postvar";
print_r($_POST); print '<hr />';
die ("you are at line 47... Do you have post vars");

Open in new window


The result that comes back in my modal window is:

Array ( [a] => login )
Array ( )
Array ( [test] => test of postvar )
you are at line 47... Do you have post vars

Which shows that the post vars are not being passed.  Do you have any other suggestions?
Thanks.
0
GaryCommented:
For an input to pass any value in a GET or POST it needs a name - your inputs don't have any names!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul KonstanskiProject SpecialistAuthor Commented:
Thanks. Wished I'd asked a couple of hours ago. I was using somebody elses text and didn't see that they didn't have names.  Glad you caught it for me.  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.