Validation help - In stock check

Discussions related to customizing hooks. Hooks are documented at http://bigprof.com/appgini/help/advanced-topics/hooks/
Post Reply
amminist
Posts: 3
Joined: 2023-08-30 15:41

Validation help - In stock check

Post by amminist » 2023-08-31 10:45

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

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: Validation help - In stock check

Post by pbottcher » 2023-09-03 07:07

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.
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

fdissait
Posts: 23
Joined: 2019-07-24 07:57

Re: Validation help - In stock check

Post by fdissait » 2023-09-10 06:32

ok !
but how retrieve a look-up value from another table using ajax ?
I try several weeks without any success.
François

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1638
Joined: 2018-04-01 10:12

Re: Validation help - In stock check

Post by pbottcher » 2023-09-12 18:50

Hi,
can you explain a bit detailled how your stucture looks like?
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

amminist
Posts: 3
Joined: 2023-08-30 15:41

Re: Validation help - In stock check

Post by amminist » 2023-09-13 08:29

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

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1817
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Validation help - In stock check

Post by jsetzer » 2023-09-13 11:47

W3schools has some nice tutorials:
https://www.w3schools.com/js/js_ajax_intro.asp

There is also help on Bootstrap, for example.
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

amminist
Posts: 3
Joined: 2023-08-30 15:41

Re: Validation help - In stock check

Post by amminist » 2023-11-01 15:32

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

Post Reply