469,636 Members | 1,929 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

Outputting variables in javascript confusion


I'm confused -

The following outputs a URL encoded string into an alert box
----------------------CUT ----------------------
function createEmbarrasment() {
$tmp = urlencode("Your fly is undone!");
?<script type="text/javascript"><!--
alert('<?php echo $tmp; ?>')
//--</script>
<?php
}
-------------------------------
Unfortunately displaying the URL encoded version of the string

If you remover the URL encoding the alert box doesn't even display

Next

Using constants - Again this fails to even open the alert box
----------------------CUT ----------------------

function createEmbarrasment() {
define("TEMP","Your fly is undone!");
?<script type="text/javascript"><!--
alert('<?php echo TEMP; ?>')
//--</script>
<?php
}
-------------------------------

AND YET

----------------------CUT ----------------------
function createEmbarrasment() {
?<script type="text/javascript"><!--
alert('<?php echo PHP_VERSION; ?>')
//--</script>
<?php
}
-------------------------------
Opens the box and displays fine without URL encoding

MY PROBLEM - I'm trying to do the first example without the display
showing the URL encoding.
Can anyone explain or help please?

-andy-

Mar 9 '07 #1
2 1289
Rik
<an****@blueyonder.comwrote:
The following outputs a URL encoded string into an alert box
URL encoding is for, well URL's, not for text, prompt or alerts. Drop
that, unless you want to alert(?) how urlencoding looks like.
If you remover the URL encoding the alert box doesn't even display
Does here, as long as $tmp is set.
We're not in a javascript group here, and all your issues are related to
javascript itself. Wether this is dynamically build or hardcoded does not
matter to the browser (heck, it doesn't even know it).

Just look at the _source_ of you generated HTML-document, check what's
wrong with the javacript, and adjust the output oh PHP accordingly.
--
Rik Wasmus
Posted on Usenet, not any forum you might see this in.
Ask Smart Questions: http://tinyurl.com/anel
Mar 9 '07 #2
On Fri, 09 Mar 2007 17:50:48 GMT, in comp.lang.php
an****@blueyonder.com
<MP**********************@news-text.blueyonder.co.ukwrote:
>|
| I'm confused -
|
| The following outputs a URL encoded string into an alert box
| ----------------------CUT ----------------------
| function createEmbarrasment() {
| $tmp = urlencode("Your fly is undone!");
| ?<script type="text/javascript"><!--
| alert('<?php echo $tmp; ?>')
| //--</script>
| <?php
| }
| -------------------------------
| Unfortunately displaying the URL encoded version of the string
|
Javascript alert boxes are plain text. So if you use
alert("This is line 1<br />This is line 2");
then This is line 1<br />This is line 2 will be displayed

To show the above correctly you need
This is line 1\nThis is line 2

Change your $tmp variable to
$tmp = "Your fly is undone!";
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Mar 10 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Oleg Konovalov | last post: by
10 posts views Thread by jason_box | last post: by
4 posts views Thread by Lando | last post: by
9 posts views Thread by skijor | last post: by
34 posts views Thread by dhtml | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.