How would I echo out this thing?

Discussion in 'PHP' started by Imozeb, Jun 21, 2010.

  1. #1
    This is my code:

    PHP code:
    $a = '<a href="test.php">test</a>
    
    $data=<<<data
    							<a id="ciButton" onmouseup="bMouseUp('$a')" onmouseover="bMouseOver()" onmouseout="bMouseOut()" style="color:#CC0000;">Click Me.</a>
    data;
    						echo($data);
    Code (markup):
    It echos something like this:

    <a href="test.php">test</a>a')" onmouseover="bMouseOver()" onmouseout="bMouseOut()" style="color:#CC0000;">Click Me.</a>
    //this code is printed to the page
    Code (markup):
    I need it to not echo out the html code. I need to to parse the html and print that. This is for a button which will show the $a html if pressed not the $a html code.

    Please help! I've been going an hour at this to no avail. :(

    Thanks.
     
    Imozeb, Jun 21, 2010 IP
  2. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $a = '<a href="test.php">test</a>';
    $data = "<a id=\"ciButton\" onmouseup=\"bMouseUp('{$a}');\" onmouseover=\"bMouseOver();\" onmouseout=\"bMouseOut();\" style=\"color:#CC0000;\">Click Me.</a>";
    
    echo $data;
    
    PHP:
     
    krsix, Jun 21, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Did you test it because it isn't working for me?

    Prints out:
    test');" onmouseover="bMouseOver();" onmouseout="bMouseOut();" style="color:#CC0000;">Click Me.
    Code (markup):
    I think that the glitch has something to do with the " marks because when I remove them it works just fine. Anyone know how I can fix this?

    Thanks.
     
    Imozeb, Jun 21, 2010 IP
  4. ttyler333

    ttyler333 Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    $a ="<a href=\"test.php\">test</a>";
    
    $data="<a id=\"ciButton\" onmouseup=\"bMouseUp('{$a}')\" onmouseover=\"bMouseOver()\" onmouseout=\"bMouseOut()\" style=\"color:#CC0000;\">Click Me.</a>";
    						echo $data;
    PHP:
    or you could try

    
    
    $data="<a id=\"ciButton\" onmouseup=\"bMouseUp('".$a."')\" onmouseover=\"bMouseOver()\" onmouseout=\"bMouseOut()\" style=\"color:#CC0000;\">Click Me.</a>";
    
    PHP:
    That should work though it looks alot like krsix's code. Anywho i do not see any errors with it.
     
    Last edited: Jun 21, 2010
    ttyler333, Jun 21, 2010 IP
  5. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    oh right, you have to escape what's in the $a for js
    it does echo out, javascript just didn't parse it.

    do what tyler gave
     
    krsix, Jun 21, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    :confused: I tried both your codes. It's still not working for me? What do I do?
     
    Imozeb, Jun 21, 2010 IP
  7. ttyler333

    ttyler333 Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #7
    i noticed your example code said <<<DATA ... are you using a template for this code?
     
    ttyler333, Jun 21, 2010 IP
  8. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Is it a php or a js issue?

    Try

    
    $a = '<a href="test.php">test</a>';
    $data = "<script type=\"text/javascript\">var a = '{$a}';</script><a id=\"ciButton\" onmouseup=\"bMouseUp(a);\" onmouseover=\"bMouseOver();\" onmouseout=\"bMouseOut();\" style=\"color:#CC0000;\">Click Me.</a>";
    
    
    PHP:
     
    krsix, Jun 21, 2010 IP
  9. ttyler333

    ttyler333 Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #9
    woah gonna get technical on me :p. So i noticed your signature, how did you manage to get 300k ip's. Also i do think imozeb is working with a template thus the code is not working correctly. Think of the way glype's template works, its kind of the same.
     
    ttyler333, Jun 21, 2010 IP
  10. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It is a php issue. I am not working with a template. I used <<< just as an easy way of storing alot of my html code in a php variable.

    What I'm trying to do is create a <a> tag which when pressed triggers a javascript code to show some text inside the <a> tag. It needs to be echo'd out from PHP because I am using PHP to validate some variables with a database before echoing out the proper values. I have all the javascript and extra php code complete. Now I am just trying to get PHP to echo out the proper data. But php won't echo it out properly. It seems that when PHP encounters a double quote it messes up the javascript in the <a> tag. When I test the codes you gave me I pasted it into its own file (without any other code) for testing to see if it just printed out the text within the <a> tag but it didn't. Could someone test the code and see if they can get it to work because I still can't figure it out?

    $a ="<a href=\"test.php\">test</a>";$data="<a id=\"ciButton\" onmouseup=\"bMouseUp('{$a}')\" onmouseover=\"bMouseOver()\" onmouseout=\"bMouseOut()\" style=\"color:#CC0000;\">Click Me.</a>";                        echo $data;
    PHP:
    Thank you.

    P.S. from my hour or so of troubleshooting I found that the quotes in the $a variable were the problem though I can't seem to get it to work even with escaping them?
     
    Imozeb, Jun 21, 2010 IP
  11. ttyler333

    ttyler333 Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #11
    ttyler333, Jun 21, 2010 IP
  12. strgraphics

    strgraphics Active Member

    Messages:
    710
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #12
    Try this.., and tell me.

    
    $a ="<a href=\"test.php\">test</a>";
    
    $data="<a id=\"ciButton\" onmouseup=\"bMouseUp('{"$a"}')\" onmouseover=\"bMouseOver()\" onmouseout=\"bMouseOut()\" style=\"color:#CC0000;\">Click Me.</a>";
                            echo $data;
    
    PHP:
     
    strgraphics, Jun 21, 2010 IP
  13. bvraghav

    bvraghav Member

    Messages:
    123
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #13

    Seems you have tried all sorts...

    When passing the data from php to js, I would not use your method. Instead, I would output all relevant data either in xml form (ie using DOM) or transferring values directly to js as object.

    I assume the information you want to pass can crudely be broken down to
    tag=a&link=http://www.link-to-my-url.com/&linktext=my link text
    Code (markup):
    so while i output to DOM, the code looks like
    <div id="ciButtonData" style="display: none;">
        <span id="tag"><?php echo "a"; ?></span>
        <span id="link"><?php echo "http://www.link-to-my-url.com/"; ?></span>
        <span id="linktext"><?php echo htmlentities( "my link text" ); ?></span>
    </div>
    
    <a id="ciButton" href="javascript:void(0)" 
        onmouseup="bMouseUp( document.getElementById( this.id+"Data" ) )"
        onmouseout="bMouseOut()"
        style="color: #c00">
        Click Me
    </a>
    
    // in this case, the function bMouseUp takes care of getting data 
    // from the Dom Element passed and assembling the element if necessary.
    
    HTML:
    alternately, you can also have
    <a 
        id="ciButtonData" 
        style="display: none;"  
        href="<?php echo "http://www.link-to-my-url.com/"; ?>">
       <?php echo htmlentities( "my link text" ); ?>
    </a>
    
    <a id="ciButton" href="javascript:void(0)" 
        onmouseup="bMouseUp( document.getElementById( this.id+"Data" ) )"
        onmouseout="bMouseOut()"
        style="color: #c00">
        Click Me
    </a>
    
    // in this case, the function bMouseUp( element ) takes care of getting data 
    // from the invisible a tag, by using 
    // element.href & element.textContent
    HTML:
    using js object you can do the same by:
    <script>
    var ciButtonData = {
        link: <?php echo "http://www.link-to-my-url.com/"; ?>,
        linktext: <?php echo htmlentities("my link text"); ?>
    };
    </script>
    
    <a id="ciButton" href="javascript:void(0)" 
        onmouseup="bMouseUp( ciButtonData )"
        onmouseout="bMouseOut()"
        style="color: #c00">
        Click Me
    </a>
    
    HTML:

    further, i am not pretty sure, what are you doing with the bMouseUp( arg ) function. But I am sure, you will be able to ply it to your needs.
     
    bvraghav, Jun 22, 2010 IP
  14. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thanks everyone and thanks bvraghav. Your DOM suggestion reminded my of a way I used to transfer html to javascript which is very similar to what you suggested. Thanks!
     
    Imozeb, Jun 22, 2010 IP