Jomar Llevado
Jomar Llevado

Reputation: 69

how to make AJAX use POST and not GET

I got this working code right here:

$(document).ready(function(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try {
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }

    $('#response').hide();

    $('#addroom').click(function(){
        var url = "./scripts/addnew_room.php";

        var room_num = document.getElementById('rm_num').value;
        var room_type = document.getElementById('rm_type').value;
        var tosend = "&rm_num="+room_num+"&rm_type="+room_type;

        ajaxRequest.open("POST", url, true);
        ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        ajaxRequest.onreadystatechange = function(){

            if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
                var return_data = ajaxRequest.responseText;
                document.getElementById("response").innerHTML = return_data;    
            }
        }
        ajaxRequest.send(tosend);
        $('#response').show();
        document.getElementById("response").innerHTML = '<img src="./ajax-images/ajax-loader.gif">';
    });
});

can someone tell me why it is not sending the variable into POST method and even if i set up the method to POST it is still using a GET method and disrupts my page!!!

Upvotes: 1

Views: 439

Answers (5)

StaticVariable
StaticVariable

Reputation: 5283

You can use something like this

$.POST("url.com", {name: "John", location: "Boston"}, function(){

})

POST() is a jquery function

You can also use jquery ajax() function

$.ajax({
type:"POST",
url:"url.php",
data: { name: "John", location: "Boston" },
}).done(function(data){
// this is very simple to get or post data
$("#response").html(data);
})

Upvotes: 3

Devnegikec
Devnegikec

Reputation: 631

you can also use this

dataValues = "a=1&b=2";
$.post("your_php_page.php", dataValues, function(data){

},"json");

//in your php file "your_php_page.php"

$a = $_POST['a'];

//you can also return data in json fomat in this way

$returnData = array();
echo json_encode($returnData); 

Upvotes: 0

Kichu
Kichu

Reputation: 3267

Try this:

function test_form(){
    var cnt = $("#formid").serialize();
    $.ajax({
    method: 'POST',
    url: 'url to your function',
    data: cnt,
    success: function(msg){
        //required code
    }
    });
}

Upvotes: 0

Blender
Blender

Reputation: 298392

The cleanest way to do it would be to just use jQuery's nifty $.post() function:

$(document).ready(function() {
    $('#response').hide();

    $('#addroom').click(function() {
        $('<img />', {src: 'ajax-images/ajax-loader.gif'}).appendTo('#response');

        $.post({
            url: 'scripts/addnew_room.php',
            data: {
                rm_num: $('#rm_num').val(),
                rm_type: $('#rm_type').val()
            },
            success: function(response) {
                $('#response').html(response);
            }
        });
    });
});

Upvotes: 2

Deepak
Deepak

Reputation: 6812

Or something like this

ajax = $.ajax({
    type: "POST",
    url: "your_php_page.php",
    data: "a=1&b=2",
    success: function(msg){     
        //success handler
    },
    error: function(msg){
       //error handler
    }
});

Upvotes: 0

Related Questions