如何从AJAX获得价格

时间:2017-09-15 23:57:15

标签: javascript ajax paypal

我问了几个问题!我付款的东西,我使用AJAX(当用户更改选择时直接更改价格),但问题是,我不知道如何获得价格(并验证它&#39) ; s good price)插入paypal付款!我尝试使用PHP等会话。但价格值并没有改变:/

的index.php:

<div class="col-lg-12 animated fadeIn">
                            <div class="col-sm-6 col-md-6 col-lg-3 order-actual-rank">
                                <p>Rang actuel</p>
                                <img id="current_league_img" src="assets/img/tiers/silver.png" alt="Silver" class="order-rank-img">
                                <div>
                                    <select name="order-division-actual_league" class="form-control" id="actual_league" onChange="changeCurrentLeague(); getDivPrice()">
                                        <option value="Bronze">Bronze</option>
                                        <option value="Silver" selected>Silver</option>
                                        <option value="Gold">Gold</option>
                                        <option value="Platinum">Platinum</option>
                                        <option value="Diamond">Diamond</option>
                                    </select>
                                </div>
                                <div>
                                    <select name="order-division-actual_division" onchange="getDivPrice()" id="actual_division" class="form-control">
                                        <option value="1">I</option>
                                        <option value="2">II</option>
                                        <option value="3">III</option>
                                        <option value="4">IV</option>
                                        <option value="5" selected>V</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-6 col-md-6 col-lg-3 order-desired-rank">
                                <p>Rang désiré</p>
                                <img id="desired_league_img" src="assets/img/tiers/gold.png" alt="Gold" class="order-rank-img">
                                <div>
                                    <select name="order-division-desired_league" onchange="changeDesiredLeague(); getDivPrice()" id="desired_league" class="form-control" id="desired_league">
                                        <option value="Bronze">Bronze</option>
                                        <option value="Silver">Silver</option>
                                        <option value="Gold" selected>Gold</option>
                                        <option value="Platinum">Platinum</option>
                                        <option value="Diamond">Diamond</option>
                                    </select>
                                </div>
                                <div>
                                    <select name="order-division-desired_division" onchange="getDivPrice()" id="desired_division" class="form-control">
                                        <option value="1">I</option>
                                        <option value="2">II</option>
                                        <option value="3">III</option>
                                        <option value="4">IV</option>
                                        <option value="5" selected>V</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-sm-12 col-md-12 col-lg-6 order-division-order">
                                    <ul>
                                        <li>Division : <span id="division_infos"></span></li>
                                        <li>Prix : <span id="boost_price"></span></li>
                                    </ul>
                                    <select name="order-division-server" class="form-control" style="width: 100px; margin-top: 10px;">
                                        <option value="euw" selected>EUW</option>
                                        <option value="eune">EUNE</option>
                                    </select>
                                    <input type="checkbox" onchange="getDivPrice()" id="order-duo" name="order-division-duo" style="height: 20px; width: 20px; vertical-align: top;">
                                    <label for="order-duo">Duo</label>
                                    <div id="paypal-button" class="form-paypal">
                                    </div><script type="text/javascript" src="assets/js/checkout.js"></script>

 

console.log(data);
 paypal.Button.render({

    env: 'sandbox', // Or 'sandbox'

     style: {
        label: 'generic',
        size:  'small',    // small | medium | large | responsive
        shape: 'rect',     // pill | rect
        color: 'gold',     // gold | blue | silver | black
        tagline: false    
    },

    client: {
        sandbox:    'AYh_cbrBW8_Ic8QLBh_hXPLpzbzFqc4_ckRsnH3cZ60oxheTz3CdZOgxlPK4TMDBF-ZiUR4EomVHMAkx',
        production: ''
    },

    commit: true,

    payment: function(data, actions) {
        return actions.payment.create({
            payment: {
                transactions: [
                    {
                        amount: { total: '0.01', currency: 'EUR' }
                    }
                ]
            }
        });
    },

    onAuthorize: function(data, actions) {
        return actions.payment.execute().then(function(payment) {

            alert("Payement effectué");
        });
    }

}, '#paypal-button');

AJAX:

function getDivPrice() {
    var price = 0;
    var actual_league = document.getElementById("actual_league").value;
    var actual_division = document.getElementById("actual_division").value;
    var desired_league = document.getElementById("desired_league").value;
    var desired_division = document.getElementById("desired_division").value;
    var dataString = 'type=division&actual_league=' + actual_league + 
    "&actual_division=" + actual_division + "&desired_league=" + desired_league +
    "&desired_division=" + desired_division;
    $.ajax({
        type: 'POST',
        data: dataString,
        url: '/FoxBoost/assets/getprice.php',
        success: function (data) {
                var duoqstr = "";
                if (document.getElementById('order-duo').checked)
                {
                    data = data * 1.3;
                    duoqstr = " en Duo"
                    data = data.toFixed(2);
                }
                if(data == 0)
                {
                document.getElementById('boost_price').innerHTML = "";
                document.getElementById('division_infos').innerHTML = "Ce boost n'est pas possible.";
                document.getElementById('hidden_price').value = data;
                }else{
                document.getElementById('boost_price').innerHTML = data + " €";
                document.getElementById('division_infos').innerHTML = actual_league + " " + actual_division + " " + "->" + " " + desired_league + " " + desired_division + duoqstr;
                document.getElementById('hidden_price').value = data;
                }
        }
    });
}

getPrice.php

<?php

$tiers = array(
               "Bronze 5" => 0,
               "Bronze 4" => 1,
               "Bronze 3" => 2,
               "Bronze 2" => 3,
               "Bronze 1" => 4,
               "Silver 5" => 5,
               "Silver 4" => 6,
               "Silver 3" => 7,
               "Silver 2" => 8,
               "Silver 1" => 9,
               "Gold 5"   => 10,
               "Gold 4"   => 11,
               "Gold 3"   => 12,
               "Gold 2"   => 13,
               "Gold 1"   => 14,
               "Platinum 5" => 15,
               "Platinum 4" => 16,
               "Platinum 3" => 17,
               "Platinum 2" => 18,
               "Platinum 1" => 19,
               "Diamond 5"  => 20,
               "Diamond 4"  => 21,
               "Diamond 3"  => 22,
               "Diamond 2"  => 23,
               "Diamond 1"  => 24
               );

$costs = array(
               10,10,10,10,12,
               12,13,14,15,18,
               18,20,22,24,28,
               30,33,36,39,45,
               65,75,85,125
               );

if($_POST['actual_league'] != null && $_POST['actual_division'] != null && $_POST['desired_league'] != null && $_POST['desired_division'] != null) {
    $actual = $_POST['actual_league']." ".$_POST["actual_division"];
    $desired = $_POST['desired_league']." ".$_POST["desired_division"];

    foreach($tiers as $index => $key) {
        if($actual === $index) {
            $actual = $key;
        }
        if($desired === $index) {
            $desired = $key;
        }
    }

    $price = 0;
    if ($actual < $desired) {
        for ($i = $actual ; $i < $desired ; $i++) {
            $price += $costs[$i];
        }
        echo $price;

    }else{
        echo $price;
    }

}else{
    header('Location: /foxboost');
}

?>

0 个答案:

没有答案