Page 1 of 1

How do I set a PHP variable from Javascript?

Posted: Thu Mar 02, 2006 7:55 pm
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

Posted: Thu Mar 02, 2006 9:02 pm
by neophyte
Sounds like you need AJAX. There's some good tutes for that in the tutoral section.

Posted: Thu Mar 02, 2006 9:06 pm
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.

Posted: Thu Mar 02, 2006 9:28 pm
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...

Posted: Fri Mar 03, 2006 7:28 am
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

Thanks, but...

Posted: Fri Mar 03, 2006 11:08 am
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