I have a table with three fields. Two are dropdown and one is input field. I have to compare value of input field and stock quantity based on dropdown field.
How to do with ajax?
function compareData() {
var quantity = document.getElementsByName('quantity[]');
var stockName = document.getElementsByName('stockName[]');
var oldQnt = [];
$.ajax({
url: 'php_action/fetchStockas.php',
type: 'post',
async: true,
data: { stock: stockName },
dataType: 'json',
success: function(data) {
oldQnt.push(data);
}
});
for (var x = 0; x < quantity.length; x++) {
alert(quantity[x].value);
// alert(oldQnt[x].value);
}
}
PHP code:
<?php
require_once 'core.php';
$stock=$_POST['stockNmame'];
$sql = "SELECT st_id, st_item_name, st_price, st_quantity FROM stock WHERE st_item_name='$stock'";
$result = $connect->query($sql);
$output = array('data' => array());
if($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$stockId = $row[0];
$stockName=$row[1];
$stockPrice=$row[2];
$stockQuantity=$row[3];
$output['data'][] = array( $stockName, $stockPrice, $stockQuantity, );
}
}
$connect->close();
echo json_encode($output);