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?

phpidz
  • 39
  • 2
  • 9

1 Answers1

0

You need to know that php is a server language, When you request a page the server will run this php and transform it into html So you cannot change the php variable through html and javascript after it is parsed You have to make a workaround in the server side to see if the current user applied a promo code maybe add it in the session and then do the calculations again when the user submit because the variable $amount won't change