Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Try out my first PHP web app!

  • This topic is empty.
Viewing 15 posts - 196 through 210 (of 211 total)
  • Author
    Posts
  • #183718
    chrisburton
    Participant

    Using a data attribute is perfect for this

    data-username="drose"
    

    http://html5doctor.com/html5-custom-data-attributes/

    #183721
    chrisburton
    Participant
    data-username="<?php echo $username; ?>"
    

    Yup.

    #183726
    __
    Participant

    Is putting PHP right inside of HTML like that best practice? Seems a bit sloppy. No?

    Yes and no. In this case, it is wholly templating-related, and that’s fine. It’s what PHP is for. It’s the business logic of your application that shouldn’t be mixed in with HTML.

    The fact that you’re worried about this sort of thing is a good sign, though.

    #183795
    __
    Participant

    Regarding this:

    while ($row = mysqli_fetch_array($comments)) {
    
        $comment =  htmlentities($row['comment']); 
        $comment_by = $row['comment_by'];
        ?>
    
        <div id='commentbox'>
        <p><?php echo $comment;?></p>
        <span class='detail1' id='username'><?php echo $comment_by;?></span>
        <button onclick='post()'>Click</button>
        </div>
        
    
        <?
    }
    

    Why are you using an ID rather than a data-* attribute as discussed above? It would work fine, while using an ID would create multiple duplicate IDs on the page (which is invalid; every ID must be unique).

    <div id="commentbox" data-username="<?php echo $comment_by ?>">
        <p><?php echo $comment;?></p>
        <button onclick="post()">Click</button>
    </div>
    
    #183798
    __
    Participant

    Also, using inline event handlers cause problems, one of which being that it makes it more difficult to get the value that you want. You should use .addEventListener (regardless) instead. See “Modern DOM Events” in this article.

    #183800
    __
    Participant

    I used the data-attribute earlier today and it gave me the same problem.

    Not quite: you’re not even getting to the part where you try to use the data attribute, because you’re still trying to select an element by a non-unique id. You can’t do this. It will never work. ids must be unique on a page.

    If you use modern event listeners, then you’ll get the event object when it fires, and you can find the element you’re looking for very easily:

    function commentbox_onClick( event ){
        var username = event.target.parentNode.getAttribute('data-username');
        console.log( username );
    }
    
    #183803
    __
    Participant

    To clarify, there are really two problems here.

    1. In your PHP, you are outputting elements in a loop, all with the same id: commentbox, username, and so forth. However, HTML expects ids to be unique. commentbox should be a class name, username should be a data attribute. Remove the ids.

    2. In your JS, you’re using inline event handlers, which are very old (DOM0) and generally shouldn’t be used anymore. See my post above.

    #183809
    __
    Participant

    Ive never really worked with JS at all.

    Event handling is a hell of a place to start! : )

    Study, study, study. Here’s an example that does something close to what you’re attempting:

    http://codepen.io/adrian-enspired/pen/Inpfx

    #184682
    __
    Participant

    Are you talking about inline HTML in your markdown that is intended to be markup (and is displayed as text instead), or is intended to be displayed as text (and is displayed as encoded text instead)?

    Once you differentiate between the three possibilities you can make sure you’re applying htmlentities in the right spot. Beyond that, you might look at the $double_encode parameter for htmlentities and make sure you’re not encoding things twice.

    Let me know when you want to see code

    In general, you should automatically be sharing enough code to make your issue clear. When you know generally where the issue lies, all the more reason.

    #184685
    drose379
    Participant

    @traq @BenWalker OK. I am trying to get text to show as HTML elements (<h1>, <script>,

    ) on the page, so people can show their code. But I do not want the code to actually be interpreted as HTML. I have been using htmlenteties() in my code and it has been doing exactly that; but, with my new inline-code markdown, whenever I put a HTML tag inside the back ticks () the tags get displayed as this (<script>) when I really want that to say <script>. This works all of the time accept for when I put the text inside back ticks () when making the comment. So weird! Ive tried looking into html_enteties() a bit more, but nothing seems to be changing the result. Hoping there is a more solid function out there that I could use to accomplish this, or maybe theres just a bug in my code. Check it out: http://pastie.org/9592497

    #184686
    __
    Participant

    If you look around line 350 of parsedown.php, you’ll see that it automatically encodes HTML markup inside of code blocks. You are also encoding the entire comment (indiscriminately) when you save it to the database, so the stuff inside the code block is getting double-encoded.

    Did you see my comment above about the $double_encode parameter for htmlentities?

    #184687
    drose379
    Participant

    @traq, wow, this is going to be a challenge then. Would $double_encode help fix this problem easier?

    #184688
    drose379
    Participant

    @traq , fixed it! Just went into ParseDown. php and removed the line where the text is converted into an entity. Should work fine now. Do you think this was the best method?

    #184689
    __
    Participant

    No.

    Did you read about what $double_encode is for?

    #184690
    drose379
    Participant

    Not yet, going to read up on it tomorrow morning.

Viewing 15 posts - 196 through 210 (of 211 total)
  • The forum ‘Back End’ is closed to new topics and replies.