douyu3145 2019-08-13 10:08
浏览 78

Jquery仅在第一次硬刷新后重新加载mysql插入

I'm making a shop and message system within my mobile website and all works fine after i refresh it by refreshing the browser.

My problem is when i refresh the message page and go to my shop page it loads in the php mysql script but if i than change a filter for the shop and reload the content with jquery it doesn't show up. If i refresh the whole page and try to change filters for the shop after that it all works like a charm.

It's only on first visiting the page that it doesnt work after jquery refresh of the page.

I've tried multiple reload div scripts with jquery but al gave the same problem. I've also tried echoing test within my scripts part. It echos only when the rest is also working (So all of the php within the reloaded divs just disappear)

Reloadscript that i use:

$("#ShopScriptsReload").load(" #ShopScriptsReload > *");

Php mysql:

$sql = "SELECT * FROM shop ORDER BY id DESC";
$result = $conn->query($sql);

And i work with cookies to change the filter. So a cookies is set with javascript and it translate to (little edit: the cookie is set with variables that are hardcoded in):

if (empty($_COOKIE["setFilterCookie"])){$_COOKIE["setFilterCookie"]="1";}
    $fltr = $_COOKIE["setFilterCookie"];
    if ((empty($fltr) or ($fltr == 1)))
    {$WhereFltr = "";}
    elseif ($fltr == 2)
    {$WhereFltr = "WHERE tag='digitaal'";}
    elseif ($fltr == 3)
    {$WhereFltr = "WHERE tag='pins'";}
    elseif ($fltr == 4)
    {$WhereFltr = "WHERE tag='fysiek'";}
$sql = "SELECT * FROM shop WHERE filter='$fltr' ORDER BY id DESC";
$result = $conn->query($sql);

I've already tried the following methods: Removing previous pages from dom, Delete cache, tried to remove the reload part and work with GET method (this works perfectly but it is not what i want).

Well that everything works without the need for a whole refresh.

Thank you in advance guys and girls :)

  • 写回答

1条回答 默认 最新

  • dongzhi5386 2019-08-27 13:22
    关注

    The problem here is, when you are refreshing the browser only then there is a server call, but refreshing the JQuery is on the client side! you can use ajax for that purpose i will demonstrate:

    in HTML

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    
        <script
                src="https://code.jquery.com/jquery-3.4.1.min.js"
                integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
                crossorigin="anonymous">
        </script>
        <script type="text/javascript" src="./app.js"></script>
    </head>
    <body>
    <select name="shop-criteria" class="shop-criteria">
        <option value="">all</option>
        <option value="2">digital</option>
        <option value="3">pins</option>
        <option value="4">fysiek</option>
    </select>
    </body>
    </html>

    in JavaScript the file app.js

    $(document).ready(function(){
    
        $(document).on('change', '.shop-criteria', function (e) {
    
            let fd = new FormData();
            let tag = $(this).val();
            fd.append('tag', tag);
    
            $.ajax({
    
                url: "shop.php",
                type: "POST",
                data: fd,
                processData: false,
                contentType: false,
    
                complete: function (results) {
    
                    let response = JSON.parse(results.responseText);
                    my_function.call(this, response);
                }
    
    
    
    
            });
        });
    
    
        function my_function(response) {
            console.log("response", response);
        }
    
    });

    in PHP shop.php

     `<?php
      $tag = $_REQUEST['tag'];
    
      switch($tag) {
    
         case 2:
            $WhereFltr = "WHERE tag='digital'";
         break;
    
        case 3:
           $WhereFltr = "WHERE tag='pins'";
        break;
    
        case 4:
           $WhereFltr = "WHERE tag='fysiek'";
        break;
    
        default:
           $WhereFltr = "";
        break;
       }
    
       $sql = "SELECT * FROM shop WHERE filter='$fltr' ORDER BY id DESC";
       $result = $conn->query($sql);`
    
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?