标题刷新不起作用

Posted

技术标签:

【中文标题】标题刷新不起作用【英文标题】:Header refresh not working 【发布时间】:2017-05-02 11:55:35 【问题描述】:

我的巨大代码的一部分是:

$query = "UPDATE grocery SET shop='$shop', category='$category', item='$item', quantity='$qnty', unit='$unit', price_based_on='$price_based_on', mrp='$mrp', sellers_price='$sellers_price', last_updated_on='$last_updated_on' WHERE id=$id";

if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))

                            $result = mysqli_query($dbc, $query)
                                                or die(mysqli_error($dbc));

header("Refresh: 5; url=index.php");
echo "<script>alert('This works');</script>";

在显示消息this works 时,页面不会重新加载。为什么是这样?如果显示消息“this works”,这意味着标题行也必须执行,对吗?那么为什么页面在 5 秒后没有重新加载呢?我什至使用 jQuery 确保每次刷新页面时都会收到通知。

$(window).on('load', function()
    alert("Reloaded!");
);

注意:为了完整起见,我的整个代码是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SomuFinance - Personal Finance Manager</title>
    <link rel="stylesheet" type="text/css" href="indexStyle.css">
    <script src="scripts/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
    <style type="text/css">
        #addItemContainer 
            background-color: rgba(204,207,232,1);
        
    </style>
    <script type="text/javascript">
        var flag=0;
    </script>
</head>
<body>
    <form id="list" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <div id="container">
            <input type="submit" class="button" name="edit" id="edit" value="Edit" />
            <input type="button" class="button" name="delete" value="Delete" />
            <input type="hidden" id="action" name="action">
            <input type="hidden" id="id" name="id">
            <table id="listDB">
                <tr>
                    <th>Select</th>
                    <th>ID</th>
                    <th>Category ID</th>
                    <th>Shop</th>
                    <th>Item</th>
                    <th>Quantity</th>
                    <th>Unit</th>
                    <th>Price Based On</th>
                    <th>MRP</th>
                    <th>Seller's Price</th>
                    <th>Last Updated On</th>
                </tr>
                <?php
                    $dbc =  mysqli_connect('localhost','root','atlantis2016','itemDB')
                                or die("Error Connecting to Database");

                    if(isset($_POST['confirmDelete']))
                    
                        if($_POST['action']=='confirmDelete')
                        
                            foreach ($_POST['selected'] as $delete_id) 
                            
                                $query = "DELETE FROM grocery WHERE id = $delete_id";
                                mysqli_query($dbc, $query)
                                    or die('Error querying database.');
                            
                        
                    

                    $query1 = "SELECT DISTINCT category FROM grocery";
                    $result1 = mysqli_query($dbc, $query1)
                                or die("Error Querying Database");

                    while($row = mysqli_fetch_array($result1))
                    
                        $category = $row['category'];
                        $query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
                        $result2 = mysqli_query($dbc, $query2)
                                or die("Error Querying Database");

                        echo '<tr>';
                            echo '<td class="catHead" colspan=11>'.$category.'</td>';
                        echo '</tr>';
                        $catCount=1;

                        while($inRow = mysqli_fetch_array($result2))
                        
                            $id = $inRow['id'];
                            $shop = $inRow['shop'];
                            $item = $inRow['item'];
                            $qnty = $inRow['quantity'];
                            $unit = $inRow['unit'];
                            $price_based_on = $inRow['price_based_on'];
                            $mrp = $inRow['MRP'];
                            $sellers_price = $inRow['sellers_price'];
                            $last_updated_on = $inRow['last_updated_on'];

                            echo '<tr>';
                                echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
                                echo '<td>'.$id.'</td>';
                                echo '<td>'.$catCount.'</td>';
                                echo '<td>'.$shop.'</td>';
                                echo '<td class="leftAligned">'.$item.'</td>';
                                echo '<td>'.$qnty.'</td>';
                                echo '<td>'.$unit.'</td>';
                                echo '<td>'.$price_based_on.'</td>';
                                echo '<td class="pri">₹'.$mrp.'</td>';
                                echo '<td class="pri">₹'.$sellers_price.'</td>';
                                echo '<td>'.$last_updated_on.'</td>';
                            echo '</tr>';

                            $catCount++;
                        
                    
                ?>
            </table>
        </div>

        <div class="dialogBG">
            <div id="deleteConfirmDialog" class="dialog">
                <div class="closeDialog"></div>
                <p>Sure you want to delete the selected Data?</p>
                <input type="submit" id="confirmDelete" class="dialogButton" name="confirmDelete" value="Delete" />
                <input type="button" id="cancelDelete" class="dialogButton cancelButton" name="cancelDelete" value="Cancel" />
            </div>
        </div>
    </form>

    <div class="dialogBG">
        <div id="addItemContainer" class="dialog">
            <div class="closeDialog"></div>
            <h1>Edit Item</h1>
            <form id="data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
                <?php
                    if(isset($_POST['action']))
                    
                        if($_POST['action']=='edit')
                        
                            echo '<script>flag=1;</script>';
                            foreach ($_POST['selected'] as $edit_id) 
                            
                                $query = "SELECT * FROM grocery WHERE id = $edit_id";
                                $result = mysqli_query($dbc, $query)
                                            or die('Error querying database.');
                                break;
                            

                            $inRow = mysqli_fetch_array($result);

                            $id = $inRow['id'];
                            $shop = $inRow['shop'];
                            $category = $inRow['category'];
                            $item = $inRow['item'];
                            $qnty = $inRow['quantity'];
                            $unit = $inRow['unit'];
                            $price_based_on = $inRow['price_based_on'];
                            $mrp = $inRow['MRP'];
                            $sellers_price = $inRow['sellers_price'];
                            $last_updated_on = $inRow['last_updated_on'];

                        
                    
                ?>
                <div class="leftAligned">
                    <input type="hidden" id="id" name="id" value="<?php echo $id; ?>" required>
                    <div class="inp">
                        <label for="shop">ID : </label>
                        <input type="text" id="id_disp" name="id_disp" value="<?php echo $id; ?>" required disabled>
                    </div> <br>
                    <div class="inp">
                        <label for="shop">Shop : </label>
                        <input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="category">Category : </label>
                        <input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="item">Item : </label>
                        <input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="qnty">Quantity : </label>
                        <input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="unit">Unit : </label>
                        <input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="price_based_on">Price based on : </label>
                        <select name="price_based_on" id="price_based_on">
                            <option value="kilos">Kilos</option>
                            <option value="packet">Packet</option>
                            <option value="bottle">Bottle</option>
                            <option value="box">Box</option>
                            <option value="piece">Piece</option>
                        </select>
                    </div> <br>
                    <div class="inp">
                        <label for="mrp">MRP (₹) : </label>
                        <input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
                    </div> <br>
                    <div class="inp">
                        <label for="sellers_price">Seller's Price (₹) : </label>
                        <input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
                    </div> <br>
                    <div class="inp">
                        <label for="last_updated_on">Last Updated on : </label>
                        <input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
                    </div>
                </div>
                <div class="inp">
                    <input id="insertButton" type="submit" name="submit" value="Update">
                </div>
                <div id="message">
                    <?php
                        if(isset($_POST['submit']))
                        
                            $id = $_POST['id'];
                            $shop = $_POST['shop'];
                            $category = $_POST['category'];
                            $item = $_POST['item'];
                            $qnty = $_POST['qnty'];
                            $unit = $_POST['unit'];
                            $price_based_on = $_POST['price_based_on'];
                            $mrp = $_POST['mrp'];
                            $sellers_price = $_POST['sellers_price'];
                            $last_updated_on = $_POST['last_updated_on'];
                            $result=null;

                            $query = "UPDATE grocery SET shop='$shop', category='$category', item='$item', quantity='$qnty', unit='$unit', price_based_on='$price_based_on', mrp='$mrp', sellers_price='$sellers_price', last_updated_on='$last_updated_on' WHERE id=$id";

                            if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
                            
                                $result = mysqli_query($dbc, $query)
                                                    or die(mysqli_error($dbc));
                            
                            header("Refresh: 5; url=index.php");
                            echo "<script>alert('This works');</script>";
                        
                    ?>
                    <script>
                    $(document).ready(function()

                        $( "#data" ).validate(
                          rules: 
                            qnty: 
                              number: true
                            ,
                            mrp: 
                              number: true
                            ,
                            sellers_price: 
                              number: true
                            
                          ,
                          messages: 
                            qnty : 
                                number: '<br> <span class="failure err">Enter a valid quantity</span>'
                            ,
                            mrp : 
                                number: '<br> <span class="failure err">Enter a valid MRP</span>'
                            ,
                            sellers_price : 
                                number: '<br> <span class="failure err">Enter a valid Price</span>'
                            ,
                          
                        );
                    );
                    </script>
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript">
        $(window).on('load', function()
            alert("Reloaded!");
        );

        $(document).ready(function()
            $('.button').click(function(event)
                if($(this).val()=="Delete")
                
                    $("#deleteConfirmDialog").show(200).parent(".dialogBG").fadeIn(200);
                    $("#action").val('confirmDelete');
                
                else if($(this).val()=="Edit")
                
                    event.preventDefault();
                    $("#action").val('edit');
                    $("#list").submit();
                
            );

            if(flag===1)
            
                console.log("This shouldn't be there if the page reloads!");
                $("#addItemContainer").show(200).parent(".dialogBG").fadeIn(200);
            

            $('#confirmDelete').click(function()
                $(".closeDialog").trigger("click");
            );
            $('#cancelDelete').click(function()
                $("input:checkbox[name='selected[]']").prop('checked', false);
            );
            $(".closeDialog").click(function (e)
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            );
            $(".cancelButton").click(function (e)
                $(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
            );
        );
    </script>

    <?php
        mysqli_close($dbc);
    ?>
</body>
</html>

注意:不知何故重新启动 php 服务器以及 Praveen 的回答解决了这个问题!

【问题讨论】:

你应该试试这个link 请原谅我的错误 嗨,你可以试试header("location: index.php");看看它是否可以重定向 @NewbeeDev 它重定向没有错误! @SomenathSinha 现在再次尝试您的代码 【参考方案1】:

只需添加 ob_start();作为&lt;?php ob_start(); 之后的第一行。在您设置标题的同一文件中。它会起作用的。

这是因为“标头已发送”。

您在开发时启用了 php 错误,它将对您有所帮助。

在页面顶部使用它 ini_set('display_errors', '1');error_reporting(E_ALL);

【讨论】:

此代码将向您显示错误。然后我可以指导你。 没有更多错误,感谢您!但是 ob_start() 有什么作用呢?

以上是关于标题刷新不起作用的主要内容,如果未能解决你的问题,请参考以下文章

拉动刷新不起作用

刷新页面时 CreateAsyncThunk 不起作用

快速刷新控制,endRefreshing 不起作用 [重复]

Spring Boot 2:动态刷新属性不起作用

为啥我的刷新验证码在 Firefox 中不起作用? [复制]

更新后刷新网格不起作用