Jump to content

Recommended Posts

hey,

 

im trying to build a php page which have a javascript for creating 2 dependent dropdown boxes. can anyone tell me how do i transfer a varaible from the java script to the php code?

 

this is the java script:

---------------------------------------------------

function GetSelectedItem() {

 

len = document.f1.s1.length

i = 0

chosen = "none"

 

for (i = 0; i < len; i++) {

if (document.f1.s1.selected) {

chosen = document.f1.s1.value

}

}

return(chosen)

}

-----------------------------------------

 

the variable is of course: chosen

 

tnx

Link to comment
https://forums.phpfreaks.com/topic/57960-get-variable-from-a-java-script/
Share on other sites

doesnt that imply that i will need to use a second webpage?

 

PHP runs on the server and sends it's output to the viewers browser which THEN runs the Javascript.  Thus the Javascript can't pass things back to the PHP that created the page.

 

What you can do is to use the Javascript to call another page (or the same page again so that the PHP runs a second time).

 

Alternatively you can use AJAX techniques so that Javascript on the browser gets more data from the server and inserts it into the page without reloading it.  Note however that it is the Javascript that is doing the updating.

If you have a table structure (with numeric keys) like below, where tableA is used to populate the first drop down and tableB is used to populate the second (eg. country/region , car make/model etc) then the baaSelect class in my sig will help.

[pre]

tableA              tableB

---------            ---------

id (int)  ---+      id (int)

descrip      |      descrip

            +-----  a_id (int)

[/pre]

Or you can use the AJAX approach - see other link

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.