Jump to content

Ajax search, need to add on to URL but how??


xcandiottix

Recommended Posts

Okay, I am creating a search form. There are two search fields.. possibly more in the future so my solution has to be scalable.

 

    <form name="searchc" action="" onkeyup="update()"> 
<input name="firstName" type="text" size="30" /> First Name<br />
    <input name="lastName" type="text" size="30" /> Last Name<br />
</form>
<script type="text/javascript">
function update() {
showCategory('firstName',document.searchc.firstName.value);
showCategory('lastName',document.searchc.lastName.value);
}
</script>
<script type="text/javascript">
function showCategory(name,str){
	if (str==""){
  			document.getElementById("PeopleResults").innerHTML="";
  			return;
  		}  
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
  			xmlhttp=new XMLHttpRequest();
  		}
	else{// code for IE6, IE5
  			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
	xmlhttp.onreadystatechange=function(){
  			if (xmlhttp.readyState==4 && xmlhttp.status==200){
    			document.getElementById("PeopleResults").innerHTML=xmlhttp.responseText;
    		}
  		}
	xmlhttp.open("GET","http://www.site.com/ajax/peoplesearchcriteria.php?"+name+"="+str,true);
	xmlhttp.send();
}

 

This currently sends EITHER a first name or a last name search to peoplesearchcriteria.php. I need to send both. As I said since this is scalable I want to use an array like this:

 

// make an array for each form field
inputs = document.searchc.elements;
numberOfInputs = count(inputs);
for(i=0; i <=numberOfInputs; i++){
feild+i=document.searchc.element[i].value
}
// add each field+i to a string .. how?

if (str==""){
document.getElementById("PeopleResults").innerHTML="";
return;
}  
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
  document.getElementById("PeopleResults").innerHTML=xmlhttp.responseText;
   }
}
//append this URL with array of field data sets
xmlhttp.open("GET","http://www.site.com/ajax/peoplesearchcriteria.php?("+name+"="+str)xeach field,true);
xmlhttp.send();
}

So the end result upon entering a first name would be:

http://www.site.com/ajax/peoplesearchcriteria.php?field0=Keith

Just last name:

http://www.site.com/ajax/peoplesearchcriteria.php?field1=Candiotti

Or Both:

http://www.site.com/ajax/peoplesearchcriteria.php?field0=Keith&&field1=Candiotti

 

ANY help on this one would be most awesome :D

 

 

 

 

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.