NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Javascript and php confusion

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 05-30-2007, 01:35 AM THREAD STARTER               #1 (permalink)
Senior Member
 
champ_rock's Avatar
Join Date: Oct 2006
Location: http://akshayjain.org
Posts: 2,817
champ_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond repute
 



Javascript and php confusion


hello

i am here once again with my problems

i have a php variable containing a URL -- $url

what i want to do is to open a new window opening the URL specified in $url

here is my code to generate a new window
Code:
		echo '<SCRIPT LANGUAGE="JavaScript"><!-- Begin 
		window.open ("$url") 
					// End --></script>';
the above does not work as the browser takes $url as a string and tries to open the relative path
????: NamePros.com http://www.namepros.com/programming/333460-javascript-and-php-confusion.html

i have tried variation such as \"$url"\ but this give me some ASCII error

please guide me on where i am going wrong
champ_rock is offline  
Old 05-30-2007, 02:12 AM   #2 (permalink)
NamePros Regular
Join Date: Jul 2006
Posts: 333
klstrphnky is a glorious beacon of lightklstrphnky is a glorious beacon of lightklstrphnky is a glorious beacon of lightklstrphnky is a glorious beacon of lightklstrphnky is a glorious beacon of light
 



It is not a very elegant solution, but must work:

PHP Code:
window.open ("'.$url.'"
????: NamePros.com http://www.namepros.com/showthread.php?t=333460
klstrphnky is offline  
Old 05-30-2007, 11:43 AM   #3 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
The reason it does not work is because the variable is in single quotes. Variables in sibngle quotes are treated as strings. To have them evaluated properly use double quotes or as klstrphnky suggests use concatenation (my favourite of the 2 methods)
Peter is offline  
Old 05-30-2007, 12:21 PM   #4 (permalink)
New Member
Join Date: Mar 2007
Posts: 16
samuofm is an unknown quantity at this point
 



Since javascript can use either single or double quotes, you just put the string together like this
????: NamePros.com http://www.namepros.com/showthread.php?t=333460

PHP Code:

    
echo "<script language='JavaScript'> window.open ('" $url "'); </script>"

effectively you are concatenating 3 strings and this would be equivalent (easier to understand maybe)

PHP Code:
$i "<script language='JavaScript'> window.open ('";
$i .= $url;
$i .= "'); </script>"

echo 
i$; 
Last edited by samuofm; 05-30-2007 at 01:22 PM.
samuofm is offline  
Old 05-30-2007, 01:01 PM   #5 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,792
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
If you're going to use double quotes, you can do
PHP Code:
 echo "<script language='JavaScript'> window.open ('$url') </script>"
????: NamePros.com http://www.namepros.com/showthread.php?t=333460
Dan is offline  
Old 05-30-2007, 01:23 PM   #6 (permalink)
New Member
Join Date: Mar 2007
Posts: 16
samuofm is an unknown quantity at this point
 



also i recommend you add a semi colon in your javascript call (see my example), its optional with only one statement like you have here but its a good practice for when you change the code later.
samuofm is offline  
Old 05-30-2007, 05:09 PM   #7 (permalink)
cef
NamePros Regular
Join Date: May 2004
Location: NYC
Posts: 236
cef is a jewel in the roughcef is a jewel in the roughcef is a jewel in the rough
 


Animal Rescue
Originally Posted by samuofm
also i recommend you add a semi colon in your javascript call (see my example), its optional with only one statement like you have here but its a good practice for when you change the code later.
Actually, you can leave off semicolons altogether if each statement is on its own line, but I've never seen anyone code this way. At least not intentionally.
cef is offline  
Old 05-30-2007, 08:25 PM THREAD STARTER               #8 (permalink)
Senior Member
 
champ_rock's Avatar
Join Date: Oct 2006
Location: http://akshayjain.org
Posts: 2,817
champ_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond reputechamp_rock has a reputation beyond repute
 



thanks a lot guys

reps for all of u
champ_rock is offline  
Old 05-31-2007, 01:41 AM   #9 (permalink)
NamePros Regular
Join Date: Oct 2006
Posts: 972
neroux has a spectacular aura aboutneroux has a spectacular aura about
 



The best way would be still to actually embed PHP into HTML.
HTML Code:
<script language='JavaScript'>window.open('<?php echo $url; ?>');</script>
__________________
Paris loves CityPics

-- Do not let others be treated this way!
neroux is offline  
Old 05-31-2007, 01:54 PM   #10 (permalink)
New Member
Join Date: Mar 2007
Posts: 16
samuofm is an unknown quantity at this point
 



Originally Posted by neroux
The best way would be still to actually embed PHP into HTML.
HTML Code:
<script language='JavaScript'>window.open('<?php echo $url; ?>');</script>
You can argue either way depending on how his app is laid out. Embedded php is ok for presentation logic, but you really want to stay away from it for business logic, and its its possible that echo would only be used after a certain business logic method was called. It also becomes harder to read embedded php when you have multiple levels of nested ifs.

For example, here is some embedded php I wrote that makes use of nested ifs and it begins to become difficult to read.


PHP Code:

????: NamePros.com http://www.namepros.com/showthread.php?t=333460
<?php if ($qeiObj->formData['fireplaceMantel'] == 'on') { ?>

  <div>Type of mantle: <?php echo $qeiObj->formData['mantelType']; ?> </div>


  <div>Spindles to paint on banister: <?php
    
if ($qeiObj->formData['numSpindles'] != 20) {
????: NamePros.com http://www.namepros.com/showthread.php?t=333460
      echo 
$qeiObj->formData['numSpindles'];
    }
    else {
      echo 
"20+";
    }
  
?></div>

<?php ?>

that's fairly hard to read with just two levels, and if I replaced those inner echoes with embedded php it would be even more challenging.
samuofm is offline  
Closed Thread


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


 
All times are GMT -7. The time now is 02:20 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger