Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php mysqli喜欢不重新加载页面_Php_Jquery_Ajax - Fatal编程技术网

Php mysqli喜欢不重新加载页面

Php mysqli喜欢不重新加载页面,php,jquery,ajax,Php,Jquery,Ajax,我有一些带有代码的post和like按钮: <a href="likes.php?userid='$userid'&postid='$postid'">Like</a> 现在在likes.php中,我对userid和postid进行了一些Get编码,以找到哪个用户喜欢哪个帖子,并将其存储到数据库中。它工作良好,但它可以重新加载页面。它转到likes.php,如果成功,它将返回主页或我想要的任何其他页面。现在我的问题是,如果我应该在页面中包含likes.php

我有一些带有代码的post和like按钮:

<a href="likes.php?userid='$userid'&postid='$postid'">Like</a>


现在在likes.php中,我对userid和postid进行了一些Get编码,以找到哪个用户喜欢哪个帖子,并将其存储到数据库中。它工作良好,但它可以重新加载页面。它转到likes.php,如果成功,它将返回主页或我想要的任何其他页面。现在我的问题是,如果我应该在页面中包含likes.php代码,其中有posts和like按钮,我如何在不重新加载页面的情况下完成这项工作。如果可能的话,使用
?或者如果有人有更好的解释,他也可以发布

是的,因为您使用了code
ahref
标记,这就是它应该如何工作的。如果您不想重新加载,请尝试使用AJAX。这不是完整的答案,但您只需要通过AJAX来实现。您会发现很多关于它的视频和演示

检查此示例:

更新:

Mainfile.php

<html>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">   </script>
<?php
session_start();
$userid=$_SESSION['userid'];
?>

<div id="like">

<a href="#" id="postid"  onclick="likeclick(this);">Like</a> 


    <!-- here id should be different for every post .I would prefer using    post id to this ahref id because i would use that to detect what post it is actually.  -->
 </div>
 </html>


 <script type="text/javascript">
function likeclick(element)
{
    var postid=element.id;
    var userid=<?php echo json_encode($userid);?>;
    //You can use different methods to pass variable to javascript.I used this one because it is easy to implement
    //it has some cons to it .Do check for that on google also.
    //http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript
    //Check this link for more.

    $.ajax({
        type:'POST',
        url:'getlike.php',
        data:{"userid":userid,"postid":postid},
        success : function(content)
        {
                $('#like').html(content);
            //This gets the html content from the getlike page and displays in the div on this page.
            //Note:I have used .html which replaces any previous content inside the 'like' div.
    })

}
</script>
<?php
require 'connect.inc.php'; //This make a connection to the database
$userid=$_POST['userid'];
$postid=$_POST['postid'];

$statement=$mysqli->prepare("select `likes` from `posts` where `postid`=?");
$statement->bind_param("s",$postid);
$statement->execute();
$result=$statement->get_result();
while($row=$result->fetch_assoc())
{
   $likes_on_this_post=$row['likes'];
}
$likes_on_this_post=$likes_on_this_post+1; //Added one like more.
$statement=$mysqli->prepare("update `posts` set `likes`=?");
$statement->bind_param("s",$likes_on_this_post);
$statement->execute();
echo "+".$likes_on_this_post;
 //This echo is actually the main thing.When this page runs through ajax the    response is given back to the calling object
//What goes with response are the html contents on this page as well as whatever i echo on this page.
//Considering this example i have only echoed the no of likes and it doesn't contain any sort of html content so only the echoed element goes.
?>

函数likeclick(元素)
{
var postid=element.id;
var userid=;
//您可以使用不同的方法将变量传递给javascript
//它有一些缺点。请在谷歌上检查一下。
//http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript
//查看此链接了解更多信息。
$.ajax({
类型:'POST',
url:'getlike.php',
数据:{“userid”:userid,“postid”:postid},
成功:功能(内容)
{
$('#like').html(内容);
//这将从getlike页面获取html内容,并显示在此页面的div中。
//注意:我使用了.html来替换“like”div中以前的任何内容。
})
}
getlike.php

<html>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js">   </script>
<?php
session_start();
$userid=$_SESSION['userid'];
?>

<div id="like">

<a href="#" id="postid"  onclick="likeclick(this);">Like</a> 


    <!-- here id should be different for every post .I would prefer using    post id to this ahref id because i would use that to detect what post it is actually.  -->
 </div>
 </html>


 <script type="text/javascript">
function likeclick(element)
{
    var postid=element.id;
    var userid=<?php echo json_encode($userid);?>;
    //You can use different methods to pass variable to javascript.I used this one because it is easy to implement
    //it has some cons to it .Do check for that on google also.
    //http://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript
    //Check this link for more.

    $.ajax({
        type:'POST',
        url:'getlike.php',
        data:{"userid":userid,"postid":postid},
        success : function(content)
        {
                $('#like').html(content);
            //This gets the html content from the getlike page and displays in the div on this page.
            //Note:I have used .html which replaces any previous content inside the 'like' div.
    })

}
</script>
<?php
require 'connect.inc.php'; //This make a connection to the database
$userid=$_POST['userid'];
$postid=$_POST['postid'];

$statement=$mysqli->prepare("select `likes` from `posts` where `postid`=?");
$statement->bind_param("s",$postid);
$statement->execute();
$result=$statement->get_result();
while($row=$result->fetch_assoc())
{
   $likes_on_this_post=$row['likes'];
}
$likes_on_this_post=$likes_on_this_post+1; //Added one like more.
$statement=$mysqli->prepare("update `posts` set `likes`=?");
$statement->bind_param("s",$likes_on_this_post);
$statement->execute();
echo "+".$likes_on_this_post;
 //This echo is actually the main thing.When this page runs through ajax the    response is given back to the calling object
//What goes with response are the html contents on this page as well as whatever i echo on this page.
//Considering this example i have only echoed the no of likes and it doesn't contain any sort of html content so only the echoed element goes.
?>

听说过ajax吗?然后阅读文档。我相信你会找到有用的演示和代码。很抱歉,我不明白你在说什么……你有没有为它编写过ajax?@Noob查看这篇文章:没有,我没有。只有php和mysqliCan我可以使用ajax get var with ref?这可能是个愚蠢的问题,但当我在iframe内使用iframe with it load page时。ref可以像页面上的任何地方一样使用iframe,然后使用ajax get var从ref获取用户ID吗?说真的,我甚至没有理解你刚才说的,但是你可以用PHP和ajax做任何事情。我的意思是,你的问题的真正答案是使用PHP和ajax。相信我,去看一些视频和阅读一些文档。从长远来看,这肯定会对你有所帮助。:)I从phpacademy找到了一些东西这不完全是我需要的,但它会帮助很多。无论如何,谢谢你,我可以给你整个工作代码,但你不会得到任何东西。给我ofc我不会使用它,但它更容易尝试了解如何工作,然后制作我自己的,然后粘贴在代码中…如果你可以。。