0

now i have a text-input and a php variables.

$price = 200;
<input type="text" name="promotion_code">

after users type in value of 'promotion_code' , without submit button , or any links.
it's can match 'promotion_code' between javascript on that pages. lets say the javascript :

var str="123203";

so i want match the 'promotion_code' with 'var str' .
if match then $price = $price - 10;
if not match then $price value doesn't change .. any could helps or any methods to solve this ?

note :!! to be told. this form did not have a submit button .. the compare process should be works in time when the user input the promotion_code

kampung30
  • 25
  • 1
  • 9
  • 1
    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 – mplungjan Apr 23 '13 at 08:57
  • 1
    You have to decide whether to do the calculation and check on the server or the client. Either way you have to transfer one value to the other. If you want to do it on the server (which I would recommend for security reasons), you could do that via AJAX to avoid having to reload the site. – Till Helge Apr 23 '13 at 08:58
  • @mplungjan just to testing... with make a tight security if i can figure out this ways.. – kampung30 Apr 23 '13 at 08:59

2 Answers2

1

You should not do that with JavaScript, it is no secure enough. Anyone can view your JavaScript code by looking at the source of your page.

Try to make it with an ajax request.

jQuery.ajax({
    type : "post",
    url : "request.php",
    data : "code=" +jQuery("input[name=promotion_code]").val(),
    success : function(ret){
        jQuery("#price").text(ret);
    }
});

Here you call the page request.php with the value code

Here what can be request.php :

$price = 200;
$code = $_POST["code"];
if( $code == "123203" ){
    $price -= 10;
}
return $price;

Then you update the price field with the data returned by the request.php page.

You have to adapt that code of course. I hope this can help.

Lucien Baron
  • 115
  • 6
  • This is saying what not to do when a viable solution is available. You should give an example of how to do it correctly, or make this a comment. ^^ – Jon Apr 23 '13 at 09:02
  • Anyways.. i just want to know what the methods used in javascript to match the text-input value with var of javascript. any idea ? it's sense direct when u user type something without button or links. – kampung30 Apr 23 '13 at 09:07
1

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);
  });
});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • between i not really understand the jQuery code.. can show in javascript ? although it's not recommended ? – kampung30 Apr 23 '13 at 09:12
  • for the javascript part... is it the process work's only with the button submit ? anyways to validate the text-input with just only input and without submit ? – kampung30 Apr 23 '13 at 09:20