View Full Version : help with string in alert box from php

09-09-2011, 10:12 PM
This is all php except for the java code I am struggling with. Please advise.

I have tried this 2 ways and have spent hours on something that should be simple.

I have a string that I am trying to display in an alert box with an onclick

method 1:

$mystring = "<a href=\"mysite.com\">Click</a>";

this does not work

echo "<input type='button' value='Grab Code' onClick=alert('$mystring')>";

nor does this

echo "<input type='button' value='Grab Code' onClick=alert('".$mystring."')>";

nor does anything I try. All I want to do is display the value of mystring in an alert box.

However, this does work ..

echo "<script type='text/javascript'> alert('".$mystring."');</script>";

But I cannot call this from an onclick.

Any help is appreciated.



Old Pedant
09-09-2011, 10:31 PM
alert( "<?php echo $mystring; ?>" );


But the string you are trying to display makes no sense.

If you had

$mystring = "<a href=\"mysite.com\">Click</a>";
<script type="text/javascript">
function doThisSometime( )
alert( "<?php echo $mystring; ?>" );

it would work.

But it would simply alert the *TEXT*

<a href="mysite.com">Click</a>

in the alert box.

That would *NOT* be a clickable link. It would simply display text. Clicking on it would do NOTHING AT ALL.

09-09-2011, 10:33 PM
Old pedant,

What I am trying to do is basically have a user click on a link or button and than an alert window pops up with the html code (text) he could copy and paste on his site.

I cant seem to get your sample working.


Old Pedant
09-09-2011, 10:52 PM
You can't copy/paste from an alert().

You need a real window to popup, instead.

Or, maybe better, just have a POPON <div> appear. That way it's not blocked by popup blockers.


<div id="POP" style="display: none; position: absolute; height: 100px; width: 300px; border: solid red 1px;">
<?php echo $mystring; ?>
<input type="button" value="close me" onclick="document.getElementById('POP').style.display='none';"/>
<input type="button" value="CLICK HERE" onclick="document.getElementById('POP').style.display='block';"/>