Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Var. into Javascript function


PHP Var. into Javascript function

Reply
  #1 (permalink)  
Old 04-04-05, 11:18 AM
Baho's Avatar
Baho Baho is offline
Newbie Coder
 
Join Date: Jan 2005
Location: Lebanon
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Angry PHP Var. into Javascript function

i wanna change the picture in my page, using another one form a database
so i tried 2 pass the variable $row[11] (which is the path of the pic)
into a javascript function but it didnt work.

<? echo "<script language=javascript>";
echo "function pic(p) {";
echo "document.images [\"picture\"].src = p";
echo "}";
echo "</script>";?>


<? echo "<a href=# onclick=\"pic(".$row[11].");\">";?>Picture</a>

<img src="<? echo $row[10];?>" height="100%" width="100%" border="0" name="picture">
Reply With Quote
  #2 (permalink)  
Old 04-04-05, 11:33 AM
kjmatthews kjmatthews is offline
Wannabe Coder
 
Join Date: Jun 2004
Location: Boston, MA
Posts: 134
Thanks: 0
Thanked 0 Times in 0 Posts
That is some ugly lookin' code. I am by no means a Javascript man, but try this. Write out the Javascript function (complete with PHP variables) and resultant HTML as though it were not being echoed by PHP, complete with line breaks, indentation, and without escaping double quotes, etc. I think it would look something like this:
Code:
<script language="javascript">
 function pic(p) {
   document.images[picture].src = p
 }
</script>

<a href="#" onclick="pic($row[11])">Picture</a>

<img src="$row[10]" height="100%" width="100%" border="0" name="picture">
Then we can use the extremely useful HEREDOC syntax in PHP to echo this all as typed and with variable substitution without having to escape all those pesky quotes and whatnot, viz:
PHP Code:

<?php

echo<<<END
<script language="javascript">
 function pic(p) {
   document.images [picture].src = p
 }
</script>

<a href="#" onclick="pic(
$row[11])">Picture</a>

<img src="
$row[10]" height="100%" width="100%" border="0" name="picture">
END;
If it still doesn't work, you know it is the Javascript function that is the problem and not the PHP.
Reply With Quote
  #3 (permalink)  
Old 04-04-05, 04:43 PM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<? echo '<a href="#" onclick="pic('.$row[11].');">Picture</a>'?>

__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
link to iframe//help moose JavaScript 1 01-29-05 12:26 PM
Scrollbar!!!!!! LiLSweetie HTML/XHTML/XML 8 07-22-04 05:01 PM
PHP Error Fairnie PHP 8 06-26-04 07:15 AM
Help with dynamic select in dynamic table noviceforever JavaScript 1 06-23-04 01:20 PM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM


All times are GMT -5. The time now is 05:40 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.