Achira silva
Achira silva

Reputation: 11

How to pass jquery variable value to php variable using ajax?

This is my javascript code. In this i declared a variable called total_price. I want to pass this variable value to php variable using ajax. How to do this? I would be grateful if anyone can help me to do this. Thanks. Can anyone to do this using this code.

$(document).ready(function() {
    $('#roomOptions #select1, #select2, #select3').change(function() {
        var total = 0;
        var total1=0;
        var Adult_price =0;
        var total2=0;
        var children_price=0;
        var total_price=0;
        $('#roomOptions #select1').each(function() {
            total+=parseInt($(this).val());  
        }); 
        $('#roomOptions #select2').each(function() {
            total1+=parseInt($(this).val());
        }); 
        $('#roomOptions #select3').each(function() {
            total2+=parseInt($(this).val()); 
        });

        Adult_price = total * total2;
        children_price = (total1 * total2) / 2;
        total_price = (Adult_price + children_price + room_rate) * no_of_nights ;

        $('#roomOptions #roomOptions_total3').html(Adult_price);
        $('#roomOptions #roomOptions_total4').html(children_price);
        $('#roomOptions #roomOptions_total').html(total);
        $('#roomOptions #roomOptions_total5').html(total_price);
        $('#roomOptions #roomOptions_total1').html(total1);
        $('#roomOptions #roomOptions_total2').html(total2);
        $('#roomOptions #roomOptions_total6').html(room_rate);
    });
});

Upvotes: 1

Views: 1119

Answers (1)

Mohammad
Mohammad

Reputation: 21489

You can use JQuery $.ajax or $.post methods to send your data to server.

file.js

$.ajax({
    url: "/folder/file.php", // php file path
    method: "POST", // send data method
    data: {"total_price": total_price}, // data to send {name: value}
    success: function(response){} // response of ajax
});

You must use sended data name to get data in php.

file.php

$total_price = $_POST["total_price"];

Upvotes: 1

Related Questions