Passing variable between PHP and Javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
desmondlk
Forum Commoner
Posts: 27
Joined: Tue Sep 24, 2002 10:27 pm
Location: Malaysia

Passing variable between PHP and Javascript

Post by desmondlk »

May i know how to pass the variable from Javascript to php...

How the variable scope from javascript being assign to php variable scope?

For e.g. :

Var a; //in Javascript

$b = a;

<?
print($b);
?>

is it work?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

You have to pass it by GET

e.g.

Code: Select all

var a = "Hello";
location.href="index.php?value="+a;
Then you will be able to get the variable a:-

Code: Select all

&lt;?php
echo $_GET&#1111;'value'];
?&gt;
desmondlk
Forum Commoner
Posts: 27
Joined: Tue Sep 24, 2002 10:27 pm
Location: Malaysia

Passing variable between PHP and Javascript

Post by desmondlk »

not post to another new page...

passing the variable within the same page...

how the variable being passed?

I have two drop-down boxes within the same page, one after another.

The main problem here is i would like to create a new drop-down list in drop box second drop box after the onChange method is fired (in JavaScript) on the first drop box.

For example:

First box is Country and the other is state of the selected country.

if the first box is UK then the second drop box should display the state of the related country.

Both of these drop-down lists are getting from MySQL database. Thus, how can it being done?

After the onChange method, the variable is passed to Javascript and how can the variable being passed back to PHP? this is because i would like to display the drop-down list of the second drop box by using PHP-MySQL connection rather than JDBC in Javascript...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

desmondlk
Forum Commoner
Posts: 27
Joined: Tue Sep 24, 2002 10:27 pm
Location: Malaysia

Post by desmondlk »

Before this i know the limitation of PHP, as a server-side scripting language. So, if like that, hard-coded the drop-dowm list for both drop boxes is the only way. Is that right?

If there any idea to link Javascript with MySQL? Which one will be better?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

You can't use Javascript to get stuff from MySQL db
CodeEye
Forum Commoner
Posts: 25
Joined: Fri Jul 05, 2002 7:19 am

Post by CodeEye »

it is possible to send a message back from the browser if it is dom compliant check out
http://www.dotvoid.com/article_condensed.php?itemID=9
Post Reply