Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Form submit from dialog

Posted on 2013-05-19
10
Medium Priority
?
556 Views
Last Modified: 2013-05-21
I have a site and its exclusively for members, when a page is accessed if the cookie is set then they are allowed in, if not i have a modal dialog that displays a form and asks for their password, the form has one field "password", the dialog has 2 buttons, "Log Me In" and "Cancel"
i have no problems with any of that...what i can't seem to do is tie up the "Log Me In" button to submitting the form to my php page _checkpassword.php
Any and all help would be appreciated

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Home Page</title>
	<link rel="stylesheet" href="http://code.jquery.c<wbr ></wbr>om/ui/1.10<wbr ></wbr>.3/themes/<wbr ></wbr>smoothness<wbr ></wbr>/jquery-ui<wbr ></wbr>.css" />
	<script src="http://code.jquery.co<wbr ></wbr>m/jquery-1<wbr ></wbr>.9.1.js"><<wbr ></wbr>/script>
	<script src="http://code.jquery.co<wbr ></wbr>m/ui/1.10.<wbr ></wbr>3/jquery-u<wbr ></wbr>i.js"></sc<wbr ></wbr>ript>
<style>
body { font-size: 80%; }
input.text { margin-bottom:12px; width:65%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
</style>
<script>
$(function() {
	$( "#passwordform" ).dialog({
	autoOpen: true,
	height: 300,
	width: 350,
	modal: true,
	buttons: {
		"Log Me In": function() {
			$("loginform").submit(func<wbr ></wbr>tion() { 
				var mydata = $("loginform").serialize()<wbr ></wbr>;
				alert(mydata); // test
				 $.ajax({
					type: "POST",
					url: "_checkpassword.php",
					data: mydata,
					success: function(response, textStatus, xhr) {
						alert("success"); //test
					},
					error: function(xhr, textStatus, errorThrown) {
						alert("error"); //test
					}
				});
				return false;
			});
			$( this ).dialog( "close" );
		},
		Cancel: function() {
			$( this ).dialog( "close" );
		}
	}
});
});
</script>
</head>
<body>
<div id="passwordform" title="Password Required">
	<p>Please enter your password.</p>
	<form id="loginform" name="loginform">
		<fieldset>
			<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
		</fieldset>
	</form>
</div>
<div>Page html here</div>
</body>
</html>

Open in new window


Also I would like that once the password is validate by the php script, that the contents of the dialog either display an error message and asking to try again,
or if the info was correct have it display "welcome <name extracted from db>" and an ok button to close out dialog...
is this even possible?....still a relatively newbie to jquery....
thx
0
Comment
Question by:leozayas
  • 4
  • 4
  • 2
10 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39180103
Your dialog is appearing only you have problem is there is <wbr ></wbr> tags inside your code. Open your favorite editor and replace them. Your code should be like:

<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8" />
	<title>Home Page</title>
	<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
	<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
	<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<style>
body { font-size: 80%; }
input.text { margin-bottom:12px; width:65%; padding: .4em; }
fieldset { padding:0; border:0; margin-top:25px; }
</style>
<script>
$(function() {
	$( "#passwordform" ).dialog({
	autoOpen: true,
	height: 300,
	width: 350,
	modal: true,
	buttons: {
		"Log Me In": function() {
			$("loginform").submit(function() { 
				var mydata = $("loginform").serialize();
				alert(mydata); // test
				 $.ajax({
					type: "POST",
					url: "_checkpassword.php",
					data: mydata,
					success: function(response, textStatus, xhr) {
						alert("success"); //test
					},
					error: function(xhr, textStatus, errorThrown) {
						alert("error"); //test
					}
				});
				return false;
			});
			$( this ).dialog( "close" );
		},
		Cancel: function() {
			$( this ).dialog( "close" );
		}
	}
});
});
</script>
</head>
<body>
<div id="passwordform" title="Password Required">
	<p>Please enter your password.</p>
	<form id="loginform" name="loginform">
		<fieldset>
			<input type="password" name="password" id="password" value="" class="text ui-widget-content ui-corner-all" />
		</fieldset>
	</form>
</div>
<div>Page html here</div>
</body>
</html>
                                  

Open in new window


If you need code for _checkpassword.php write here.
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39180106
And one solution as you need is here:
http://www.bitrepository.com/ajax-login-modal-box.html
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39180543
You're not selecting your form correctly. An ID selector starts with the hash so it should be $('#loginform'). Something like this:

$("#loginform").submit(function() { 
     var mydata = $("#loginform").serialize();

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39180578
Also, you don't have a submit button in your form, so you don't need to do it the way you are. You have a Log Me In button, so you need to set that to send the data to your form using ajax:

$("#passwordform").dialog({
	autoOpen: true,
	height: 300,
	width: 350,
	modal: true,
	buttons: {
		"Log Me In": function() {
			var mydata = $("#loginform").serialize();
			alert(mydata);

	 		$.ajax({
				type: "POST",
				url: "_checkpassword.php",
				data: mydata,
				success: function(response, textStatus, xhr) {
					alert("success");
				},
				error: function(xhr, textStatus, errorThrown) {
					alert("error");
				}
			});
			$( this ).dialog( "close" );
		},
		Cancel: function() {
			$( this ).dialog( "close" );
		}
	}
});

Open in new window

0
 

Author Comment

by:leozayas
ID: 39182637
yes thank you chris what i did was capture the "log Me In" click event and then send the val of the password field to the php file.....

the part i am working on now and kinda stuck is sending information back to the dialog based on correct entry or incorrect entry....

thx so much gain
0
 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39183708
The data sent back to the ajax call is whatever your PHP script outputs. This can be a simple value echoed or an array of data - json encoded and then echoed. The data is sent back in the 'response' argument of the success function. If your PHP script has the following...

echo "Thank you for submitting your password";

Open in new window

...then the value of the response argument would be 'Thank you for submitting your password'. You could then do:

success: function(response) {
     alert(response);
}

Open in new window

If you want to send back more than a single value, you would JSON encode an array before echoing it:

<?php
$myData = array(
     'name' => 'chris',
     'role' => 'admin',
);
echo json_encode($myData);
?>

Open in new window

Now your 'response' argument will be a JSON object, and you access the properties like so:

success: function(response) {
     alert("Your name is " + response.name);
     alert("Your role is " + response.role);
}

Open in new window


You may need to tell the ajax script that the response is JSON in the options using the dataType argument:

$.ajax({
	type: "POST",
	url: "_checkpassword.php",
	data: mydata,
	dataType: 'json',
	success: function(response) {
		alert("success");
	},
	...

Open in new window

0
 

Author Closing Comment

by:leozayas
ID: 39183725
thank you chris that's it exactly.....
0
 

Author Comment

by:leozayas
ID: 39183729
now i can get the response whether echoed or json back in the dialog correct?
0
 
LVL 44

Expert Comment

by:Chris Stanyon
ID: 39183836
Yeah - the output of the PHP script will be sent back to your AJAX call - either as a single value or a JSON object. You can then do whatever you like with that info.

//Alert a value
alert(response.name);

//Set the content of a DIV to a returned value
$('#someDiv').html(response.name);

//Check a returned value and act accordingly:
if (response.loggedin) {
     alert("Logged in successfully");
} else {
     alert("Incorrect Password: Please Try Again!!");
}
0
 

Author Comment

by:leozayas
ID: 39183866
awesome thank you sir
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

916 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