选择下拉列表后更新脚本formData值

时间:2019-05-10 02:33:09

标签: jquery

我有一个下拉列表,其中列出了已登录客户的所有可用送货地址。用户选择选定的送货地址后,需要使用用户选定的送货地址更新脚本标签中的Paypal formData。我该如何执行此操作?我对如何开始一无所知。

/*jQuery code for drop down list*/
$(document).on('change', '#shipAddSelect', function(){
    $.ajax({
        type: 'POST',
        url: 'shoppingbag.php',
        data: {'selectedShipAdd': $('#shipAddSelect').val()},
        dataType: 'json',
        cache: false,
        success: function(data){
            $('.flex-container-row.form-text.displayShip').html(data.shipFirstName+" "+data.shipLastName+'<br>'+
                data.shipBlock+" "+data.shipStreet+"<br>"+"#"+data.shipUnit+"<br>"+data.shipBuilding+"<br>"+data.shipCity+" "+data.shipState+"<br>"+data.shipCountry+" "+data.shipPostal);
        },
        error: function(){ // display error response(i.e, server timeout etc) from the PHP script
        }
    });
});


<?php
// AJAX call this code to display selected shipping address
if(isset($_POST['selectedShipAdd']) && $_POST['selectedShipAdd'] !== ""){


$selectedShipAdd = preg_replace('#[^0-9]#i', '', $_POST['selectedShipAdd']);

$sql = "SELECT * FROM ShippingAdd WHERE PK_ShippingAddID ='$selectedShipAdd' AND FK_CustomerID = '$log_id' LIMIT 1";
$query = mysqli_query($connection, $sql);
$rowCount = mysqli_num_rows($query);
if ($rowCount > 0){
    while($row = mysqli_fetch_array($query)){
        $shipFirstName = $row["ShipFirstName"];
        $shipLastName = $row["ShipLastName"];
        $shipBlock = $row["ShipBlock"];
        $shipBuilding = $row["ShipBuilding"];
        $shipStreet = $row["ShipStreet"];
        $shipUnit = $row["ShipUnit"];
        $shipPostal = $row["ShipPostal"];
        $shipCity = $row["ShipCity"];
        $shipState = $row["ShipState"];
        $shipCountry = $row["ShipCountry"];         
    }   
}

echo json_encode(array("shipFirstName" => $shipFirstName, 
                        "shipLastName" => $shipLastName,
                        "shipBlock" => $shipBlock,
                        "shipBuilding" => $shipBuilding,
                        "shipStreet" => $shipStreet,
                        "shipUnit" => $shipUnit,
                        "shipPostal" => $shipPostal,
                        "shipCity" => $shipCity,
                        "shipState" => $shipState,
                        "shipCountry" => $shipCountry)
                );

exit();
}
?>

<script>
paypal.Buttons({    

// Set up the transaction
paypal.Buttons({
    createOrder: function(data, actions) {
        const formData = new URLSearchParams();     
            formData.append("ship_name",        <?php echo json_encode($shipFirstName.' '.$shipLastName)?>);
            formData.append("ship_block",       <?php echo json_encode($shipBlock)?>);
            formData.append("ship_street",      <?php echo json_encode($shipStreet)?>);
            formData.append("ship_unit",        <?php echo json_encode($shipUnit)?>);
            formData.append("ship_building",    <?php echo json_encode($shipBuilding)?>);
            formData.append("ship_postal",      <?php echo json_encode($shipPostal)?>);
            formData.append("ship_city",        <?php echo json_encode($shipCity)?>);
            formData.append("ship_state",       <?php echo json_encode($shipState)?>);
            formData.append("ship_country",     <?php echo json_encode($shipCountryCode)?>);
        return fetch('paypal_createOrder.php', {
            method: 'post',
            headers:{
                "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
            },
            body:formData
        }).then(function(res) {
            return res.json();
        }).then(function(data) {
            return data.orderID;
        });
    },
}).render('#paypal-button-container');
</script>

第1步:用户从下拉列表中选择送货地址。 步骤2:执行所示的jQuery代码,并将“ selectedShipAdd”变量发送到php脚本,如图所示。 PHP将编码后的数组发回给jquery,后者在html div中显示值。

***步骤1和步骤2完美运行 步骤3:Paypal formData值应在步骤2之后更新

***步骤3不起作用

0 个答案:

没有答案