day_dreamer
day_dreamer

Reputation: 834

Http POST request using AJAX

I have one web application which I have been working for some time.I am sending data using HTTP POST request using AJAX technique.

I am using JavaScript like this:

var rdate=document.getElementById('rdate').value;
var Description=document.getElementById('description').value; 
var urlstring="rdate="+rdate+"&Description="+Description;

oxhr.open("POST","http://localhost/info/php/store.php",false); 
//checking for ready state change here

oxhr.send(urlstring);

But in database I see everything blank (no data but a row created) Shouldn't the urlstring contain variables? or what could be the possible mistake in JavaScript.

Any help is greatly appreciated.

Upvotes: 0

Views: 237

Answers (1)

user166390
user166390

Reputation:

Solution (to find the problem, because people aren't physic): use a tool like Firebug / Fiddler2 / IE Developer Tools, or other, to see what is really sent, or if a request is even made.

While I won't claim the following is an answer, the above manual string building in the post is ... painful and not very robust. Here is a little useful function:

function encodeParams(params) {      
    var components = []
    for (var k in params) {
        if (params.hasOwnProperty(k)) {
            var v = params[k]
            components.push(encodeURIComponent(k) + "=" + encodeURIComponent(v))
        }
    }
    return components.join("&")
}

Use as:

var queryString = encodeParams({
    admin_id: 1,
    rdate: rdate,
    Description: description
})

This also correctly encodes the components so the query string does not become corruptued.

Happy coding.

Upvotes: 1

Related Questions