0

I need to take data in from a form, using a javascript call. From there I need to send the data to a php script that puts it in a database. I have an AJAX call that does this...but I know that the AJAX call also waits for a response from there Server. This will work but I'm looking for a more efficient way to do it, i.e. a way to do it with out waiting for there server to respond.

Thanks in advance.

2
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by chris.aaker
0

That's the method that returns the AJAX object that I'm asking about. The more specefic quesiont was.

All the examples I've seen using this object require a two way comms. From the Client to the Server and back again.

I want to go from the client to the server only.

but yes that is the method/fucntion I used to instatiate the object.

1

I don't quite understand in regards to the two way comms you mentioned.

function getHTTPObject() {
 if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try {
            return new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e) {}
    }
    return false;
}

var http = getHTTPObject();

http.open('POST', 'http://www.example.com/your_script.php?key1='+value1+'&key2='+value2, true);
http.send();

Edited by Javvy: n/a

0

That's neat. I have not seen that yet. I wasn't sure if that object could be used just to send...all the examples I've seen had looked something like this....

With your way though..it just sends the data...and I'm assuming does not wait for any response?

function ajax_post(path, args, parameters)
    {
    params=""
    for (var p in parameters)
        {
        params += p + "=" + parameters[p] + "&"; 
        }
    object1 = new ajax_object()
    object1.open("POST", path, true)/*insert to mysql here*/
    object1.setRequestHeader("Content-type",
        "application/x-www-form-urlencoded")
    object1.setRequestHeader("Content-length", params.length)
    object1.setRequestHeader("Connection", "close")
    object1.onreadystatechange = function()
    {
        if (this.readyState == 4)
        {
            if (this.status == 200)
            {
                if (this.responseText != null)
                {
                for (var a in args)
                    {
                    document.getElementById(a).innerHTML = args[a]
                    }
                }
                else alert("Ajax error: No data received")
            }
            else alert( "Ajax error: " + this.statusText)
        }
    }
    object1.send(params)
  }

Edited by mike_2000_17: Fixed formatting

0

Fix:

function ajax_post(path, args, parameters)
	{
	params=""
	for (var p in parameters)
		{
		params += p + "=" + parameters[p] + "&"; 
		}
	object1 = new ajax_object()
	object1.open("POST", path, true)/*insert to mysql here*/
	object1.setRequestHeader("Content-type",
		"application/x-www-form-urlencoded")
	object1.setRequestHeader("Content-length", params.length)
	object1.setRequestHeader("Connection", "close")
	object1.onreadystatechange = function()
	{
		if (this.readyState == 4)
		{
			if (this.status == 200)
			{
				if (this.responseText != null)
				{
				for (var a in args)
					{
					document.getElementById(a).innerHTML = args[a]
					}
				}
				else alert("Ajax error: No data received")
			}
			else alert( "Ajax error: " + this.statusText)
		}
	}
	object1.send(params)
  }
0

That's how I usually do it. Maybe that's why I didn't understand your 2 way comms. Anyway, glad it's working for you :)

0

Thanks...i'll implement that soon...thanks again. I guess it was easier than I imagined...haha.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.