Independent Articles and Advice
Login | Register
Finance | Life | Recreation | Technology | Travel | Shopping | Odds & Ends
Top Writers | Write For Us


PRINT |  FULL TEXT PAGES:  1
Embedding Special Characters in JavaScript with PHP 
 
by Chris Stone June 25, 2005

A quick guide to embedding newlines, quotes and tick marks inside a JavaScript statement using PHP.

About

This is a demonstration of how to embed new lines and tick marks inside a javascript function using PHP. Dealing with quotes and escaped characters can be very confusing when building dynamic scripts. The example below gives a web programmer an idea of how it should be done in order for it to work properly.

Example

This is an example PHP script which displays a JavaScript confirm() dialog box that has quotes and apostrophies inside it.

<?php

echo <<<EOT

<form method=POST><input type=submit name=payment value='Accept Offer' onClick="return confirm(&quot;This text has newlines.\\nIt and 'apostrophies'.\\n\\nPress 'OK' to accept the offer.\\nPress 'Cancel' to go back without submitting any changes.\\n&quot;);" /></form>

EOT;

?>

Conclusion

I hope the example helped you out.

Credits

Special thanks to brainy on the #javascript IRC channel for helping me figure this out. We can now hide javascript inside a link instead of making a function.

PREV PAGE 1 NEXT PAGE

 




Home  |  Write For Us  |  FAQ  |  Copyright Policy  |  Disclaimer  |  Link to Us  |  About  |  Contact

© 2005 GoogoBits.com. All Rights Reserved.