|
 |
|
 |
05-18-2006, 07:20 PM
|
#1 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
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?
|
|
|
05-18-2006, 07:58 PM
|
#2 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
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?
|
|
|
05-18-2006, 08:32 PM
|
#3 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
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 
|
|
|
05-18-2006, 11:14 PM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
__________________
Mike
|
|
|
05-19-2006, 06:08 AM
|
#5 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
Hah, sweet. I searched google several times over before I found the answer to the riddle. It was right under my nose :p
|
|
|
05-19-2006, 07:59 AM
|
#6 (permalink)
|
|
PHP Student
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 150
|
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>
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:35 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|