How do I set a PHP variable from Javascript?

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
vidya
Forum Newbie
Posts: 6
Joined: Wed Mar 01, 2006 12:46 pm

How do I set a PHP variable from Javascript?

Post by vidya »

I have a PHP (popup) page which is a list of checkboxes.
I'd like to fill in PHP session variables with the checked results
instantly, as soon as a checkbox is checked (or unchecked).

I.E., I don't want to have a Submit button to gather the results,
as the user could just close the popup after making selections,
without hitting the Submit button.

Thus, I was thinking of using Javascript OnClick on each checkbox.
But then, even if I write a Javascript function as the object of the OnClick,
how do I pass an On or Off value to a PHP variable from Javascript,
as the whole purpose is to store the results in a database field from
the PHP variables?

(Be nice if this was a DUH!! answer....)

Any ideas?

Thanks

....Vidya
http://www.webwisesage.com
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Sounds like you need AJAX. There's some good tutes for that in the tutoral section.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if your end goal is to set session vars, ajax won't do it for you. If you're just looking to update a db, then neophyte is correct, ajax is your answer.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

There's no reason ajax couldn't call a page that only set a session var passed by get or post and returned true or false if it was successful in setting that... The pop-up would likely need to refresh the parent for the parent to read in the session vars. Granted, something doesn't seem right about the question to me...
darryladie
Forum Commoner
Posts: 62
Joined: Thu Mar 02, 2006 6:14 pm
Location: East Sussex, UK

Post by darryladie »

find an AJAX tutorial and you want to have the following function in your checkboxes (I think):

Code: Select all

onclick="setValue(this.value)"
you would need to define setValue using PHP and DOM. Try: http://www.w3schools.com

Good Luck
vidya
Forum Newbie
Posts: 6
Joined: Wed Mar 01, 2006 12:46 pm

Thanks, but...

Post by vidya »

I came up with a better and simple solution.
Simple is always the way-to-go, in my book.

I realized that my whole challenge was to get the
checkbox data even if the user closed the window.
Which is what the user would have to do eventually,
anyways.

So the solution is to have a window without a "close X box".
And instead simulate a close X with a Submit button that looks
like a regular close X button.

Thus, I get the checkboxes data, server-side.

Actually, by a little thought, I also realized that Javascript is client-side
processing, while PHP is server-side processing. So what I wanted
to do was inherently contradictory.

Thanks for your answers.

....Vidya
Post Reply