Make sure to do that on the server since anyone can see the code in the source of the page. Also $price is on the server and the client value is not available to the server until you send a request back to the server using for example AJAX
So something like
<form action="purchase.php" id="form1">
Promotion code: <input type="text" name="promotion_code" id="prCode" />
<input type="button" id="prCodeBut" value="Apply" />
Price: <input type="text" readonly="readonly"
name="finalprice" id="fPrice" value="<?PHP echo $price; ?>" />
</form>
JavaScript
DEMO 1 - using click
var orgPrice = <?PHP echo $price; ?>;
window.onload=function() {
document.getElementById("prCodeBut").onclick=function(){
// hardcoding the promocode is NOT recommended
if (document.getElementById("prCode").value == "<?php echo $promocode; ?>") {
document.getElementById("fPrice").value=orgPrice-10;
}
}
}
DEMO 2 using onkeyup
var orgPrice = <?PHP echo $price; ?>;
window.onload=function() {
document.getElementById("prCode").onkeyup=function(){
// hardcoding the promocode is NOT recommended
if (document.getElementById("prCode").value == "<?php echo $promocode; ?>") {
document.getElementById("fPrice").value=orgPrice-10;
}
}
}
DEMO 3 using onblur
var orgPrice = <?PHP echo $price; ?>;
window.onload=function() {
document.getElementById("prCode").onblur=function(){
// hardcoding the promocode is NOT recommended
if (document.getElementById("prCode").value == "<?php echo $promocode; ?>") {
document.getElementById("fPrice").value=orgPrice-10;
}
}
}
Ajax to hide the promocode
// delay from http://stackoverflow.com/a/1909508/295783
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(function() {
$("#prCodeBut").on("keyup",function() {
delay(function() {
$.get("checkPromo.php",{promotion: $("#prCode").val()},function(data) {
$("#fPrice").val(data);
});
},300);
});
});