Jump to content

HTML form indexes question


RIRedinPA

Recommended Posts

I'm trying to validate a form but each time I loop through the elements it is recognizing the second text entry as index 1, I'm passing the values through AJAX to PHP to validate them but my query string is all messed up because of the index number shuffle.

 

Here's the javascript code I am using to validate:

 


function validateform(objname) {
var theformvalues = getformvalues(objname);
alert(theformvalues);
var xmlHttp = checkajax();

xmlHttp.onreadystatechange=function() {
    	if(xmlHttp.readyState==4) {
      		//document.getElementById("nasvfform").innerHTML = xmlHttp.responseText;
		document.getElementById("newsletterform").innerHTML = xmlHttp.responseText;
      	}
    }
  	xmlHttp.open("GET","validateform.php" + theformvalues + "&thisform=" + objname,true);
  	xmlHttp.send(null);

}


/*SUBROUTINES*/

function getformvalues(objname) {
if (objname == "newslettersub") { 
	var theform = document.newslettersub;
}

var theformvalues = "?";
for(var x=0; x<theform.elements.length; x++) {
	if (x == 1) {
		alert(theform.elements[x].name);
		//get text values
		if (theform.elements[x].type == "text") {  
			theformvalues += theform.elements[x].name + "=" + theform.elements[x].value;
		} else if (theform.elements[x].type == "textarea") {
			theformvalues += theform.elements[x].name + "=" + theform.elements[x].value;
		} else if (theform.elements[x].type == "select-one") {
			var formIndex = theform.elements[x].selectedIndex;
			var dropdownvalue = theform.elements[x].options[formIndex].value;
			theformvalues += theform.elements[x].name + "=" + dropdownvalue;
		}
	} else {
		//get text values
		if (theform.elements[x].type == "text") {  
			theformvalues += "&" + theform.elements[x].name + "=" + theform.elements[x].value;
		} else if (theform.elements[x].type == "textarea") {
			//alert(theform.elements[x].name + "\n" + theform.elements[x].value);
			theformvalues += "&" + theform.elements[x].name + "=" + theform.elements[x].value;
		} else if (theform.elements[x].type == "select-one") { 
			var formIndex = theform.elements[x].selectedIndex;
			var dropdownvalue = theform.elements[x].options[formIndex].value;
			theformvalues += "&" +  theform.elements[x].name + "=" + dropdownvalue;
		} 
	}
}

return theformvalues; 
}

function checkstate(statename) {
alert(statename); 
}

function checkajax() {
var xmlHttp;
try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	} catch (e) {
  		// Internet Explorer
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}

return xmlHttp;
  }

</script>

 

Here's the form:

 


<div id="content">
<div id="links"><a href="javascript:void(0);" onmousedown="showdiv('newsletterform');">Newsletter Form</a>  |  <a href="javascript:void(0);" onmousedown="showdiv('eventregform');">Event Registration Form</a></div>
<div id="instructions" style="display: block;">Click on a link above</div>
<div id="newsletterform" style="display: none;">
	Fill in the form below to subscribe to the NASVF newsletter. Items marked with an * are required.<p>
	<form name="newslettersub">
		<label for "firstname">First Name*</label><br>
		<input type="text" size="20" name="firstname"><p>
		<label for "middlename">Middle Name</label><br>
		<input type="text" size="20" name="middlename"><p>
		<label for "lastname">Last Name*</label><br>
		<input type="text" size="20" name="lastname" id="lastname"><p>
		<label for "title">Title*</label><br>
		<input type="text" size="20" name="title" id="title"><p>
		<label for "company">Company*</label><br>
		<input type="text" size="20" name="company" id="company"><p>
		<label for "department">Department*</label><br>
		<input type="text" size="20" name="department" id="department"><p>
		<label for "email">Email*</label><br>
		<input type="text" size="20" name="email" id="email"><p>
		<label for "officephone">Office Phone*</label><br>
		<input type="text" size="20" name="officephone" id="officephone"><p>
		<label for "faxphone">Fax Phone</label><br>
		<input type="text" size="20" name="faxphone" id="faxphone"><p>
		<label for "address">Address</label><br>
		<input type="text" size="20" name="address" id="address"><p>
		<label for "city">City</label><br>
		<input type="text" size="20" name="city" id="city"><p>
		<label for "state">State/Province</label><br>
		<select name="state">
		<option value="null">Select A State</option>
			<?php 
				foreach($state_array as $key => $value) {
					echo "<option value='$key'>$value</option>"; 
				}
			?>
		</select><p>
		<label for "country">Country</label><br>
		<select name="country" onchange="checkstate(this.value);">
		<option value="null">Select A Country</option>
			<?php 
				foreach($country_array as $key => $value) {
					echo "<option value='$key'>$value</option>"; 
				}
			?>
		</select><p>
		<label for "url">Web Page</label><br>
		<input type="text" size="20" name="url" id="url"><p>
		<label for "comments">Comments</label><br>
		<textarea name="comments" rows=10 cols=30></textarea><p>
		<a href="javascript:void(0);" onmousedown="validateform('newslettersub')"><img src="images/submitbtn.png" border="0"></a>
	</form>
</div>
<div id="eventregform" style="display: none;">Event Registration</div>
</div>

 

Any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.