I have a form that hides the submit button until a selection is made. If the selection is invalid (ie. out of stock item, or product unavailable), it won't display the submit button.
HTML/PHP:
<select id="choice" name="conc" required="required" onChange="appearButton();">
<option selected="selected" value="" disabled="disabled">select an option</option>
<option value="0">choice</option>
</select>
Qty: <input type="text" size="2" maxlength="2" value="1" name="qty" required="required"/><br/>
<span id="availability">Select Option to see Stock Availability</span><br/> <?php
//SQL related to checking if stock is avail. or not would go here
if ($checkstock->rowCount() == 0) {
echo "This item is currently unavailable.";
} else {
echo '<span id="cartbutton"></span>';
} ?>
JS:
function appearButton() {
if (($("#availability").text() != "Out of Stock")
&& ($("#availability").text() != "Select Options to see Stock Availability")) {
$("#cartbutton").html('<input type="submit" value="Add to Cart"/>');
} else {
$("#cartbutton").text("");
}
}
#availability
changes depending on #choice
dropdown selection, grabs a value from a JSON object.
My concern is that despite a submit button being nonexistent if the selection is invalid, a user can go into the qty
field, press enter, and it will submit despite no submit button.
How best to disable form submission on return in specific instances? I want to form submit on enter if the item is valid, but if it isn't I don't want the option to be available.