Header refresh not working
<p>A section of my HUGE code is:</p>

<pre><code>$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)&amp;&amp;!empty($category)&amp;&amp;!empty($item)&amp;&amp;is_numeric($qnty)&amp;&amp;!empty($unit)&amp;&amp;is_numeric($mrp)&amp;&amp;is_numeric($sellers_price)&amp;&amp;!empty($last_updated_on))
{
                            $result = mysqli_query($dbc, $query)
                                                or die(mysqli_error($dbc));
}
header("Refresh: 5; url=index.php");
echo "&lt;script&gt;alert('This works');&lt;/script&gt;";
</code></pre>

<p>While the message <code>this works</code> is displayed, the page doesn't get reloaded. Why is this? If the message "this works" is displayed, this means the header line must also be executed, right? So why doesn't the page appear to reload after 5 seconds? I've even ensured using jQuery that I'm notified everytime the page is refreshed. </p>

<pre><code>$(window).on('load', function(){
    alert("Reloaded!");
});
</code></pre>

<p>NOTE:  for the sake of entirity, my entire code is:</p>

<pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;title&gt;SomuFinance - Personal Finance Manager&lt;/title&gt;
    &lt;link rel="stylesheet" type="text/css" href="indexStyle.css"&gt;
    &lt;script src="scripts/jquery-3.1.0.min.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="scripts/jquery.validate.min.js"&gt;&lt;/script&gt;
    &lt;style type="text/css"&gt;
        #addItemContainer {
            background-color: rgba(204,207,232,1);
        }
    &lt;/style&gt;
    &lt;script type="text/javascript"&gt;
        var flag=0;
    &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="list" method="post" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;"&gt;
        &lt;div id="container"&gt;
            &lt;input type="submit" class="button" name="edit" id="edit" value="Edit" /&gt;
            &lt;input type="button" class="button" name="delete" value="Delete" /&gt;
            &lt;input type="hidden" id="action" name="action"&gt;
            &lt;input type="hidden" id="id" name="id"&gt;
            &lt;table id="listDB"&gt;
                &lt;tr&gt;
                    &lt;th&gt;Select&lt;/th&gt;
                    &lt;th&gt;ID&lt;/th&gt;
                    &lt;th&gt;Category ID&lt;/th&gt;
                    &lt;th&gt;Shop&lt;/th&gt;
                    &lt;th&gt;Item&lt;/th&gt;
                    &lt;th&gt;Quantity&lt;/th&gt;
                    &lt;th&gt;Unit&lt;/th&gt;
                    &lt;th&gt;Price Based On&lt;/th&gt;
                    &lt;th&gt;MRP&lt;/th&gt;
                    &lt;th&gt;Seller's Price&lt;/th&gt;
                    &lt;th&gt;Last Updated On&lt;/th&gt;
                &lt;/tr&gt;
                &lt;?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 '&lt;tr&gt;';
                            echo '&lt;td class="catHead" colspan=11&gt;'.$category.'&lt;/td&gt;';
                        echo '&lt;/tr&gt;';
                        $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 '&lt;tr&gt;';
                                echo '&lt;td&gt;&lt;input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /&gt;&lt;/td&gt;';
                                echo '&lt;td&gt;'.$id.'&lt;/td&gt;';
                                echo '&lt;td&gt;'.$catCount.'&lt;/td&gt;';
                                echo '&lt;td&gt;'.$shop.'&lt;/td&gt;';
                                echo '&lt;td class="leftAligned"&gt;'.$item.'&lt;/td&gt;';
                                echo '&lt;td&gt;'.$qnty.'&lt;/td&gt;';
                                echo '&lt;td&gt;'.$unit.'&lt;/td&gt;';
                                echo '&lt;td&gt;'.$price_based_on.'&lt;/td&gt;';
                                echo '&lt;td class="pri"&gt;₹'.$mrp.'&lt;/td&gt;';
                                echo '&lt;td class="pri"&gt;₹'.$sellers_price.'&lt;/td&gt;';
                                echo '&lt;td&gt;'.$last_updated_on.'&lt;/td&gt;';
                            echo '&lt;/tr&gt;';

                            $catCount++;
                        }
                    }
                ?&gt;
            &lt;/table&gt;
        &lt;/div&gt;

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

    &lt;div class="dialogBG"&gt;
        &lt;div id="addItemContainer" class="dialog"&gt;
            &lt;div class="closeDialog"&gt;&lt;/div&gt;
            &lt;h1&gt;Edit Item&lt;/h1&gt;
            &lt;form id="data" method="post" action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;"&gt;
                &lt;?php
                    if(isset($_POST['action']))
                    {
                        if($_POST['action']=='edit')
                        {
                            echo '&lt;script&gt;flag=1;&lt;/script&gt;';
                            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'];

                        }
                    }
                ?&gt;
                &lt;div class="leftAligned"&gt;
                    &lt;input type="hidden" id="id" name="id" value="&lt;?php echo $id; ?&gt;" required&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="shop"&gt;ID : &lt;/label&gt;
                        &lt;input type="text" id="id_disp" name="id_disp" value="&lt;?php echo $id; ?&gt;" required disabled&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="shop"&gt;Shop : &lt;/label&gt;
                        &lt;input type="text" id="shop" name="shop" value="&lt;?php echo $shop; ?&gt;" required&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="category"&gt;Category : &lt;/label&gt;
                        &lt;input type="text" id="category" name="category" value="&lt;?php echo $category; ?&gt;" required&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="item"&gt;Item : &lt;/label&gt;
                        &lt;input type="text" id="item" name="item" value="&lt;?php echo $item; ?&gt;" required&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="qnty"&gt;Quantity : &lt;/label&gt;
                        &lt;input type="text" id="qnty" name="qnty" value="&lt;?php echo $qnty; ?&gt;" required&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="unit"&gt;Unit : &lt;/label&gt;
                        &lt;input type="text" id="unit" name="unit" value="&lt;?php echo $unit; ?&gt;" required&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="price_based_on"&gt;Price based on : &lt;/label&gt;
                        &lt;select name="price_based_on" id="price_based_on"&gt;
                            &lt;option value="kilos"&gt;Kilos&lt;/option&gt;
                            &lt;option value="packet"&gt;Packet&lt;/option&gt;
                            &lt;option value="bottle"&gt;Bottle&lt;/option&gt;
                            &lt;option value="box"&gt;Box&lt;/option&gt;
                            &lt;option value="piece"&gt;Piece&lt;/option&gt;
                        &lt;/select&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="mrp"&gt;MRP (₹) : &lt;/label&gt;
                        &lt;input type="text" id="mrp" name="mrp" value="&lt;?php echo $mrp; ?&gt;" required&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="sellers_price"&gt;Seller's Price (₹) : &lt;/label&gt;
                        &lt;input type="text" id="sellers_price" value="&lt;?php echo $sellers_price; ?&gt;" name="sellers_price" required&gt;
                    &lt;/div&gt; &lt;br&gt;
                    &lt;div class="inp"&gt;
                        &lt;label for="last_updated_on"&gt;Last Updated on : &lt;/label&gt;
                        &lt;input type="date" id="last_updated_on" name="last_updated_on" value="&lt;?php echo $last_updated_on; ?&gt;" required&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
                &lt;div class="inp"&gt;
                    &lt;input id="insertButton" type="submit" name="submit" value="Update"&gt;
                &lt;/div&gt;
                &lt;div id="message"&gt;
                    &lt;?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)&amp;&amp;!empty($category)&amp;&amp;!empty($item)&amp;&amp;is_numeric($qnty)&amp;&amp;!empty($unit)&amp;&amp;is_numeric($mrp)&amp;&amp;is_numeric($sellers_price)&amp;&amp;!empty($last_updated_on))
                            {
                                $result = mysqli_query($dbc, $query)
                                                    or die(mysqli_error($dbc));
                            }
                            header("Refresh: 5; url=index.php");
                            echo "&lt;script&gt;alert('This works');&lt;/script&gt;";
                        }
                    ?&gt;
                    &lt;script&gt;
                    $(document).ready(function(){

                        $( "#data" ).validate({
                          rules: {
                            qnty: {
                              number: true
                            },
                            mrp: {
                              number: true
                            },
                            sellers_price: {
                              number: true
                            }
                          },
                          messages: {
                            qnty : {
                                number: '&lt;br&gt; &lt;span class="failure err"&gt;Enter a valid quantity&lt;/span&gt;'
                            },
                            mrp : {
                                number: '&lt;br&gt; &lt;span class="failure err"&gt;Enter a valid MRP&lt;/span&gt;'
                            },
                            sellers_price : {
                                number: '&lt;br&gt; &lt;span class="failure err"&gt;Enter a valid Price&lt;/span&gt;'
                            },
                          }
                        });
                    });
                    &lt;/script&gt;
                &lt;/div&gt;
            &lt;/form&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;script type="text/javascript"&gt;
        $(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');
            });
        });
    &lt;/script&gt;

    &lt;?php
        mysqli_close($dbc);
    ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><strong>NOTE</strong> : Somehow restarting the php server along with Praveen's answer solved the issue!</p>