Testadmin
Testadmin

Reputation: 2898

Change a value in a PHP variable using JavaScript

I have a value in a php variable $thumb_path="images/Gallery1/thumbs/";. I need to change this value to $thumb_path="images/Gallery2/thumbs/"; when I am clicking on Gallery2 Link n my project. Is it possible to change a value in a PHP variable using JavaScript?

Or is there any other way to do this?

Upvotes: 0

Views: 4213

Answers (2)

ayk
ayk

Reputation: 1340

JS is a client side language, PHP is parsed on server, so you can't change the php file itself with js... BUT: :)

You can manage it with GET:

$thumb_path="images/Gallery".(($_GET['gallery'] && preg_match('/^[0-9]+$/', $_GET['gallery'])) ? $_GET['gallery'] : "1")."/thumbs/";

now you can call your link like this:

http://www.page.com/yourphpfile.php?gallery=2

This will open gallery 2.

If you dont set ?gallery gallery 1 will shown as default.

Upvotes: 2

fabrik
fabrik

Reputation: 14365

Since JavaScript is a client-, and PHP is a server-side language, obviously you can't.

You can use some AJAX if possible. If this is some kind of dynamic variable (you'll getting this data from a form), you can change it with JavaScript.

Upvotes: 0

Related Questions