HTML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignHTML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 19th, 2003, 04:21 PM
jahu jahu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 110 jahu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 17 sec
Reputation Power: 7
scope of variable inside js function

I was wondering how I could get a php variable to recognized inside a javascript function.

PHP Var:
$address = "Street Address";

<script language=javascript>
<!--
function addbookmark(){
bookmark="Listing - "+<?=$address?>+" - CompanyName";
}
//-->
</script>

I am tring to concatinate the javascript var bookmark to equil:
"Listing - Street Address - CompanyName"

The php var $address does not seem to be read inside javascript. Does anybody know how to get the scope of a php variable to work inside this javascript function. Thanks in advance for any help...

Reply With Quote
  #2  
Old February 19th, 2003, 07:40 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 9
This is really a basic php question, and has nothing to do with javascript. You're php expressoin evaluates the address variable, but does not print it out to the resulting html. Look at the resulting source of the function, and you'll see something like this:
Code:
bookmark="Listing - "++" - Company Name";

This obviously won't work. You need to echo/print the value of your php variable into the script.
Code:
bookmark = "Listing - " + <? echo $address; ?> + " - Company Name";

Reply With Quote
  #3  
Old February 19th, 2003, 11:51 PM
kicken's Avatar
kicken kicken is offline
Wiser? Not exactly.
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: May 2001
Location: Ft Myers, FL
Posts: 4,388 kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)kicken User rank is General (90000 - 100000 Reputation Level)  Folding Points: 153387 Folding Title: Super Ultimate Folder - Level 1Folding Points: 153387 Folding Title: Super Ultimate Folder - Level 1Folding Points: 153387 Folding Title: Super Ultimate Folder - Level 1Folding Points: 153387 Folding Title: Super Ultimate Folder - Level 1Folding Points: 153387 Folding Title: Super Ultimate Folder - Level 1Folding Points: 153387 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Month 3 Weeks 3 Days 10 h 30 m 3 sec
Reputation Power: 959
Send a message via ICQ to kicken Send a message via AIM to kicken Send a message via MSN to kicken
<?=$var?> and <?php echo $var; ?> and <? echo $var; ?> are the same thing. <?= is basically a show way to say 'print this'.

The thing is, php will get parsed before the JS, you need to NOT escape the string in javascript, just put the PHP into it. The way it is now, you'd end up with something like this:

Code:
bookmark="Listing - "+1234 fake street.  Unknown, NW, 55555+" - Company Name";


or what ever happens to be in the $address variable. That is going to produce a JS error obviosuly. What you need to do is just throw the PHP in to the string directly.

Code:
bookmark="Listing - <?=$address?> - Company name";

Reply With Quote
  #4  
Old February 20th, 2003, 12:07 AM
jahu jahu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 110 jahu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 16 m 17 sec
Reputation Power: 7
Of course, server side and client side. I don't know how I got hung up on that one. Works fine now and makes perfect sence, thanks you for the help.... :-)

Reply With Quote
  #5  
Old February 20th, 2003, 12:47 AM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 9
Crap, I didn't catch the "=" in the php. And my solution wouldn't have worked anyway, since I terminated the strings for some gawd awful reason.

My mind is slippin now...

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignHTML Programming > scope of variable inside js function


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT