2016-12-16 87 views
1

我的巨大的代碼段是:頭刷新不工作

$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的答案一起解決這個問題!

+0

你應該試試這個[鏈接](http://stackoverflow.com/a/12383397/6840733) – asissuthar

+0

OPS對不起我的錯誤 – Beginner

+0

您好可以嘗試只'header(「location:index.php」);'看看它是否可以重定向 – Beginner

回答

1

只需添加ob_start();作爲<?php ob_start();之後的第一行。在您設置標題的相同文件中。它會工作。

這是因爲「標題已發送」。

你已經啓用PHP錯誤,而你開發它會幫助你。

頁面頂部使用它 ini_set('display_errors', '1');error_reporting(E_ALL);

+0

它的工作!這條線究竟做了什麼? –

+0

這段代碼會告訴你錯誤。然後我可以指導你。 –

+0

沒有更多的錯誤,感謝你!但ob_start()做什麼? –