Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    passing a javascript value to php using ajax

    i have created a book order form and it calculates the total price as the user checks a checkbox and enters a quantity
    the total is done using a javascript
    here's how my form looks like


    i want to send the value of this total to php(mail.php) which will send this value to the desired mail

    but i am not able to send the total price to the php
    i have tried using ajax
    but still no results

    please if anyone can help me on this
    thanks in advance
    This is a part of my javascript code where the total is calulated
    Code:
    function calculateTotal()
    {
        //Here we get the total price by calling our function
        //Each function returns a number so by calling them we add the values they return together
        var bookPrice =  book1() + book2() + book3() + book4() + book5() + book6() + book7() + book8() + book9() + book10() + book11() + book12() + book13() + book14()+ book15() + book16() + book17() + book18()+ book19() + book20() + book21() + book22() + book23() + book24() + book25() + book26() + book27() + book28() + book29() + book30() + book31() + book32() + book33()+ book34() + book35() + book36()+ book37()+ book38() + book39()+ book40()+ book41() + book42() + book43() + book44() + book45() + book46() + book47() + book48() + book49() + book50() + book51() + book52() + book53() + book54()+ book55() + book56() + book57() + book58()+ book59() + book60()+ book61() + book62() + book63() + book64() + book65() + book66() + book67() + book68() + book69() + book70() + book71() + book72() + book73() + book74()+ book75() + book76() + book77() + book78()+ book79()+ book80()+book81() + book82() + book83() + book84() + book85() + book86() + book87() + book88() + book89() + book90() + book91() + book92() + book93() + book94()+ book95() + book96() + book97() + book98()+ book99()+ book100() + book101() + book102() + book103() + book104() + book105() + book106() + book107() + book108() + book109() + book110() + book111() + book112() + book113();
        
        //display the result
        var divobj = document.getElementById('totalPrice');
        divobj.style.display='block';
        divobj.innerHTML = "Total Price For the Books RS."+bookPrice;
    
    }
    function ajaxRequest() {
    var AJAX = null; // Initialize the AJAX variable.
    if (window.XMLHttpRequest) { // Does this browser have an XMLHttpRequest object?
    AJAX=new XMLHttpRequest(); // Yes -- initialize it.
    } else { // No, try to initialize it IE style
    AJAX=new ActiveXObject("Microsoft.XMLHTTP"); // Wheee, ActiveX, how do we format c: again?
    } // End setup Ajax.
    if (AJAX==null) { // If we couldn't initialize Ajax...
    alert("Your browser doesn't support AJAX."); // Sorry msg. 
    return false // Return false, couldn't set up ajax
    }
    AJAX.onreadystatechange = function() { // When the browser has the request info..
    if (AJAX.readyState==4 || AJAX.readyState=="complete") { // see if the complete flag is set.
    callback(AJAX.responseText, AJAX.status); // Pass the response to our processing function
    } // End Ajax readystate check.
    }
    var url='www.yehtechnologies.com/raghav/orderr/mail.php?bookprice=10'; // This is the URL we will call.
    AJAX.open("GET", url, true); // Open the url this object was set-up with.
    AJAX.send(null); // Send the request.
    }

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Your line
    Code:
    var url='www.yehtechnologies.com/raghav/orderr/mail.php?bookprice=10';
    is sending 10 as the price of all books every time your invoking the ajaxRequest() function.

    do this instead:

    Code:
    var price = document.getElementById('totalPrice').innerHTML;
    var url='www.yehtechnologies.com/raghav/orderr/mail.php?bookprice='+price;
    in your mail.php you have to set a variable to $_GET['bookprice'] and then filter it. Because your price is a string => "Total Price For the Books RS."+bookPrice;

    The "Total Price For the Books RS." must be removed.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply
    i did as you mentioned
    and also removed "total price for the book Rs"

    but it doesnt work

    in my mail.php
    i have

    $total = $_GET['bookprice'];
    echo $total;(to check if the value is reaching..but nothing gets echod)

    $message .= "Total Price :$total\n";(to send it in the mail..but in mail also Total price :
    (is blank))

    is their anything else that needs to be done

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,763
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Make sure the file "www.yehtechnologies.com/raghav/orderr/mail.php" is where you say it is and then add this as the first line in it: "echo 'made it';die;" and see what happens. Your browser goes blank when you submit and 'made it' should appear on your screen.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •