udo
udo

Reputation: 19

how to add more params to ajax post request

I am able to use ajax GET method to post data to a php page, but I have a problem adding more parameters to the POST METHOD. Below is the code I use. Because I am able to have a response from the php script:

    if(XMLHttpRequestObject) {
   XMLHttpRequestObject.open("POST", url,true);XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
 XMLHttpRequestObject.onreadystatechange = function()
 {
    document.getElementById('statuses').innerHTML = msg1;
   if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
var content = XMLHttpRequestObject.responseText;
 $(document).ready(function(){
    $('#statuses').text(content);
    });
}
 }
XMLHttpRequestObject.send("id=" + id);
}

Now assuming I decided to use another code to send the data which is quite easier with jquery, how do I get the response from the php script? Below is the second code:

     $(document).ready(function(){ 
    // get values
     cc = $('#ft').attr('value');
     cop = $('#copt').attr('value');

     // send to processing PHP script
     $.ajax({
        type: "GET",
        cache: false,
        url: "processor.php",
        data: "cctotal="+ cc +"&coptotal="+ cop + "&id="+ id + "&get=" + 'update',
        success: function(){
            $('#processing').fadeIn(function(){
            var content = 'Customer Account Updated !' ;
            $('#statuses').text(content);
            });
        }
            });

     });

Upvotes: 0

Views: 243

Answers (2)

adeneo
adeneo

Reputation: 318252

The default contentType of ajax is 'application/x-www-form-urlencoded; charset=UTF-8', and cahce so you don't need to set that, and the plain JS XMLHttpRequestObject call you're making would in jQuery look like :

$(function() {
    $.post(url, , function(content) {
        $('#statuses').html(content);
    });
});

To do a POST request with some added data you would do something like

$(function(){ 
    cc = $('#ft').val();
    cop = $('#copt').val();

    $.ajax({
         type: 'POST',
         url: url,
         cache: false,
         data: {cctotal: cc, coptotal: cop, id: id, get: 'update'}
    }).done(function(content) {
         $('#processing').fadeIn(function(){
             $('#statuses').text('Customer Account Updated !');
         });
    });
});
​

And you would access that data in the POST superglobal on the server. For instance in PHP it would be :

$cc  = $_POST['cctotal'];
$cop = $_POST['coptotal'];
$id  = $_POST['id'];
$get = $_POST['get'];

Upvotes: 1

Infinity
Infinity

Reputation: 3875

//Try this

 $(document).ready(function(){ 
    // get values
     cc = $('#ft').attr('value');
     cop = $('#copt').attr('value');

     // send to processing PHP script
     $.ajax({
        type: "GET",
        cache: false,
        url: "processor.php",
        data: "cctotal="+ cc +"&coptotal="+ cop + "&id="+ id + "&get=" + 'update',
        success: function(data){
            $('#processing').fadeIn(function(){
            var content = 'Customer Account Updated !' ;
            $('#statuses').text(content);
            });
            alert(data) //server response here
        }
            });

     });

Upvotes: 0

Related Questions