www.webdeveloper.com
Results 1 to 3 of 3

Thread: Output from search function to display on seperate page

  1. #1
    Join Date
    Mar 2010
    Location
    St.Louis, MO
    Posts
    2

    Output from search function to display on seperate page

    Hey guys I had a question about outputting the results from a javascript search function onto a new page. Right now I am using a javascript search script on 7 pages and it works but the results overwrite the current page that the search was initiated from. I would like it to open a new page and print out into a specific area. Im not really proficient in js, tho I know enough to get by. Any help would be greatly appreciated, since ive been grinding my gears at work trying to get this to display right. I'll post the code tomorrow morning when I get to work so you can see the function.

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    I am assuming you are using ajax to do your js search.

    Something like the following should do the trick:

    1) get your server side script to get the search results and wrap them in appropropriate html code using echo statements.

    2) the responseText property of your ajax object will then receive all echo output from your php script. Store the responseText in a variable,

    var str = ajaxObj.responseText;

    3) In the ajax request handler function then open a pre-existing 'results window' with a designated element, say <div id="results"></div> since you erquire a designated area for search results to be displayed.

    resultsWin = window.open("results.htm","results");
    4) write the search results to the "results" div.
    Code:
     
    resultsWin.document.getElementById("results").innerHTML = str;
    Last edited by tirna; 03-15-2010 at 08:44 PM.

  3. #3
    Join Date
    Mar 2010
    Location
    St.Louis, MO
    Posts
    2
    How bout I show you what I have.

    Code:
    function search(){
    var skeyword=document.searchengine.keywords.value.toLowerCase();
    var check=1;
    var pos=0;
    var i=0;
    var j=0;
    var  itemp=0;
    var config='';
    
    while (true)
    	{
    	if (skeyword.indexOf("+") == -1 )
    		{
    		keywords[check]=skeyword;
    		break;
    		}
    	pos=skeyword.indexOf("+");
    	if (skeyword !="+")	
    	{
    	keywords[check]=skeyword.substring(0,pos);
    	check++;
    	}
    	else
    	{
    	check--;
    	break;
    	}
    	skeyword=skeyword.substring(pos+1, skeyword.length);	
    	if (skeyword.length ==0)
    		{
    		check--;
    		break;
    		}
    			 
    	}
    // the keywords have been put in keywords object.
    keywords[0]=check;
    
    // matching and storing the matches in matched
    for ( i=1; i<=keywords[0];i++)
    	{
    	for (j=1;j<=title[0];j++)
    		{
    		if (title[j].toLowerCase().indexOf(keywords[i]) > -1 )
    			{
    			  matched[j]++;
    			}
    		}	
    	}
    // putting all the indexes of the matched records  in found
    
    for (i=1;i<=title[0];i++)
    {
    	if (matched[i] > 0 )
    		{
    		  found[0]++;
    		// increment the found 	
    		  found[found[0]]=i;
    			
    		}	
    }
    for (i=1;i<=found[0]-1;i++)
    	{
    	for(j=i+1;j<=found[0];j++)
    		{
    		if ( matched[found[i]]< matched[found[j]] )
    			{
    			temp= found[j];
    			found[j]=found[i];
    			found[i]=temp;
    			}
    		}
    	}
    document.write('<html>');
    document.write('<head>');
    document.write('<script>');
    document.write('window.onerror=new Function("return true")');
    document.write('(<\/script>');
    document.write('<title> Search Results </title>');
    document.write('</head>');
    document.write('<BODY bgcolor=#ffffff  text=#000000  link=#990099 vlink =#339966 >');
    document.write('<center> <h1> Search Results </h1></center>');    
    document.write('<hr>');
    document.write(' You searched for: '.big() )
    for (i=1;  i<=keywords[0]; i++)
    	{
    	document.write( keywords[i].bold() +"   ");
    	}
    document.write('<br>');
    
    if (found[0]==0)
    	{
    	//alert(found[0]);
    	document.write('<hr>');
    	document.write("<b>No matches resulted in this search </b> <br>");
    	document.write("You may close the results and reduce the length/number  of the keywords  <br>");
    	}
    else
    	{
    	// data has been found
    	document.write(" <hr> <b> The Results of the search are  : </b>  ");
    	document.write( found[0] +"  Entries found  ".italics());
    	document.write("<center><table border=0 width=100&#37;>");
    	for (i=1; i<=found[0];i++)
    		{
    		document.write("<tr><td valign=top bgcolor=#597786>");
    		document.write("<h3><font color=#DED9C2>" +i +"</font></h3>");
    		document.write("<td valign=top bgcolor=#DED9c2>");
    		itemp=found[i];
    		document.write("<font color=#905A33>"+desc[itemp].bold() +"</font><br><font color=#597786>" +
    		links[itemp].link(links[itemp])+"</font><br>");
    		temp= (matched[itemp]/keywords[0])*100
    		document.write("<i> Matched  with keywords: " +temp+" %  </i>" );
    		matched[itemp]=0
    		} 
    	found[0]=0;
    	document.write("</table><center>");
    	}
    document.write("<hr>");
    document.write("<center>Hit the back button to return to your previous page<center>");
    document.write("</body></html>"); 
    document.close();
    }
    Basically above that I set Keywords for each of the pages that I have indexed, and then when someone searches for something, the search looks for through the keywords that I set for each page, and if there is a match it stores it in an array and then prints it out.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles