dongnigeng1295 2014-01-25 16:28
浏览 49
已采纳

添加一个收藏夹按钮,用PHP中的值更新mysql数据库,无需重新加载页面[重复]

This question already has an answer here:

I've got a page which will randomly select a number of images from the database, and display a favourite button next to them. I guess the easy way would be to post the ID in the URL, and insert it into the database on page reload (then reload again to take it out the URL). Although, unless I add some sort of seed into the random select, on reload entirely new images will be selected, which obviously is not good if you were planning on favoriting more than one image on the page.

The other way I'd know how would be to open a new page in a tab, insert the ID into the database, and close the page, although I know it could be a lot better though, so would anyone have an example of how to make the link update the mysql table without leaving the page?

There is jquery running on the site, but I only know a little javascript from figuring bits out, so it'd be great if I could have something basic to work on. I can most likely get the font colour to change and all, just need help with the mysql bit.

Not that it really matters, but the table is only FavID, UserID, ImgID and Date

Also, I've had a quick look at this - How to update a mysql database without reloading page, is that for posting the ID to another page to run without the user being redirected? Will it also have the session variables or should I post the required ones through too?

Thanks

</div>
  • 写回答

1条回答 默认 最新

  • dpstir0081 2014-01-25 17:57
    关注

    With AJAX !

    if ((isset($_POST['Action'])) && (substr($_POST['Action'], 0, 11) == "AJAX_Update")) {
        $id = substr($_POST['Action'], 11);
        // Update SQL
        $query = "...";
        @$result = mysqli_query($query);
        if ($result) { print "OK"; } else { print "Error !"; }
        die();
    }
    
    (...)
    
    $id = 5; // Example
    print "<img src='update.png' style='cursor:pointer' onClick='AJAX_Update($id);'>"; print "<span id='result'></span>";
    
    (...)
    
    ?>
    
    <script language='JavaScript'>
    
                function AJAX_Update(id) {
                    xhr = new XMLHttpRequest();
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            document.all['result'].innerHTML = 'Update result : ' + xhr.responseText;
                        }
                    }
                    xhr.open("POST", "<?php print "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; ?>", true);
                    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xhr.send("Action=AJAX_Update" + id);
                }
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路