-1

I have tested all the code, it is fine except calc_price() that isn't executed even when I deleted all the code in function body except message.

<?php
include("header.php");
?>
<script type="text/javascript">

function calc_price()
{
    alert('ooooooooooooooooooooooooooooo');
    var pro_qty=<?php echo($row['pro_qty']);?>;
    var price=document.getElementById('pro_price').value;
    var count=document.getElementById('pro_qty').value;
    var total_price;

    if(count>pro_qty)
        {alert('تعداد موجودی انبار کمتر از درخواست شماست');
         document.getElementById('pro_qty').value=0;
         count=0;
        }
    if(count==0 || count='') total_price=0;
    else total_price=count*price;

    document.getElementById('total_price').value=total_price;

}

</script>


<form action="action_order.php" method="post" name="order">
  <p> 
    <label for="textfield3">تعداد درخواستی</label>
    <input type="text" name="pro_qty" id="pro_qty" onChange="calc_price();">
  </p>
    <input type="submit" name="button" id="button" value="خرید محصول">
  </p>
</form>


<?php
include("footer.php");
?>
  • try, oninput instead of onchange event `` – Ashish Ranjan Mar 27 '19 at 15:05
  • 1
    Check your browser console for JavaScript errors. Also, if the problem is in the client-side code then only look at the client-side code. This PHP code prevents us from seeing the actual JavaScript completely. – David Mar 27 '19 at 15:05
  • as @David says there must be a JS syntax error if the alert isn't happening, so check the console, and check what your rendered JS is. Most likely `$row['pro_qty']` is null or something which is causing a JS syntax error. – Robin Zigmond Mar 27 '19 at 15:08
  • 1
    You have a typo `count=''` change it to `count==''` – ponury-kostek Mar 27 '19 at 15:11
  • I don't think onChange on a text input field will do anything unless enter is pressed, and in this case, pressing enter will likely submit the form. You could try `onkeyup` instead. If that's not the issue, then paste the code from source (right click view source and copy paste this function), this way we can see if the values you set with php are not causing a type error - though you can check for errors using the console of your browser. – Kevin Vandenborne Mar 27 '19 at 15:48

1 Answers1

0

I have made a quick test, onchange or onChange are working both fine. The function what you attach as an event triggers when you leave the focus from the input. You can read further about onchange event in this link. HTML is not considered case sensitive but it is a good practice to keep it lowercase (Source: Is HTML case sensitive?).

If you would like to trigger on each key press your function just change the event to on key up like the below example:

<input type="text" name="pro_qty" id="pro_qty" onkeyup="calc_price()">

Source: onkeyup Event

norbitrial
  • 14,716
  • 7
  • 32
  • 59