Page 1 of 1
getting variable before submitting the form
Posted: Mon Sep 19, 2005 3:04 am
by dude81
Hello forum,
I need to get a varibale in a form before submitting the form? is it possible? or is there any other way I can implement it.
Sai Krishna
Posted: Mon Sep 19, 2005 3:14 am
by s.dot
its very possible that you do not need to get the variable beore submitting the form. please post your scenario.
however, with javascript, you could.
Posted: Mon Sep 19, 2005 3:15 am
by CoderGoblin
In PHP no, only via Javascript. PHP is based on the web server. Javascript is on the client machine. There are things like AJAX/HttpRequests but they still require Javascript to initialise sending things to the server.
Posted: Mon Sep 19, 2005 3:22 am
by dude81
Yes,
the scenario is as follows.
I have to check the stock is available from the database before submitting the form,
I need the quantity variable from the form saleorder and execute a sql query, but executing sql query is not possible through java script.
Is there any other alternative.
Posted: Mon Sep 19, 2005 3:23 am
by s.dot
why would you need to check it before submitting the form? using a simple query, with an if/else can tell you if stock is available, then you can do what you need to do based on that

Posted: Mon Sep 19, 2005 3:25 am
by phpdevuk
I've seen this sort of thing done with javascript and frames before, you have a hidden form in a frame on the page that submits the data via js to a php page which executes the query and then returns the result via js to the main page. It is a really hideous way to do it tho.
I would probably do it all in php and have the form action page check the stock levels and use a redirect back to the form if there are any probs, you can keep the contents of the form in a session var and fill out all the details from it.
Posted: Mon Sep 19, 2005 3:47 am
by dude81
I cant use If, else condition because I need the variable of the form quantity in the sql query, based on that quantity I need to popup that stock is available or not for the particular consumer. so Is ther way??
Posted: Mon Sep 19, 2005 3:55 am
by dude81
Hey PHP devuk
can you just put a sample code of that i.e
variables going thorugh js to phpand returing via js to php.
Posted: Mon Sep 19, 2005 4:03 am
by CoderGoblin
You could get all the stock levels and place them in a Javascript array when initially loading the page for a "quick check" via Javascript. You still need to check the stock level however when the form is submitted. No matter what you do/How you do it this check is necessary as the submitting of the form is the user saying "Yes I want to do this". Also this provides a check when Javascript is disabled.
Rather than use IFrames or frames I would check out this link as it describes a method called HTTPRequest. [url=
viewtopic.php?t=34665[/url]
Posted: Mon Sep 19, 2005 4:20 am
by dude81
Can you just give a sample code to get the data and store in the array in javascript??

Posted: Mon Sep 19, 2005 4:28 am
by CoderGoblin
Don't have the time at present but similar things are around if you look. E.g
http://forums.devnetwork.net/viewtopic.php?t=29084
Posted: Tue Sep 20, 2005 1:19 am
by dude81
Hi
In the following url,
viewtopic.php?t=34665
In the updateIt() function
in the place of
Code: Select all
reqsend.open("POST", "online.php", true);
can I open a file which exists but I dont want to load the file on to the borwser and still execte the php file,
Also how to pass numerous variables
Code: Select all
var stuff = "upd=1&who=<?=$_SESSION['loggedinid'];?>"
when Im passing various variables to it I get an error in Javascript console saying
Code: Select all
var variables="country=<?$country?>&kit_code=<br />
where the actual code is as follows
Code: Select all
var variables="country=<?$country?> &kit_code=<?$item_code?>&stock_type=<?$stock_type?>";
Posted: Tue Sep 20, 2005 2:51 am
by phpdevuk
do a view source on the page in the browser and see what is displayed for kitcode, in my experience a value of <br> will probably be because that variable has no value and php is displaying a warning emssage for it or something.