Thread: Javascript in php echo()
Results 1 to 7 of 7
Related
-
add code for font size under php echo code Forum: PHP Forum
Replies: 0 -
javascript in echo php Forum: PHP Forum
Replies: 2 -
echo Javascript Forum: PHP Forum
Replies: 1 -
Javascript event inside echo statement Forum: PHP Forum
Replies: 2 -
Is it possible to stop a javascript function w/ other javascript code? Forum: Javascript Forum
Replies: 5
-
06-08-2007, 03:25 AM #1
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>
Any help would be most appreciated.
Thank you.Last edited by vinyl-junkie; 06-08-2007 at 06:12 AM. Reason: Added code tags
-
06-08-2007, 04:00 AM #2
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!!
-
06-08-2007, 04:55 AM #3
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
-
06-08-2007, 07:34 AM #4
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
-
06-08-2007, 07:56 AM #5
Re: Javascript in php echo()
Last edited by DeadMeatGF; 06-08-2007 at 07:57 AM. Reason: changed code to php tags :)
-
06-08-2007, 09:02 AM #6
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","","")">';
-
06-08-2007, 11:50 AM #7
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> ";
Cloudjiffy- PaaS for Developers
10-05-2020, 12:30 AM in Web Hosting Forum