HTML Form with one textbox submits automatically.

I ran across this problem while building a search function for one of the web-pages at work. I already had couple of forms on that page that used jQuery to submit the form via AJAX. So, I just copied one of the forms and modified it to build the form for search. I was capturing the return key on the form to submit the data to the server. But instead of sending the AJAX request, the whole page got submitted and the page would refresh. I found this very annoying. As, similar code worked for the other two forms but it won’t work for this form. After couple of hours of trial and error and making sure that I didn’t have a typo and everything was correct, I found out that – “if you have only one textfield in your form then the browser would automatically submit the form on hitting the return key.” :O

To circumvent this weird behavior, I returned false from the jQuery event handler function after doing my processing of the form. Below is an executable bare-bones example code snippet of this scenario.


<html>
<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
	</script>
	<script type="text/javascript">
		$(document).ready(function(){

			$("#search_form").keypress(function(e) {
				if(e.which == '13') {

					//Do Processing.
					process();

					/*This is important if there is only one 
                                           textfield in your form.Try to comment 
                                           this and see the difference.*/
					return false;
				}
			});

			$("#submit_search").click(function() {
				process();
			});

			$("#search_string").focus();

		});
		function process() {
			var search_string = $("#search_string").val();

			if( jQuery.trim(search_string) == "") {
				alert("Please enter a non-empty search string!");
				return false;
			}

			alert("You searched for: " +  search_string);
		}
	</script>
</head>
<body>

	<form id="search_form">
		<input id="search_string" type="text" />

	<!--
		Uncomment this textfield and then comment the 
                "return false" statement and the page won't 
                refresh/submit. But keeping both - this and the 
                "return false" statement commented would execute 
                your process() function but it would refresh/submit
		your page.

		<input id="dummy_txt" type="text" />

	-->
		<input id="submit_search" type="button" value="Search" />
	</form>
</body>
</html>

The “return false” in the above code is the key. If you don’t have that statement then the form will get submitted to itself. But if you have more than one text-field in your form, then you do not need that “return false”. 🙂 And yes you do need a “return false”, just a return or “return true” won’t work either. 🙂

Advertisements
Posted in HTML Forms | Leave a comment

MySQL Double Field

MySQL doesn’t have a money field. So you end-up using double datatype to store Money values. Make sure you’re removing the ‘$’ character and commas from your money values before storing. Otherwise it would end-up truncating your values.

For example:

if you try to store 2,000.00 in your insert statement. Then it will only store 2 in the field. As it considers comma as a non-integer and discards the remaining digits in the number.

I figured this hard way, when a transaction of $2,000 ended up as only $2.00 in my database. 🙂

Posted in MySQL | Leave a comment

Failed to start Glassfish Server

I was getting following error, while starting the GlassFish Enterprise Server v2.1 (9.1.1) (build b60e-fcs). And it failed to startup.

[#|2010-07-07T17:55:38.663-0500|SEVERE|sun-appserver2.1|javax.enterprise.system.core|_ThreadID=10;_ThreadName=main;_RequestID=592002e1-2f4e-44e3-b7da-1cc5af8129da;|CORE5071: An error occured during initialization
com.sun.appserv.server.ServerLifecycleException: Cannot bind to URL [rmi://REMOTE_IP:8686/management/rmi-jmx-connector]: javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: REMOTE_IP; nested exception is:
java.net.ConnectException: Connection timed out]

[#|2010-07-07T18:32:18.697-0500|SEVERE|sun-appserver2.1|javax.enterprise.system.core|_ThreadID=10;_ThreadName=main;_RequestID=e14b9b96-f473-409a-b6cd-f46bee3574fb;|CORE5071: An error occured during initialization
com.sun.appserv.server.ServerLifecycleException: Cannot bind to URL [rmi://REMOTE_IP:8686/management/rmi-jmx-connector]: javax.naming.NoPermissionException [Root exception is java.rmi.ServerException: RemoteException occurred in server thread; nested exception is:
java.rmi.AccessException: Registry.Registry.bind disallowed; origin /192.168.1.1 is non-local host]

After few hours of googling and trial and error. I found that you can disable the JMX Connector by disabling it in the domain.xml file located at /domains/domain1/config/domain.xml in your GlassFish install directory.

You need to set the enabled attritute to false, as shown below.


<jmx-connector accept-all="true" address="0.0.0.0" auth-realm-name="admin-realm" enabled=”false” name=”system” port=”8686″ protocol=”rmi_jrmp” security-enabled=”false”>

Obviously this is not a permanent solution. But it does start the server back up. If you know the solution to the issue please let me know in the comments.

Posted in GlassFish | Leave a comment