Javascript & PHP
Author
Discussion

BliarOut

Original Poster:

72,863 posts

269 months

Tuesday 5th April 2005
quotequote all
Right, I have a select list that I want to use to set a PHP variable..... But for reasons best known to myself, I don't want to submit the form.

I was going to use the "onchange" property to populate my variable, but I just can't get it to work.

I would have thought the following would do it, but it doesn't (I have omitted bracketing to get it to post!!).

My Code said:

select name="search" width = "50" onChange=" ?php $widget; = document.searchform.search.value"


Any javascript experts know how I can populate my variable when the select list is selected? It's doing my box in

KITT

5,345 posts

271 months

Tuesday 5th April 2005
quotequote all
I don't think you can use Javascipt to set a PHP variable without submitting a form.

By the time the Javascript runs, your PHP will have already been executed as it's a server-side language.

zaktoo

1,401 posts

270 months

Tuesday 5th April 2005
quotequote all
That is correct. You may be able to do simple http requests via javascript though, and use that to communicate with your back-end (stop giggling you at the back). IMHO whatever you're trying to do, this is not the best way to go about it...

Ciao

Zak

BliarOut

Original Poster:

72,863 posts

269 months

Tuesday 5th April 2005
quotequote all
I'm open to any suggestions!!!

All I want to do is use what's in my dynamic select list to set a session variable on the page it ends up on. However, because I am not submitting the form, I can't use a $_POST

I can make the form submit itself easy enough with something like

onchange="searchform.submit();"

whenever what's in the list changes and it works... But then the list doesn't reflect what's actually been chosen.

zaktoo

1,401 posts

270 months

Tuesday 5th April 2005
quotequote all
BliarOut said:
I'm open to any suggestions!!!

All I want to do is use what's in my dynamic select list to set a session variable on the page it ends up on. However, because I am not submitting the form, I can't use a $_POST


Naturally... let me see if I have this straight: you want to choose a variable from a list and make that variable know to whatever page is next loaded without actually submitting anything back to the server? If that is the case, you'll have to do some javascript fiddling - IMHO not a good idea.

BliarOut said:
I can make the form submit itself easy enough with something like

onchange="searchform.submit();"


Javascript-submitted forms are the devil's own work... ;-)

BliarOut said:

whenever what's in the list changes and it works... But then the list doesn't reflect what's actually been chosen.


Yes, you'd need to filter it out server-side, and add the appropriate html tag (either <option... ... selected> or Moption ... ... selected="selected"> depending on your html flavour) in the form elements.

Is there a url where I can take a look at this for you?

Ciao

Zak

BliarOut

Original Poster:

72,863 posts

269 months

Tuesday 5th April 2005
quotequote all
Not right now i'm afraid.... It's hiding somewhere on PHPFreaks, but I've got to nip out to site.... I'll post back when I get a mo

size13

2,033 posts

287 months

Tuesday 5th April 2005
quotequote all
I didn't think you could do it as Javascipt is client-side and PHP server-side.

Only a submit would feed the data back to your PHP script

BliarOut

Original Poster:

72,863 posts

269 months

Tuesday 5th April 2005
quotequote all
Sod it, I'll submit the form

All I need to do is make my buttons graphical and lay them out and it'll still work. I just like to save a click wherever possible

fatsteve

1,143 posts

307 months

Tuesday 5th April 2005
quotequote all
BliarOut said:
Sod it, I'll submit the form

All I need to do is make my buttons graphical and lay them out and it'll still work. I just like to save a click wherever possible


You won't will save a click, since when the select list is changed, the page will submit automatically (bit of JS), PHP can then process the contents of the $_POST collection and re-display the form again complete with original data + the changes made.

Sadly, there is no way round this without submit. The classic example is where you have combo boxes or lists that are dynamically populated from other lists, eg a combo box of Car Makes, once selected populates the Car Model list.

However, I've thought, there is a way, but it's messy. It basically involves populating JS arrays from the PHP code and using JS onClick or onChange functions to populate the lists from the arrays...

Oh, fun, fun, fun...

Steve

devdog

165 posts

284 months

Tuesday 5th April 2005
quotequote all
I've used that method for a page with 2 dependent dropdown lists

when the page is created a JS array is set up with all possible entries for dropdown 2

when an item from dropdown 1 is selected the onchange calls a js function to clear dropdown1 and repopulate it with new data.