Question with code from: Enter $value to a field and depending on the value, modify another field
Thanks to @Marcos I solved the previous linked question.
Now I have:
id_producto = 1;
$stock_c = (int) $_POST['cantidad']; // 0 a 100
if ($stock_c < 0) { $stock_c = 0; }
else if ($stock_c > 100) { $stock_c = 100; }
$stock = $stock_c > 0 ? 'si' : 'no';
// Update
$sql = "UPDATE productos
SET stock_c = $stock_c,
$stock = '$stock'
WHERE id_producto = $id_producto";
mysqli_query($cnx, $sql);
Suppose the customer wants to **buy x amount*
<input type="number" name="cantidad" placeholder="¿Cuantos desea comprar?
How do I set a limit to the quantity input according to $stock_c?
If you $stock_c = 0
deactivate the input (that is, there is no stock)
One idea is to do the following;
You can add the tributes
min
andmax
to your inputThe minimum and maximum can be dynamic depending on your stock
already with JavaScript you can add the attribute
readonly
if the stock is 0. Something like;is what occurs to me.
As @Yo Albert says, I solved it with
min
andmax
in the input.Using php:
Since my question is from PHP , the solution I thought of was this.
For the topic of "disabling input" I thought of doing a
if