Unstoppable Domains

Javascript and php confusion

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
91
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

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

please guide me on where i am going wrong
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
It is not a very elegant solution, but must work:

PHP:
window.open ("'.$url.'")
 
1
•••
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)
 
1
•••
Since javascript can use either single or double quotes, you just put the string together like this

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


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

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

echo i$;
 
Last edited:
0
•••
If you're going to use double quotes, you can do
PHP:
 echo "<script language='JavaScript'> window.open ('$url') </script>";
 
0
•••
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.
 
1
•••
samuofm said:
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.
 
0
•••
thanks a lot guys

reps for all of u :)
 
0
•••
The best way would be still to actually embed PHP into HTML.
HTML:
<script language='JavaScript'>window.open('<?php echo $url; ?>');</script>
 
0
•••
neroux said:
The best way would be still to actually embed PHP into HTML.
HTML:
<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:
<?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) {
      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.
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back