Page 1 of 1

Validation help - In stock check

Posted: 2023-08-31 10:45
by amminist
Hi
I would like to create a script to check if the order quantity is less than the quantity in stock and <500.
I have two tables
Product
ID
Description
Quantity in sotck (As calculated field from child Order - sum of quantity)
...

Order
ID
Product_ID
Quantity (int)
...

Image

thanks to udemy course i created a table-dv.js ( order-dv.js) to verify that the quantity does not exceed 500 ( and it works! :mrgreen: ) but i don't know how to get Quantity_in_stock.Product to compare with the entered quantity.order

Code: Select all


function show_error(field, msg){
	modal_window({
		message: '<div class="alert alert-danger">' + msg + '</div>',
		title: 'Error in ' + field,
		close: function(){
			$j('#' + field).focus();
			$j('#' + field).parents('.form-group').addClass('has-error');
		}
	});

	return false;
}

$j(function(){
	$j('#update, #insert').click(function(){

		/* Quantity < 500 */
		var quantita = $j('#Quantita').val();



		if(isNaN(quantita) || quantita < 500 ){
			return show_error('Quantita', 'Inserire un numero < 500.');
		}


	});
})


Can anyone help me?


Ivan

Re: Validation help - In stock check

Posted: 2023-09-03 07:07
by pbottcher
Hi,

at the time the user enters the Quantity (on change event) you could retrive the Quantity in stock via ajax and check if enough is available.

Re: Validation help - In stock check

Posted: 2023-09-10 06:32
by fdissait
ok !
but how retrieve a look-up value from another table using ajax ?
I try several weeks without any success.
François

Re: Validation help - In stock check

Posted: 2023-09-12 18:50
by pbottcher
Hi,
can you explain a bit detailled how your stucture looks like?

Re: Validation help - In stock check

Posted: 2023-09-13 08:29
by amminist
Same problem, do you know any site to understand the basics of ajax call in java script? I would like to understand and study how to create the call and how to use its result as a js variable.. Thanks
Ivan

Re: Validation help - In stock check

Posted: 2023-09-13 11:47
by jsetzer
W3schools has some nice tutorials:
https://www.w3schools.com/js/js_ajax_intro.asp

There is also help on Bootstrap, for example.

Re: Validation help - In stock check

Posted: 2023-11-01 15:32
by amminist
Hi

after a little bit of study..

In hooks/ajax.php

Code: Select all

<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");


	$pid= makesafe($_REQUEST['PID']);


	$sql = "SELECT Quantity_in_stock FROM Product WHERE ID = '{$pid}'";

	$qis= sqlValue($sql);

	echo $qis;
?>

The ajax code works ( localhost/hooks/ajax.php?pid=1 and it returns the correct value)

Now my problem is, after call and have the value back how to set that value as var QuantityInStock to compare it..

This not work.. in hooks/order-dv.js

Code: Select all

$j(function(){
	$j('#update, #insert').click(function(){
             var ProdID= $j('#Product_ID').val() ;
             var QuantityInStock;
	
		$j.ajax({
			url: 'hooks/ajax.php',
			data: { pid: ProdID},
			success: function (data){
					$j('#QuantityInStock').val(data);
			}
		});
		
			/* now i want to copare QuantityInStock whith Quantity		*/	
	
			if(isNaN(quantita) || quantita < QuantityInStock){
		         	return show_error('Quantita', 'OuT of Stock');
		    	}
			
	});
})

Is it possible to set var QuantityInStock with the result of the ajax call or i'm wrong in this point ?

Ivan