0

I want to apply a promocode for RazorPay purchase and if someone applied a valid code then show them a discounted amount. for that I have to update my php variable value. I have tried all possible solutions, but nothing worked. I am able to change html value as per Ajax response, but how can I update my php value?

This is my php page - buy.php

<?php $amount = 2500; ?>

<p>Get 1 Year for<br> &#8377; <span id="price"><?php echo $amount; ?</span></p>

<form id="promoForm" action="pay.php" method="POST">
    <input type="text" name="code" placeholder="Have a Promo Code?" required />
    <button id="applybtn" type="submit">Apply</button>
</form>

<p class="errormsg"></p>

<?php include"pay.php"; ?>

So, if someone apply a promocode, I am checking for the code from database via ajax call like this -

<script>    
$("#promoForm").submit(function(event){

    event.preventDefault(); //prevent default action 
    var post_url = $(this).attr("action"); //get form action url
    var request_method = $(this).attr("method"); //get form GET/POST method
    var formdata = $(this).serialize(); //Encode form elements for submission

    $.ajax({
        url : post_url,
        type: request_method,
        data : formdata,
        success: function(response){
            if(response.includes("codeApplied")){

            $('#applybtn').prop('disabled', true);
            ogprice = $("#price").text();  // Get price value

            //calculate percentage
            function fpercent(quantity, percent)
            {
                return quantity * percent / 100;
            }
            discount = fpercent(ogprice, 5);
            offerPrice = ogprice - discount.toFixed(2);

            $("#price").text(offerPrice);  //Set discounted price value

            response = 'Your Code Applied Successfully!';
            $('.errormsg').html('<span style="color:green;">'+response+'</span>');

          }else{
                response = 'This code does not exist';
                $('.errormsg').html('<span style="color:red;">'+response+'</span>');
          }
        }

    });
});

</script>

Here is my pay.php to check the code from db and if the code applied successfully then use discounted amount on click buy button (buy button is a razorpay-payment-button)-

<?php

include('dbconnect.php');

$codeApplied = 'false';
if($_SERVER["REQUEST_METHOD"] == "POST"){
$code = $_POST['code'];

// Check if code exist
$exist = mysqli_query($con, "SELECT `code` FROM `promocodes` WHERE `code` = '".$code."'");
$cnt = mysqli_num_rows($exist);

if($cnt > 0){
    $msg = 'codeApplied'; //'Your Code Applied Successfully!';
    $codeApplied = 'true';
}else{
    $msg = 'failed'; //'This code does not exist';
}

echo $msg;
echo $codeApplied;
}

if($codeApplied == 'true'){
    $amt = 2375;    
}else{$amt = 2500;}

echo $amt;
?>

Till now, I am getting all the expected result. The HTML value of amount also changed as per discount. But the php varible $amount still have the original value (2500).

Can anyone help me with this?

4

1 回答 1

0

你需要知道php是一种服务器语言,当你请求一个页面时,服务器会运行这个php并将其转换为html所以你不能在解析后通过html和javascript更改php变量你必须在服务器端查看当前用户是否应用了促销代码,可能会将其添加到会话中,然后在用户提交时再次进行计算,因为变量 $amount 不会改变

于 2019-03-04T13:15:37.243 回答