58 users online (0 members and 58 guests)  


  Results 1 to 7 of 7

Related

  1. Replies: 0
  2. javascript in echo php    Forum: PHP Forum
    Replies: 2
  3. echo Javascript    Forum: PHP Forum
    Replies: 1
  4. Javascript event inside echo statement    Forum: PHP Forum
    Replies: 2
  5. Replies: 5
  1. #1
    chilu's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2007
    Posts
    5

    Question Javascript in php echo()

    Hello everybody,

    I am pretty sure that this has a simple answer of which everyone else is aware.

    I am trying to display text links that have their information (link name and URL) retrieved from a .txt file using PHP on a page called "links.php". The problem that I am encountering is that I would like to have the links open a new window when clicked and the "onClick" function cause the "links.php" page to appear blank. If I remove the "onClick()"function from the PHP script, I can get working links to show up on the in the right location.

    Here is the PHP code that I am trying to use:

    Code:
    <?php
    $fp = fopen('text/linksIII.txt','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
    
    while (!feof($fp)) {
    	$line = fgets($fp,1024); //use 2048 if very long lines
    	list ($section, $url, $name,) = split ('\|', $line);
    	echo '
    <tr>
    <td class="leftCol">'.$section.'</td>
    <td class="spacerCol"></td>
    <td class="rightCol"><a href="javascript:;" onClick="MM_openBrWindow('.$url.','','')">
    '.$name.'</a></td>
    </tr>
    I understand that PHP is processed by the server and that Javascript is processed by the client, but I don't see how this applies as the PHP is creating javascript instruction to be carried out by the client.

    Any help would be most appreciated.

    Thank you.
    Last edited by vinyl-junkie; 06-08-2007 at 06:12 AM. Reason: Added code tags

  2. #2
    firmaterra's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    81

    Re: Javascript in php echo()

    i'm a bit fuzzy on this but i think the problem your having is beacause your closing off your echo quotes by the use of single quotes in your javascript statement.try escaping them with a back slash/replacing them with double quotes.
    also try a href=javascript:void(0);
    if all else fails try assigning your code to a variable and echoing the variable instead.
    hope this helps. let me know!!

  3. #3
    chilu's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2007
    Posts
    5

    Re: Javascript in php echo()

    Hi firmaterra,

    Thanks for the quick reply.

    I tried all your suggestions in all their various combinations (as correctly as I could). It definitely go me closer, but still no biscuit... with the following script, I get to see the page with all the links again. Everything but the link seems to work. I even see the "Run Javascript:void(0)" message in the browser's status bar, but no new window opens.

    As you predicted the problem seems to stem from the handling of the single quotes, but using / to escape them didn't seem to do the trick and the double quotes don't seem to be doing it either...

    Code:
    <?php
    $fp = fopen('text/linksIII.txt','r');
    if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
    
    while (!feof($fp)) {
    	$line = fgets($fp,1024); //use 2048 if very long lines
    	list ($section, $url, $name,) = split ('\|', $line);
    	$link = '<a href="javascript:void(0);" onClick="MM_openBrWindow("'.$url.'","","")">';
     
    	echo '
    <tr>
    <td class="leftCol">'.$section.'</td>
    <td class="spacerCol"></td>
    <td class="rightCol">'.$link.''.$name.'</a></td>
    </tr>
    ';
    	$fp++;
    }
    fclose($fp); ?>
    Last edited by vinyl-junkie; 06-08-2007 at 07:29 AM. Reason: Added code tags

  4. #4
    vinyl-junkie's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    721

    Re: Javascript in php echo()

    Please use code tags when posting code. Thanks.

    What are you doing in your javascript? Unless you're doing anything besides just opening a new window, why not just use the url directly target="_blank" to do that?
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more

  5. #5
    DeadMeatGF's Avatar
    Moderator

    Status
    Offline
    Join Date
    Sep 2005
    Posts
    381

    Re: Javascript in php echo()

    Quote Originally Posted by chilu View Post
    but using / to escape them didn't seem to do the trick and the double quotes don't seem to be doing it either...
    You need to use \ to escape - not /

    Personally I'd use - for example:
    PHP Code:
    echo "
    <tr>
    <td class=\"leftCol\">
    $section</td>
    <td class=\"spacerCol\"></td>
    <td class=\"rightCol\">
    $link$name</a></td>
    </tr>
    "

    Last edited by DeadMeatGF; 06-08-2007 at 07:57 AM. Reason: changed code to php tags :)

  6. #6
    firmaterra's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Apr 2007
    Posts
    81

    Re: Javascript in php echo()

    ^^ What Dead Meat Said!!

    Also I'd try change the javascript to:
    Code:
    $link = '<a href="javascript:void(0);" onClick="MM_openBrWindow("$url","","")">';
    i.e. leaving out the single quotes.

  7. #7
    chilu's Avatar
    New User

    Status
    Offline
    Join Date
    Jun 2007
    Posts
    5

    Re: Javascript in php echo()

    Thank you all for your help, DeadMeatGF, vinyl-junkie and firmaterra. A mash up of your suggestions has worked. vj - your simple solution was the ticket. I ended up using the following:

    Code:
    	$link = '<a href="'.$url.'" target="_blank">'; 
    	echo " 
    <tr> 
    <td class=\"leftCol\">$section</td> 
    <td class=\"spacerCol\"></td> 
    <td class=\"rightCol\">$link$name</a></td> 
    </tr> 
    ";
    Thanks again!



Tags for this Thread