Я создаю скрипт для создания счетов. Использование ajax для заполнения данных базы данных при автопоиске. Но у продуктов есть поле с именем min_selling_price, пользователь не должен иметь возможность выставить цену ниже min_selling_price. Поскольку мои записи являются динамическими c, я не понимаю, где и как это проверить.
Вот моя форма
<div class="table-responsive">
<table class="table table-active table-bordered table-sm">
<thead class="thead-active"><tr>
<th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
<th>Name</th>
<th>Description</th>
<th>UOM</th>
<th>Price</th>
<th>Qty</th>
<th colspan="2"></th>
</tr> </thead>
<tr>
<td><input type='checkbox' class='case'/></td>
<td><input type="text" class="form-control form-control-sm" id="productname_1" name="productname[]" required style="width:120px;"></td>
<input type="hidden" class="form-control" id="productcode_1" name="productcode[]">
<td><textarea class="form-control form-control-sm" id="description_1" name="description[]"></textarea></td>
<td><select name="uom[]" class="form-control form-control-sm" id="uom_1" style="width:80px;">
<option value="">UOM</option>
<?php $su1 = mysqli_query($con, "select * from uom"); while($su2 = mysqli_fetch_array($su1)) {
$uoptions .= "<option value='". $su2['uom_name'] . "'>" .$su2['uom_name'] . "</option>";
?>
<option value="<?php echo $su2['uom_name']; ?>"><?php echo $su2['uom_name']; ?></option>
<?php
}
?>
</select>
</td>
<td><input type="text" class="form-control form-control-sm price" required id="price_1" name="price[]"></td>
<td><input type="text" class="form-control form-control-sm quantity" required id="quantity_1" name="quantity[]"></td>
<input type="hidden" class="form-control amount" id="amount_1" name="amount[]">
<td><button type="button" class='btn btn-danger delete'>-</button></td>
<td><button type="button" class='btn btn-success addmore'>+ </button></td>
</tr>
</table>
</div>
<script type="text/javascript">
var options1 = "<?= $uoptions; ?>";
</script>
<script type="text/javascript" src="js/auto.js"></script>
auto. js
$(".delete").on('click', function() {
$('.case:checkbox:checked').parents("tr").remove();
$('.check_all').prop("checked", false);
check();
});
var i = $('table tr').length - 1;
$(".addmore").on('click', function() {
count = $('table tr').length - 1;
var data = "<tr><td><input type='checkbox' class='case'/></td><td><input class='form-control form-control-sm' type='text' id='productname_" + i + "' name='productname[]' required /></td><input class='form-control' type='hidden' id='productcode_" + i + "' name='productcode[]'/><td> <textarea class='form-control form-control-sm' id='description_"+ i + "' name='description[]'></textarea></td><td><select class='form-control form-control-sm uom' id='uom_" + i + "' name='uom[]'><option value=''>UOM</option>" + options1 + "</select></td><td><input class='form-control form-control-sm price' required type='text' id='price_" + i + "' name='price[]'/></td><td><input class='form-control form-control-sm quantity' required type='text' id='quantity_" + i + "' name='quantity[]'/></td><input class='form-control amount' type='hidden' id='amount_" + i + "' name='amount[]'/></tr>";
$('table').append(data);
row = i;
$('#productname_' + i).autocomplete({
source: function(request, response) {
$.ajax({
url: 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'items_table',
row_num: row
},
success: function(data) {
response($.map(data, function(item) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data: item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function(event, ui) {
var names = ui.item.data.split("|");
id_arr = $(this).attr('id');
id = id_arr.split("_");
$('#productcode_' + id[1]).val(names[1]);
$('#description_' + id[1]).val(names[2]);
$('#uom_' + id[1]).val(names[3]);
$('#price_' + id[1]).val(names[4]);
//$('#tax_' + id[1]).val(names[5]);
}
});
i++;
});
function select_all() {
$('input[class=case]:checkbox').each(function() {
if ($('input[class=check_all]:checkbox:checked').length == 0) {
$(this).prop("checked", false);
} else {
$(this).prop("checked", true);
}
});
}
function check() {
obj = $('table tr').find('span');
$.each(obj, function(key, value) {
id = value.id;
$('#' + id).html(key + 1);
});
}
$('#productname_1').autocomplete({
source: function(request, response) {
$.ajax({
url: 'ajax.php',
dataType: "json",
method: 'post',
data: {
name_startsWith: request.term,
type: 'items_table',
row_num: 1
},
success: function(data) {
response($.map(data, function(item) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data: item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function(event, ui) {
var names = ui.item.data.split("|");
$('#productcode_1').val(names[1]);
$('#description_1').val(names[2]);
$('#uom_1').val(names[3]);
$('#price_1').val(names[4]);
}
});
в ajax. php
if($_POST['type'] == 'items_table'){
$row_num = $_POST['row_num'];
$name = $_POST['name_startsWith'];
$query = "SELECT * FROM items WHERE status='Active' AND name LIKE '".strtoupper($name)."%'";
$result = mysqli_query($con, $query);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['name'].'|'.$row['item_id'].'|'.($row['description']).'|'.$row['uom'].'|'.$row['selling_price'].'|'.$row_num;
array_push($data, $name);
}
header('Content-Type: application/json');
echo json_encode($data);
}
в таблице товаров, у меня есть поле 'min_selling_price', я хочу проверить для каждой позиции, является ли введенная пользователем цена выше, если нет это не должно позволить продолжить. Не понимаешь, как это сделать?