I want to change the send data method in the code from GET TO POST

Discussion in 'JavaScript' started by lionking, May 4, 2009.

  1. #1
    Hello Everybody

    I have ajax code send data by GET method to php page

    I want to change the send data method in the code from GET TO POST

    THE CODE
    
    
    function Add_Site(){
        var site_name = document.getElementById('site_name').value;
        var site_desc = document.getElementById('site_desc').value;
        var site_url = document.getElementById('site_url').value;
        
        if (site_name == "" || site_desc == "" || site_url == "") {
            document.getElementById('show_result').innerHTML = "fields is empty";
        }
        else {
            Http.onreadystatechange = function(){
                if (Http.readyState == 4) {
                    document.getElementById('show_result').innerHTML = "";
                    document.getElementById('show_result').innerHTML = Http.responseText;
                }
                else {
                    document.getElementById('show_result').innerHTML = "";
                    document.getElementById('show_result').innerHTML = "Loading";
                }
            }
            
            rand = Math.random() * 999999999;
            url = "addSite.php?site_name=" + site_name + "&site_desc=" + site_desc + "&site_url=" + site_url + "&rand=" + rand;
            Http.open("GET", url, true);
            Http.send(null);
        }
    }
    
    
    Code (markup):
     
    lionking, May 4, 2009 IP
  2. oceaniajimmy

    oceaniajimmy Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you only need to change the get to post
     
    oceaniajimmy, May 4, 2009 IP
  3. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #3
    Hi Lionking,

    Follow the steps for using 'post' method in AJAX.

    1. change the first argument of the "open" function from GET to POST.

    2. the "url" variable should only consist of the site name.

    3. Set some hhtp headers required for post method

    4. Define a variable say 'parameters' and assign the additional parameters in the url variable to this new variable.

    5. pass this new variable in the http.send method.

    Here is the changed code.

    Hope this helps.

    Unni Krishnan
    TechnoTwins Design and Solutions
     
    Unni krishnan, May 4, 2009 IP
  4. lionking

    lionking Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thank you Mr Unni krishnan for Help and also mr oceaniajimmy
    I make it if write this the code working

    
    
    rand = Math.random() * 999999999;
    url = "addSite.php";
    parameters = "site_name=" + site_name + "&site_desc=" + site_desc + "&site_url=" + site_url + "&rand=" + rand;
    Http.open("POST", url, true);
    
    //Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    Http.send(parameters);
    
    Code (markup):

    but if add this The code is not working
    
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close"); 
    
    Code (markup):
    I want to know what this (params.length)

    thank you
     
    lionking, May 5, 2009 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I think it is meant to be:
    parameters.length
     
    camjohnson95, May 5, 2009 IP
  6. Unni krishnan

    Unni krishnan Peon

    Messages:
    237
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    0
    #6
    Sorry lion king.
    Cam Johnson is right.
    I meant 'parameters.length'
     
    Unni krishnan, May 5, 2009 IP
  7. lionking

    lionking Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thank you mr Unni krishnan and mr camjohnson95 for Help
     
    lionking, May 6, 2009 IP
  8. Aaron Sustar

    Aaron Sustar Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Lionking, I know this wasn't your question, but I still somehow feel obliged to say this ... Take an hour of your time and get familiar with jQuery, which is a superior JavaScript library.

    It will take you hours and hours of work on your future project, the code is much more likely to run without problems on all browsers and it allows you to do a really trendy user interface with a few lines of code.

    For instance, a POST request in jQuery looks like this:
    $.post("location-of-your-script.php", 
       { firstParameter: "this is first parameter's value", secondOne: "yeah!" }, 
       function(data) {		
          // what happens when the server sends a response
          $("#show_result").html(data);
       }
    );
    Code (markup):
    I really suggest you using jQuery from the bottom of my heart, haha. :D
     
    Aaron Sustar, May 7, 2009 IP