简体   繁体   English

在单击按钮时如何运行php脚本

[英]On clicking a button how to run php script

Im a beginner in PHP. 我是PHP的初学者。 I want to add the Functionality to like button. 我想将功能添加到喜欢按钮。 Whenever a user clicks like button then the insert query is to be run to insert values in db. 每当用户单击“赞”按钮时,将运行插入查询以在db中插入值。 There are several images on home page, the corresponding productimage info(productid) must be inserted in product_likes db.` 主页上有几张图像,必须在product_likes db中插入相应的productimage info(productid)。

     <?php
     $user_name=$_SESSION['user_name'];
     $query="SELECT * FROM product_info";
     $result= mysqli_query($con, $query);
     while ($row = mysqli_fetch_array($result)) {


?>
  <div class="w3-container"><br>
    <img src="<?php echo "img/product_img/".$row['productimage'].""; ?>">
    <p><b>Product Name:  </b><?php echo"".$row["productname"].""?><br>    
    </p>
    <form id="like" method="post" action="home1.php">
        <button type="submit" name="like"><i class="fa fa-heart"></i>  Like</button>
    <?php 
    if(isset($_POST['like'])){
        $result=mysqli_query($con,"INSERT INTO product_likes VALUES ('','".$row['productid']."','".$row1['sellerid']."','".$buyerid."')");

    }
    ?>
    </form>
  </div> 
     <?php } ?>`

But whenever I run this the same productid, sellerid and buyerid corresponding to first image are inserted in database and only the first image is displayed. 但是每当我运行此产品时,对应于第一张图片的相同productid,sellerid和Buyerid就会插入数据库中,并且仅显示第一张图片。 Is there a way to correct this issue? 有没有办法解决这个问题?

First thing that you need to understand is, PHP is server side language, gets executed before the client, and JavaScript is client side language, gets executed after the server side has finished processing and there's no going back to the server. 您需要了解的第一件事是,PHP是服务器端语言,在客户端之前执行,而JavaScript是客户端语言,在服务器端完成处理之后执行,并且没有返回到服务器。

When you want to do something like speaking to a server based on user's behaviour, you need to have an end-point configured and fire an AJAX call to the server. 当您想要做类似基于用户行为与服务器对话的操作时,您需要配置一个端点并向该服务器发出AJAX调用。 Simple example using jQuery to like a post would be: 使用jQuery赞帖子的简单示例是:

 $(function() { $("a").click(function(e) { e.preventDefault(); $this = $(this); if ($(this).text().trim() == "Like") { $.post("/posts/like", { PostID: 1 }, function(res) { if (res == "success") $this.text("Unlike"); }); $this.text("Unlike"); } else { if ($(this).text().trim() == "Unlike") { $.post("/posts/unlike", { PostID: 1 }, function(res) { if (res == "success") $this.text("Like"); }); $this.text("Like"); } } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a href="#">Like</a> 

The above example kinda "works", because of the fall-back, but pretty much this is the concept. 上面的示例由于存在后备功能,因此有点“有效”,但这几乎是概念。 The whole PHP code that makes the "Like" or "Unlike" should be given separately and using jQuery's AJAX function, you need to fire it off. 应当单独给出使“ Like”或“ Unlike”成为可能的整个PHP代码,并使用jQuery的AJAX函数,将其触发。

Both the above URLs: /posts/unlike and /posts/like take in a data parameter PostID and based on that make the necessary changes in the database. 以上两个URL: /posts/unlike/posts/like接受数据参数PostID并根据该参数在数据库中进行必要的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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