View Full Version : Resolved can i mix php and javascript on a onclick

10-30-2012, 02:33 AM
Hi can i mix php and javascript on an onclick function call like this

<script type="text/javascript">

function show_big()

foreach($fullpic_array as $key => $imagelink)
/* remove the single quotes from each value now that you have the comma seperation */
$fullpic_array[$k] = str_replace("'","",$fullpic_array[$k]);
$medpic_array[$k] = str_replace("'","",$medpic_array[$k]);

$imagenow = "<a href='".$fullpic_array[$k]."' rel='lightbox[myprofile]'><img src='".$CONST_LINK_ROOT.'/'.$medpic_array[$k]."' border='0' alt='' /></a>";

echo $imagenow;

}//close foreach

}//close function show_big


and then i call it like this

Total pics = <a href="#" onclick="show_big();"><?=$no_of_pics;?></a>

its not working which is why im asking lol

10-30-2012, 04:02 AM
Yep. PHP is preprocessed though, so you'll end up with whatever is in $imagenow on the last iteration. You can't interact with it in any way of course since PHP is not capable of responding to client events.
So yep, you can create JS code with PHP. Nope you cannot interact with PHP using JS.

10-30-2012, 04:32 AM
Thanks Fou-Lu, i changed the foreach to a while

while($k < count($fullpic_array))

But i will have to look at this in the morning again, im getting very tired, its still not even trying to open the images in the lightbox and i did test the $imagenow seperately and it is correct it does work if i take it out of the function and just manually assign it to [0] rather than $k,

but ill look at it again tomorrow, i may end up having to open a window and display them on a sep window first.

Thanks so much... im headed to bed :)

10-30-2012, 06:48 PM

i got it, that was the last item on the list for this deal and thanks for all your help.

The fix was so obvious and right there in front of me but i was so tired and stressed over this that i just didnt see it. Remember that i said i may have to open another window for the lightbox. In the back of my mind i kept thinking there has to be a way im not the first one to have this issue.

I just kept thinking two things, they must be on a page first, and you have to click them, before it will work. And then it hit me like a ton of bricks man, i about fell out of my chair, and i remember i had seen it before on other sites but just didnt pay attention.

I just listed the images 30x30 in a row under the main image side by side, they are large enough to make out the image somewhat and small enough to put 8 of them under there before making another row.

Now i have them listed on the page using that array we made and now i can use lightbox, you click on one and then you can click next and prev images. I honestly have seen this setup so many times but it just never hit me till now after much needed sleep and after stressing all weekend over this.

Also doing it this way i did not have to use it wrapped in a function so much better.

Thanks sooooooooooooooo much for your help, really appreciate it.. :)