SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ottawa, Ont. Canada
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Having Trouble Passing Php Variable To Javascript

    Hello

    I'm an experienced PHP programmer but a newbie in Javascript.

    I have a Javascript snippet that calculates the sales tax for Ontario, Canada and it works.

    But I now have to expand the calculation to the other provinces of Canada and exclude other countries since the client is an Ontario, Canada based company.

    When I try to pass my PHP country variable to the Javascript section I always get an error.

    co = <? echo $country; ?>;

    I get an error on Line 106 but the autosuma.js only has 83 lines????

    Here is the autosuma.js script:

    Any help would be appreciated.

    Thank you
    _____________________________________________

    function startCalc(){
    interval = setInterval("calc()",1);
    }
    function calc(){
    a = 299.00;
    b = document.icf_join.bb_qty.value;
    c = 1195.00;
    d = document.icf_join.emerald_qty.value;
    e = 1695.00;
    f = document.icf_join.garnet_qty.value;
    g = 2595.00;
    h = document.icf_join.sapphire_qty.value;
    i = 4295.00;
    j = document.icf_join.ruby_qty.value;
    k = 6695.00;
    l = document.icf_join.gold_qty.value;
    m = 8295.00;
    n = document.icf_join.diamond_qty.value;

    o = document.icf_join.bb_value.value = (a * 1) * (b * 1);
    p = document.icf_join.emerald_value.value = (c * 1) * (d * 1);
    q = document.icf_join.garnet_value.value = (e * 1) * (f * 1);
    r = document.icf_join.sapphire_value.value = (g * 1) * (h * 1);
    s = document.icf_join.ruby_value.value = (i * 1) * (j * 1);
    t = document.icf_join.gold_value.value = (k * 1) * (l * 1);
    u = document.icf_join.diamond_value.value = (m * 1) * (n * 1);

    co = <? echo $country; ?>;

    z = 0.08; // PST
    v = 0.05; // GST
    a1 = 0.039; // PayPal

    w = document.icf_join.subtotal.value = (o * 1) + (p * 1) + (q * 1) + (r * 1) +(s * 1) + (t * 1) + (u * 1);
    zz = document.icf_join.pst.value = (z * 1) * (w * 1) ;
    x = document.icf_join.gst.value = (v * 1) * (w * 1) ;

    chosen = "";
    c1 = 0;
    len = document.icf_join.pay.length;

    for (i = 0; i <len; i++)
    {
    if (document.icf_join.pay[i].checked)
    {
    chosen = document.icf_join.pay[i].value;
    }
    }
    if(chosen == "Paypal")
    {
    c1 = w * a1;
    }
    else
    {
    c1 = 0;
    }

    y = document.icf_join.total.value = (w * 1) + (x * 1) + (zz * 1) + (c1 * 1);

    document.icf_join.bb_value.value = o.toFixed(2);
    document.icf_join.emerald_value.value = p.toFixed(2);
    document.icf_join.garnet_value.value = q.toFixed(2);
    document.icf_join.sapphire_value.value = r.toFixed(2);
    document.icf_join.ruby_value.value = s.toFixed(2);
    document.icf_join.gold_value.value = t.toFixed(2);
    document.icf_join.diamond_value.value = u.toFixed(2);

    document.icf_join.subtotal.value = w.toFixed(2);
    document.icf_join.pst.value = zz.toFixed(2);
    document.icf_join.gst.value = x.toFixed(2);
    document.icf_join.paypal.value = c1.toFixed(2);
    document.icf_join.total.value = y.toFixed(2);

    }


    function stopCalc(){
    clearInterval(interval);

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    The line number includes the html code as well, if the script is performed inline.

    Try enclosing the country name in quotes

    Code javascript:
    co = '<? echo $country; ?>';
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ottawa, Ont. Canada
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul Wilkins

    Thanks. That cleared the error but when I try to equate the variable to Canada it fails the test:

    Any ideas? I have tried double quotes, single quotes for Canada, I have tried all capitals and first letter Capitals eg Canada, and many other variations. It always equates false. The same situation will be with the state variable.

    Here is the code in Javascript:

    var co;
    var pst_rate;
    var gst_rate;
    var hst_rate;
    var qst_rate;
    var x;
    var c1;


    co = '<? echo strtoupper($country); ?>';

    if(co == 'CANADA')
    {
    var state1 = '<? echo $state; ?>';
    switch(state1)
    {
    case "BC":
    pst_rate = .07;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "AB":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "SK":
    pst_rate = 0.05;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "MB":
    pst_rate = .07;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "ON":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "QC":
    pst_rate = 0.05;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "NB":
    pst_rate = 0;
    gst_rate = 0;
    hst_rate = .13;
    qst_rate = 0;
    break;
    case "NS":
    pst_rate = 0;
    gst_rate = 0;
    hst_rate = .13;
    qst_rate = 0;
    break;
    case "PE":
    pst_rate = .10;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "NL":
    pst_rate = 0;
    gst_rate = 0;
    hst_rate = .13;
    qst_rate = 0;
    break;
    case "NU":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "NT":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "YT":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;


    }
    }
    else
    {
    pst_rate = .2;
    gst_rate = 0;
    }



    a1 = 0.039; // PayPal

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    The php parts should not be in javascript code.
    They should be echo'd from the php code so that the php is able to convert the php variables to javascript code.

    Code php:
    co = '<? echo strtoupper($country); ?>';
     
    if(co == 'CANADA')
    {
    ...

    Resulting in the following javascript code on the page

    Code javascript:
    co = 'CANADA';
     
    if(co == 'CANADA')
    {
    ...
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW. Use <?php instead of just <?

    In order to avoid stupid JS errors write your JS code as .js file. Then replace the needed variable.

    Code JavaScript:
    var country = "{PHP_COUNTRY}";

    In this way you can always test your JS code separately.

    Then do this.

    Code PHP:
    $js = file_get_contents("myfile.js");
    echo str_replace("{PHP_COUNTRY}",$country,$js);

    Hope this helps you out.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ottawa, Ont. Canada
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello

    This will not work for my application since at some times many people are buying at the same time and autosumb.js is the sales tax calculator.

    Here is the complete autosumb.js file. This is an external file that is called/included in the head of the HTML file for the purchasing form.

    function startCalc(){
    interval = setInterval("calc()",1);
    }
    function calc(){
    a = 299.00;
    b = document.icf_join.bb_qty.value;
    c = 1195.00;
    d = document.icf_join.emerald_qty.value;
    e = 1695.00;
    f = document.icf_join.garnet_qty.value;
    g = 2595.00;
    h = document.icf_join.sapphire_qty.value;
    i = 4295.00;
    j = document.icf_join.ruby_qty.value;
    k = 6695.00;
    l = document.icf_join.gold_qty.value;
    m = 8295.00;
    n = document.icf_join.diamond_qty.value;

    o = document.icf_join.bb_value.value = (a * 1) * (b * 1);
    p = document.icf_join.emerald_value.value = (c * 1) * (d * 1);
    q = document.icf_join.garnet_value.value = (e * 1) * (f * 1);
    r = document.icf_join.sapphire_value.value = (g * 1) * (h * 1);
    s = document.icf_join.ruby_value.value = (i * 1) * (j * 1);
    t = document.icf_join.gold_value.value = (k * 1) * (l * 1);
    u = document.icf_join.diamond_value.value = (m * 1) * (n * 1);

    var co;
    var pst_rate;
    var gst_rate;
    var hst_rate;
    var qst_rate;
    var x;
    var c1;


    co = '<? echo strtoupper($country); ?>';

    if(co == 'CANADA')
    {
    var state1 = '<? echo $state; ?>';
    switch(state1)
    {
    case "BC":
    pst_rate = .07;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "AB":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "SK":
    pst_rate = 0.05;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "MB":
    pst_rate = .07;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "ON":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "QC":
    pst_rate = 0.05;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "NB":
    pst_rate = 0;
    gst_rate = 0;
    hst_rate = .13;
    qst_rate = 0;
    break;
    case "NS":
    pst_rate = 0;
    gst_rate = 0;
    hst_rate = .13;
    qst_rate = 0;
    break;
    case "PE":
    pst_rate = .10;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "NL":
    pst_rate = 0;
    gst_rate = 0;
    hst_rate = .13;
    qst_rate = 0;
    break;
    case "NU":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "NT":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;
    break;
    case "YT":
    pst_rate = 0;
    gst_rate = .05;
    hst_rate = 0;
    qst_rate = 0;


    }
    }
    else
    {
    pst_rate = 0;
    gst_rate = 0;
    }



    a1 = 0.039; // PayPal

    w = document.icf_join.subtotal.value = (o * 1) + (p * 1) + (q * 1) + (r * 1) +(s * 1) + (t * 1) + (u * 1);
    zz = document.icf_join.pst.value = (pst_rate * 1) * (w * 1) ;
    x = document.icf_join.gst.value = (gst_rate * 1) * (w * 1) ;

    chosen = "";
    c1 = 0;
    len = document.icf_join.pay.length;

    for (i = 0; i <len; i++)
    {
    if (document.icf_join.pay[i].checked)
    {
    chosen = document.icf_join.pay[i].value;
    }
    }
    if(chosen == "Paypal")
    {
    c1 = w * a1;
    }
    else
    {
    c1 = 0;
    }

    y = document.icf_join.total.value = (w * 1) + (x * 1) + (zz * 1) + (c1 * 1);





    document.icf_join.bb_value.value = o.toFixed(2);
    document.icf_join.emerald_value.value = p.toFixed(2);
    document.icf_join.garnet_value.value = q.toFixed(2);
    document.icf_join.sapphire_value.value = r.toFixed(2);
    document.icf_join.ruby_value.value = s.toFixed(2);
    document.icf_join.gold_value.value = t.toFixed(2);
    document.icf_join.diamond_value.value = u.toFixed(2);

    document.icf_join.subtotal.value = w.toFixed(2);
    document.icf_join.pst.value = zz.toFixed(2);
    document.icf_join.gst.value = x.toFixed(2);
    document.icf_join.paypal.value = c1.toFixed(2);
    document.icf_join.total.value = y.toFixed(2);

    }


    function stopCalc(){
    clearInterval(interval);
    }

  7. #7
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,629
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Any PHP code in an external .js file will NOT be processed by the PHP engine. After the line ...

    co = '<?php echo strtoupper($country); ?>';

    ... put alert (co); and see what you get.

    You need to define co inline in <script> tags in your PHP page, and put this above the <script> tag that calls the autosumb.js file.

    Code:
    <script type="text/javascript">
    var co = '<?php echo strtoupper($country); ?>';
    </script>
    <script type="text/javascript" src="autosumb.js"></script>
    This way the function will recognise the co variable as it has been declared globally.
    Ian Anderson
    www.siteguru.co.uk

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ottawa, Ont. Canada
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Ian Anderson

    Thank you very much.

    I have got the tax calculator working properly now.

    Again Thank you


Bookmarks

Posting Permissions

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