function Min(ID) {
$Number = document.getElementById(ID);
if ($Number.value < 1 && $Number.value !== "") {
$Number.value = 1;
}
}
echo '
<form id="' . $Item2 . '">
<input type="image" src="AddToCart.png" style="margin:5px; float:left; font-size:25px;" width="65px" height="33" id="' . $EAN2 . '"
onmouseup="KeyUp(this.id)" onmousedown="Click(this.id)"/>
<input type="number" id = "' . $Image .'" name = "Amount" min="1" onKeyUp="Min(this.id)">
<input type="text" name = "ID" value="' . $Item . '" readonly style="display: none;">
<input type="text" name = "Cost" value="' . $Cost . '" readonly style="display: none;">
<input type="text" name = "Kom" value="' . $Kom . '" readonly style="display: none;">
<input type="text" name = "EAN" value="' . $EAN . '" readonly style="display: none;">
<input type="text" name = "Type" value="Food/Slatko" readonly style="display: none;">
<input type="text" name = "Image" value="' . $Image . '" readonly style="display: none;">
<input type="text" name = "Account" value="' . $_SESSION["Account"] . '" readonly style="display: none;">
</form>';
<script>
echo "
<script>
$('#" . $EAN2 . "').on('click',function(event){
event.preventDefault()
$.ajax({
type: 'get',
url: 'ItemProcessor.php',
data: $('#" . $Item2 . "').serialize(),
})
})
</script>";
I have this form here. It normally stops the user from inputting less than 1 into the input, but because of the ajax code it sends it regardless.
I have tried adding a function onkeyup that checks the value and sets it to 1 if it’s lower than it, but the user can press the image while not releasing the key to still have a negative number.
2
Answers
I would suggest just adding the validation before the
.ajax
call. It will validate it regardless of the event:Instead of keyup, i would use input since it check as soon as an input is detected :
But in any case you should check the value again in ItemProcessor.php and not spend too much time on the front side, as there are lot of ways to go through a JS or HTML restriction (you’re sending data with Ajax, using GET method, it would be really simple)