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?
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.
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.
精彩评论