Current location: Hot Scripts Forums » General Web Coding » JavaScript » Change this AJAX script to accomidate dynamic url


Change this AJAX script to accomidate dynamic url

Reply
  #1 (permalink)  
Old 06-13-06, 05:03 PM
arandlett arandlett is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Change this AJAX script to accomidate dynamic url

How could I update this code so that the url variable could be dynamic. Where I would call the preSearch(); function and open a different url with variables instead having to replicate the function for different situations.



Code:
var xmlhttp = false;
        
// If the user is using Mozilla/Firefox/Safari/etc
if (window.XMLHttpRequest) {
        //Intiate the object
        xmlhttp = new XMLHttpRequest();
        //Set the mime type
        //xmlhttp.overrideMimeType('text/xml');
} else if (window.ActiveXObject) {
        //Intiate the object
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}


function preSearch() {
    //Put the form data into a variable
	
	//var theQuery = -1;
	//var theQuery_a = -1;
	//var theQuery_b = -1;
	
	var theQuery = document.getElementById('query').value;
	var theQuery_a = document.getElementById('query2').value;
	var theQuery_b = document.getElementById('query3').value;
	
	if(theQuery !=="") {
	
    //If the form data is *not* blank, query the DB and return the results
	
        //Change the content of the "result" DIV to "Searching..."
        //This gives our user confidence that the script is working if it takes a moment for the result to be returned. However the user will likely never see this...
		document.getElementById('result').innerHTML = "Searching...";
		
        //This sets a variable with the URL (and query strings) to our PHP script
		var url = 'test_v3.php?type=' + theQuery + '&description=' + theQuery_a + '&owner=' + theQuery_b + '&Submit=Submit';
		
        //Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
		xmlhttp.open('GET', url, true);
        //Check that the PHP script has finished sending us the result
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //Replace the content of the "result" DIV with the result returned by the PHP script
				document.getElementById('result').innerHTML = xmlhttp.responseText + ' ';
			} else {
                //If the PHP script fails to send a response, or sends back an error, display a simple user-friendly notification
				document.getElementById('result').innerHTML = 'Searching...';
			}
		};
		xmlhttp.send(null);  
	} 
	}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-13-06, 05:41 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
You can define there variables outside the preSearch() function:

Code:
var theQuery = document.getElementById('query').value;
var theQuery_a = document.getElementById('query2').value;
var theQuery_b = document.getElementById('query3').value;
And add these as arguments in the function instead.

Code:
preSearch(theQuery, theQuery_a, theQuery_b)
{
// function rest...
{

Now you could use the same function like this

Code:
var theQuery = document.getElementById('query').value;
var theQuery_a = document.getElementById('query2').value;
var theQuery_b = document.getElementById('query3').value;

preSearch(theQuery, theQuery_a, theQuery_b);
Since you're defining the variables before calling the function, are you able to change them.


If you want to change the whole URL and not only these vars, then could you do this

Code:
preSearch(url)
{
// function rest...
{
Add only the url arg to the function, and use it like this

Code:
var theUrl = 'script.php?get=whatever&and=soon';

preSearch(theUrl);
You would have to remove this line from the function though
Code:
var url = 'test_v3.php?type=' + theQuery + '&description=' + theQuery_a + '&owner=' + theQuery_b + '&Submit=Submit';
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-13-06, 05:50 PM
arandlett arandlett is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Will those three variables be able to caputre the values when the form is submittedi if i place them outside the funtion? Or do i need to create another function that when the form is submitted will capture the variables then send the url through the preSearch(url); function?

Code:
  searchB() {

var theQuery = document.getElementById('query').value;
var theQuery_a = document.getElementById('query2').value;
var theQuery_b = document.getElementById('query3').value;

var urlVar = 'test_v3.php?type=' + theQuery + '&description=' + theQuery_a + '&owner=' + theQuery_b + '&Submit=Submit';

preSearch(urlVar);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-14-06, 04:01 AM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
Note that defining (and setting the values of) the variables outside the function like that will probably return an error because the page won't have finished loading when the script is executed. Either that or the "theQuery.." variables will all be empty since document.getElementById("") won't return a list of elements if it can't find them.

You would only need to do
Code:
var theQuery,theQuery_a,theQuery_b;
outside the function to make those variables global. Keep the rest of the code in the function but remove the var before the "theQuery.." variables or they will still be declared as private to that function.

That way, you can change any part of the query string any time by simply doing
Code:
theQuary="something new"
and the complete url string will be recreated inside the function each time it runs.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Raffle/Lottery Script (Very profitable!), Coded it myself. Voltaire General Advertisements 6 03-16-09 08:15 AM
2 profitable script sites for sale cms-master.com General Advertisements 3 07-03-07 11:17 AM
Raffle/Lottery Script (Very profitable!), Coded it myself. Voltaire General Advertisements 2 01-03-06 12:55 AM
need Java script ( Ajax) Calendar, tree, grid curtisannev Job Offers & Assistance 1 10-30-05 09:59 PM
CGI problem - Script only allows a small number of digits in my customers site url??? Ireland Perl 6 10-09-05 08:09 PM


All times are GMT -5. The time now is 01:41 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.