简体   繁体   中英

Is it possible to embed PHP code within the class or name attributes of HTML tags?

I am trying to create a like buttons for by blog posts. The problem I am facing is that I have no way to link up my like buttons with each blog post. Currently, all the like buttons share the same class and name attributes. I tried to use the following:

 <?php 

 include_once('connectserver.php');

 $query_blog = mysql_query("SELECT             `category_x`,`sub_category_x`,`specified_sub_category_x`,`tag_1`,`tag_2`,`tag_3`,`title`,`contents`,`date_posted` FROM `posts` ORDER BY `date_posted` DESC");

 while($get_rows = mysql_fetch_assoc($query_blog)) {





 $get_title = $get_rows['title'];
 $get_category = $get_rows['category_x'];
 $get_sub_category = $get_rows['sub_category_x'];
 $get_specified_sub_category = $get_rows['specified_sub_category_x'];
 $get_tag1 = $get_rows['tag_1'];
 $get_tag2 = $get_rows['tag_2'];
 $get_tag3 = $get_rows['tag_3'];
 $get_contents = $get_rows['contents'];
 $get_date_posted = $get_rows['date_posted'];


 $new_date = date('dS F Y', strtotime($get_date_posted));



 echo "<p class='blog-heading' align='left'><font face='Narkisim' size='5' color='#3E537C'>       <strong>".$get_title."</strong></font></p>";
 echo "<br><pre class='blog-underheading'><font face='David' size='2' color='black'><font   face='David' size='3' color='#0040A1'>".$new_date."</font> BY <a class='blog-link1' href='home.php'></a>  |  <a class='blog-link1' href='eere'>UpVote</a></font></pre><br>";
 echo "<br><p class='blog-content' align='justify'><font size='4' face='Narkisim'   color='#545B6A'>".$get_contents."</font></p><br><hr><br>";



 echo "<pre class='blog-ending'><font face='David' size='2' color='black'>|  POSTED IN <a class='blog-link1' href='eere'>".strtoupper($get_category)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_sub_category)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_specified_sub_category)."</a>  |  TAGGED <a class='blog-link1' href='eere'>".strtoupper($get_tag1)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_tag2)."</a>, <a class='blog-link1' href='eere'>".strtoupper($get_tag3)."</a>  |</font></pre><br><hr>";

echo "


";

echo "

"; }

?>

This piece of code serves to increment the blog counter each time a new blog post is made so that I can link up my blog post with that unique name field. Is that even conceptually possible and if not, what other methods can I use to solve the issue. The above code snippet give me errors btw.

You can try when you query your blog posts, you also query the blog post ID (assuming you have one at your database. You should always have a PRIMARY KEY for each record). When you create the button you can do the following:

echo "<button class="..." name="$BlogID" type="submit">Like</button>";

OR Create a hidden input with every Blog Post you fetched. So when you submit a form the hidden input will be submitted with it.

echo "<input type="hidden" name="postID" value="$blogID">";

Then you the PHP script you are trying to pass to can process the value and modify the database accordingly.

PS you should us PHP extension PDO to do your database query. mysql_query() approach is not as powerful. I made this mistake when I started my first web project, now I have to rewrite it using PDO. Hope this post is helpful!

Typically, instead of a using a counter in your server side code, you'd use some kind of sequence in the database, or other uinique identifier stored along with the blog post content. For instance, many people will use an integer primary key for every row in a table (eg all rows in the blog_post table have a colum blog_post_id). If this primary key is set to AUTO INCREMENT/SEQUENCE or whatever your database software uses, it will automatically be incremented with each blog post.

When you retrieve your blog posts, the blog_post_id will come along with it. Assume that your row is an associative array. You could do:

<form method="post" action="/like-blog-post.php" >
<?php foreach($rows as $row) { ?>
   <input type="submit" name="blog_post_<?php echo $row['blog_post_id']; ?> value="Like!" />
<?php } ?>
</form>

Whenever someone clicks a like button, a post will be sent to the server, and you will receive a post request to /like-block-post.php

Doing this type of operation in this manner is a little clunky though. It causes a page change, the blog post id has to be parsed out, and the name/value is actually in the key of the $_POST array.

Another way to do it would be via a link tag like so (you could style the link to make it look like a button) :

<?php foreach($rows as $row) { ?>
<a href="like-blog-post.php?blog_post_id="<?php echo $row['blog_post_id']; ?>">Like!</a>
<?php } ?>

This would still cause a page change, but the data would be easier to deal with on the server side. This also would be problematic in that anyone could just spam that URL with a blog post id and like it infinitely many times. To prevent that, you'd want to pass the session id and a csrf token with the query parameters. Once the page loads, you could resolve the issue of a page change by adding onclick handlers via javascript. Your code that sets the handlers could use the exact same URL from the href attribute, but the onclick handler could make the request asyncronously in order to avoid a page change.

I would start small though with either the link, or the button, and then work your way up to adding in protection against multiple likes per user per blog post, and then to avoiding a page change/refresh.

You definitely should not be maintaining your own blog post counter though, that is a task best suited to the database. If you'd rather not use the row id, you can come up with some other unique identifier column, index it as UNIQUE, and use that to find the blog post that has been liked.

Initially it seemed like you were asking if you can write out php code to the client and have it run. If you were curious about that, the answer is no. Once the page is delivered to the client, any php code will just be text in the document. The client will not execute it.

write your code like this please. it will give you better idea.

<?php
include_once ('connectserver.php');
$query_blog = mysql_query("SELECT 
                        `category_x`,
                        `sub_category_x`,
                        `specified_sub_category_x`,
                        `tag_1`,
                        `tag_2`,
                        `tag_3`,
                        `title`,
                        `contents`,
                        `date_posted` 
                FROM 
                `posts` 
                ORDER BY `date_posted` DESC");
while ($get_rows = mysql_fetch_assoc($query_blog))
{
    $get_title = $get_rows['title'];
    $get_category = $get_rows['category_x'];
    $get_sub_category = $get_rows['sub_category_x'];
    $get_specified_sub_category = $get_rows['specified_sub_category_x'];
    $get_tag1 = $get_rows['tag_1'];
    $get_tag2 = $get_rows['tag_2'];
    $get_tag3 = $get_rows['tag_3'];
    $get_contents = $get_rows['contents'];
    $get_date_posted = $get_rows['date_posted'];
    $new_date = date('dS F Y', strtotime($get_date_posted));
?>
<p class='blog-heading' align='left'>
    <font face='Narkisim' size='5' color='#3E537C'>
        <strong>
            "<?=$get_title?>"
        </strong>
    </font>
</p>

<br>
<pre class='blog-underheading'>
    <font face='David' size='2' color='black'>
        <font face='David' size='3' color='#0040A1'>
            "<?=$new_date?>"
        </font>
        BY
        <a class='blog-link1' href='home.php'>User Name</a>
        |
        <a class='blog-link1' href='eere'>UpVote</a>
    </font>
</pre>
<br>

<br>
<p class='blog-content' align='justify'>
    <font size='4' face='Narkisim' color='#545B6A'>
        "<?=$get_contents?>"
    </font>
</p>
<br>
<hr>
<br>

<pre class='blog-ending'>
    <font face='David' size='2' color='black'>
        | POSTED IN
        <a class='blog-link1' href='eere'>". <?=strtoupper($get_category)

?>"</a>
        ,
        <a class='blog-link1' href='eere'>"<?=strtoupper($get_sub_category)?>"</a>
        ,
        <a class='blog-link1' href='eere'>"<?=strtoupper($get_specified_sub_category)?>"</a>
        | TAGGED
        <a class='blog-link1' href='eere'>"<?=strtoupper($get_tag1)?>"</a>
        ,
        <a class='blog-link1' href='eere'>"<?=strtoupper($get_tag2)?>"</a>
        ,
        <a class='blog-link1' href='eere'>"<?=strtoupper($get_tag3)?></a>
        |
    </font>
</pre>
<br>
<hr>
<?php
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM