Javascript 单击一个按钮如何运行php脚本
我是PHP的初学者。我想将功能添加到“喜欢”按钮。每当用户单击like按钮时,将运行insert查询以在db中插入值。主页上有多个图像,必须将相应的productimage信息(productid)插入product\ db中`Javascript 单击一个按钮如何运行php脚本,javascript,php,jquery,mysqli,Javascript,Php,Jquery,Mysqli,我是PHP的初学者。我想将功能添加到“喜欢”按钮。每当用户单击like按钮时,将运行insert查询以在db中插入值。主页上有多个图像,必须将相应的productimage信息(productid)插入product\ db中` <?php $user_name=$_SESSION['user_name']; $query="SELECT * FROM product_info"; $result= mysqli_query($con, $query
<?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 } ?>`
">
产品名称:
像
您需要了解的第一件事是,PHP是服务器端语言,在客户端之前执行,JavaScript是客户端语言,在服务器端完成处理之后执行,不返回服务器
当您想根据用户的行为与服务器对话时,需要配置一个端点并向服务器发出AJAX调用。使用jQuery来喜欢帖子的简单示例如下:
$(函数(){
$(“a”)。单击(功能(e){
e、 预防默认值();
$this=$(this);
if($(this.text().trim()==“Like”){
$.post(“/posts/like”{
职位:1
},功能(res){
如果(res==“成功”)
$this.text(“不像”);
});
$this.text(“不像”);
}否则{
if($(this).text().trim()=“不像”){
$.post(“/posts/DISTANCE”{
职位:1
},功能(res){
如果(res==“成功”)
$this.text(“Like”);
});
$this.text(“Like”);
}
}
});
});
您需要了解的第一件事是,PHP是服务器端语言,在客户端之前执行,JavaScript是客户端语言,在服务器端完成处理之后执行,并且不会返回服务器
当您想根据用户的行为与服务器对话时,需要配置一个端点并向服务器发出AJAX调用。使用jQuery来喜欢帖子的简单示例如下:
$(函数(){
$(“a”)。单击(功能(e){
e、 预防默认值();
$this=$(this);
if($(this.text().trim()==“Like”){
$.post(“/posts/like”{
职位:1
},功能(res){
如果(res==“成功”)
$this.text(“不像”);
});
$this.text(“不像”);
}否则{
if($(this).text().trim()=“不像”){
$.post(“/posts/DISTANCE”{
职位:1
},功能(res){
如果(res==“成功”)
$this.text(“Like”);
});
$this.text(“Like”);
}
}
});
});
您试图以顺序模式同时执行JavaScript和PHP。这不会发生。PHP先执行,然后执行JavaScript。此外,PHP在服务器端执行,JavaScript在客户端执行。您试图以顺序模式同时执行JavaScript和PHP。这不会发生。PHP先执行,然后执行另外,PHP在服务器端执行,JavaScript在客户端执行。