Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-18-2006, 07:20 PM   #1 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Javascript Submit Form Options?

Okay, I'm putting together a user registration form using PHP, javascript and ajax.

The username and image-text verification string are checked by calling a php script with ajax. The problem is it takes about 1-2 seconds for the response and I can't seem to make the script wait for the response before moving on. This ultimately causes the form validation function to return a false result.

I was able to setup a while() loop to loop continually until a flag is set at the end of the processResponse function of the AJAX request, but this loop delays the ajax response even more and triggers a javascript warning in firefox.

The best way I can think of solving this is to submit the form from within javascript when a response is received through AJAX, and I've tried it. Apparently "document.myform.submit()" is no-longer a valid function? There must be another way to submit forms via javascript besides the "onsubmit" handler?
__________________
Current Project
Redline is offline   Reply With Quote
Old 05-18-2006, 07:58 PM   #2 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Hmmm, any reason why the form.submit() function works when the script resides on the local machine, but not when trying to run it from the www?
__________________
Current Project
Redline is offline   Reply With Quote
Old 05-18-2006, 08:32 PM   #3 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Case Closed

I kid you not, I've been at this stupid form ALL DAY. It's taking all my strength to not burst out in fits of cussing rage. Most of the problems in the last several hours have been a result of the button I'm using to submit being named "submit".

Can't do that if you're using the submit() function. Oops. Problem solved
__________________
Current Project
Redline is offline   Reply With Quote
Old 05-18-2006, 11:14 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
wanna see something funny? .. 8/2004
javascript riddle: why won't my form submit from this text link?

__________________
Mike
sde is offline   Reply With Quote
Old 05-19-2006, 06:08 AM   #5 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Hah, sweet. I searched google several times over before I found the answer to the riddle. It was right under my nose :p
__________________
Current Project
Redline is offline   Reply With Quote
Old 05-19-2006, 07:59 AM   #6 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Alright, so my form works like a dream in firefox. No problems what-so-ever. IE on the other hand... it's still a bit buggy. It's possible to submit the form, yes, but if you fill the form out correctly and receive no errors the first time, you have to click submit twice. After making one or two mistakes filling the form out, the submit button works on the first try. If you have more than two errors (duplicate username check returns true, or input fields not filled out correctly), then the form won't submit at all. THEN when you try to refresh, it acts like the page isn't available anymore...

I THINK it has something to do with sending multiple, rapid AJAX requests. I think it might be choking the browser. Anyone run into a similar problem? It's a lot of code to post, but here it is. Here's the link too:

https://www.avclan.net/wip/register.php

HTML Code:
<script type="text/javascript">
	function numOnly(e, input, maxChars) {
		var keynum;
		var keychar;
		var numcheck;

		if (window.event) {
			keynum = e.keyCode;
		} else if (e.which) {
			keynum = e.which;
		}
		// Allow Backspace, Delete and Arrowkeys
		if (keynum == 8 || keynum == 127 || !keynum) {
			return true;
		}
		numcheck = /\d/;
		keychar = String.fromCharCode(keynum);
		if (input.value.length >= maxChars) return false;

		return numcheck.test(keychar);
	}
	function getIndex(what) {
		for (var i=0;i<document.register.elements.length;i++) {
			if (what == document.register.elements[i])
			return i;
		}
		return -1;
	}
	function jumpIndex(e, input, maxChars) {
		var i;
		var num = input.value.length;
		var keynum;
		var keychar;
		var chrcheck;
		if (window.event) {
			keynum = e.keyCode;
		} else if (e.which) {
			keynum = e.which;
		}

		if (num == maxChars && keynum != 8 && keynum != 127 && keynum != 9 && keynum) {
			i = getIndex(input);
			document.register.elements[i+1].focus();
		}
	}

	function validate() {

		var uname = document.register.uname.value;		
		var email = document.register.email.value;
		var dname = document.register.dname.value;
		var pass = document.register.pass.value;
		var pass2 = document.register.pass2.value;
		var birth_month = document.register.birth_month.value;
		var birth_day = document.register.birth_day.value;
		var birth_year = document.register.birth_year.value;
		var imgtext = document.register.imgtext.value;

		var testEmail = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/;
		
		if (!uname) {
			alert("Please enter Username");
			return false;
		}

		if (!email) {
			alert("Please enter E-mail");
			return false;
		} else if (email && !testEmail.test(email)) {
			alert("Incorrect e-mail address format");
			return false;
		}

		if (!dname) {
			alert("Please enter Display Name");
			return false;
		}

		if (!pass) {
			alert("Please enter Password");
			document.register.pass.value = "";
			document.register.pass2.value = "";
			return false;
		} else if (pass != pass2) {
			alert("Password Mismatch");
			document.register.pass.value = "";
			document.register.pass2.value = "";
			return false;
		}

		if (birth_month || birth_day || birth_year) {
			var bday_flag = 0
			if (!birth_month || birth_month.length < 2) bday_flag = 1;
			if (!birth_day || birth_day.length < 2) bday_flag = 1;
			if (!birth_year || birth_year.length < 4) bday_flag = 1;
			if (bday_flag) {
				alert("Birth date format incorrect");
				return false;
			}
		}
		if (!imgtext) {
			alert("Type the letters shown in the image in the supplied textbox");
			return false;
		}		

		return true;
	}



	function processResponse(response) {
		data = eval(response);
		if (data[0] == 1 && data[1] == 0) {
			document.register.submit();
		} else if (data[1] == 1) {
			alert("That username is taken");
		} else if (data[0] == 0) {
			alert("Invalid Serial");
		}
	}

	function handleStateChange() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				processResponse(xmlHttp.responseText);
			}
		}
	}

	function createXMLHttpRequest() {
		if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} else if (window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		}
	}

	function startRequest(method, URL, serial) {
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChange;
		xmlHttp.open(method, URL, true);
		xmlHttp.setRequestHeader(
			'Content-Type',
			'application/x-www-form-urlencoded; charset=UTF-8');
		xmlHttp.send(serial);
	}

	function checkSerial(x) {
		frm = x;
		if (validate()) {
			var serial = document.register.imgtext.value;
			var user_name = document.register.uname.value;
			var query = serial + "<!>" + user_name;
			startRequest('POST', 'https://www.avclan.net/wip/checkserial.php', query);
		}
	}
</script>


<form id="register" name="register" method="post" action="createuser.php" onsubmit="checkSerial(); return false;">
...
	<input type="submit" name="submit_form" value="Register" />

</form>
__________________
Current Project
Redline is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
can we make single form multiple submit buttons salmanjoo PHP 7 08-20-2005 07:44 AM
Javascript form submit on SP2 cocoy HTML, XML, Javascript, AJAX 0 08-15-2005 07:18 PM
javascript riddle: why won't my form submit from this text link? sde HTML, XML, Javascript, AJAX 6 09-29-2004 05:56 AM
Using javascript to submit a form sammy HTML, XML, Javascript, AJAX 2 07-15-2004 01:31 PM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 05:35 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting