Solved

Form submit from dialog

Posted on 2013-05-19
10
554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 43

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 43

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 43

Accepted Solution

by:
Chris Stanyon earned 500 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 43

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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)
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

617 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