|
|
|
| Здравствуйте.
Помогите, пожалуйста, решить вопрос, связанный с передачей переменной из php в javascript.
Задача с следующем.
Нужно сделать так, что бы при клике по превьюшке, открывалось окно заданного размера, и выводилось изображение в полный размер.Делаю это для фотогалереи.
создаю окно :
<script type="text/javascript">
function myPhoto()
{
var x = "<?php echo "<p><img src='files/".$view_photo['big']."'></p>";?>";
newWindow = window.open("","newWindow","width=800,height=550");
newWindow.document.open();
newWindow.document.write("<html>");
newWindow.document.write("<head>");
newWindow.document.write("<title>Big Size</title>");
newWindow.document.write("</head><body>");
newWindow.document.write(""+x+"");
newWindow.document.write("<p><button onClick='javascript:window.close()'></button></p>");
newWindow.document.write("</body></html>");
newWindow.document.close();
}
</script>
|
превьюшки вывожу в цикле php
<?php
..........
$image = "<a onClick = myPhoto() href='#'><img src='files/".$view_photo['small']."' ></a>"
.........
?>
|
Если всё сделать так, то при клике на любой превьюшке выводится оригинал одной и той же фотографии. Как я понимаю, надо передать id нужного изображения, но как это сделать не знаю.
Подскажите, пожалуйста, как сделать правильно.
Заранее благодарю. | |
|
|
|
|
|
|
|
для: Slo_Nik
(12.11.2008 в 04:53)
| | надо передавать имя файла как параметр функции, а не присваивать его заново х при
каждом вызове функции myPhoto
<script type="text/javascript">
function myPhoto(imya_fayla)
{
// var x = "<?php echo "<p><img src='files/".$view_photo['big']."'></p>";?>";
newWindow = window.open("","newWindow","width=800,height=550");
newWindow.document.open();
newWindow.document.write("<html>");
newWindow.document.write("<head>");
newWindow.document.write("<title>Big Size</title>");
newWindow.document.write("</head><body>");
newWindow.document.write(""+imya_fayla+"");
newWindow.document.write("<p><button onClick='javascript:window.close()'></button></p>");
newWindow.document.write("</body></html>");
newWindow.document.close();
}
</script>
|
<?php
..........
$image = "<a onClick = myPhoto('files/".$view_photo['small']."') href='#'><img src='files/".$view_photo['small']."' ></a>"
.........
?>
|
| |
|
|
|
|
|
|
|
для: elenaki
(12.11.2008 в 10:47)
| | Благодарю за подсказку, всё заработало.
Вот рабочий код, поправьте, если можно сделать по другому.
<?php /* формируем переменную $image, отвечающую за вывод уменьшенной копии изображения, которая является ссылкой на большую фотографию */
if(!empty($view_photo['small']) && $view_photo['small'] != "-" && file_exists("files/".$view_photo['small'])){
/* объявляем переменную которую передаём javascript */
$imja = $view_photo['big'];
$image = "<a onClick = myPhoto('files/".$imja."') href='#'><img src='files/".$view_photo['small']."' ></a>"; ?>
|
Функция javascript
<script type="text/javascript">
function myPhoto($imja)
{ newWindow = window.open("","newWindow","width=800,height=550");
newWindow.document.open();
newWindow.document.write("<html>");
newWindow.document.write("<head>");
newWindow.document.write("<title>Big Size</title>");
newWindow.document.write("</head><body>");
newWindow.document.write("<img src='"+$imja+"'>");
newWindow.document.write("<p><button onClick='javascript:window.close()'></button></p>");
newWindow.document.write("</body></html>");
newWindow.document.close();
}
</script>
|
| |
|
|
|
|
|
|
|
для: Slo_Nik
(12.11.2008 в 15:06)
| |
<script type="text/javascript">
function myPhoto($imja)
{ newWindow = window.open("","newWindow","width=800,height=550");
newWindow.document.open();
newWindow.document.write("<html>");
newWindow.document.write("<head>");
newWindow.document.write("<title>Big Size</title>");
newWindow.document.write("</head><body>");
newWindow.document.write("<img src='"+$imja+"'>");
newWindow.document.write("<p><button onClick='javascript:window.close()'></button></p>");
newWindow.document.write("</body></html>");
newWindow.document.close();
}
</script>
|
здесь должны быть переменные JS, а не PHP. это объявление функции. а вот при вызове
этой функции вы даете ей каждый раз новые значения. | |
|
|
|
|
|
|
|
для: elenaki
(12.11.2008 в 18:13)
| | Вы говорили, что надо передать имя файла как параметр функции, вот я и присвоил $imja имя файла.
$view_photo['big'] это и есть имя файла. | |
|
|
|
|
|
|
|
для: Slo_Nik
(13.11.2008 в 11:44)
| | function myPhoto($imja)
вот здесь, в java-script, не должно быть переменной PHP, должно быть имя переменной java-script, т.е.
function myPhoto(imja)
....
и потом эту же переменную (параметр функции) подставлять в код
newWindow.document.write("<img src='"+imja+"'>"); | |
|
|
|
|
|
|
|
для: elenaki
(13.11.2008 в 11:48)
| | Благодарю за подсказки, но ... как ни крути, работает и так и так :) | |
|
|
|