getting variable before submitting the form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

getting variable before submitting the form

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post 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.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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??
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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]
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post by dude81 »

Can you just give a sample code to get the data and store in the array in javascript?? :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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?>";
User avatar
phpdevuk
Forum Contributor
Posts: 220
Joined: Mon Jul 04, 2005 5:31 am
Location: UK
Contact:

Post 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.
Post Reply