1

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);
    });
});
2
  • 4
    Where is your ajax request? If you are asking how to make an ajax request see the jquery docs for ajax Commented May 9, 2015 at 2:59
  • Yes. I need to how to make an ajax request. Please help me Commented May 9, 2015 at 3:23

1 Answer 1

1

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"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.