Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help with string in alert box from php

    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.

    Thanks

    JT

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    Code:
    alert( "<?php echo $mystring; ?>" );
    BUT...

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

    If you had
    Code:
    $mystring = "<a href=\"mysite.com\">Click</a>";
    ...
    <script type="text/javascript">
    function doThisSometime( )
    {
        alert( "<?php echo $mystring; ?>" );
    }
    </script>
    it would work.

    But it would simply alert the *TEXT*
    Code:
        <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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    JT
    Last edited by jtorral; 09-09-2011 at 09:46 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,200
    Thanks
    75
    Thanked 4,342 Times in 4,308 Posts
    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.

    So:
    Code:
    ...
    <body>
    <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';"/>
    </div>
    ...
    <input type="button" value="CLICK HERE" onclick="document.getElementById('POP').style.display='block';"/>
    ...
    </body>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •