2014-04-24 101 views
1

我使用的角度與MySQL通過PHP。當我嘗試添加到數據庫時,它會添加,但我必須單擊瀏覽器上的刷新才能看到結果。爲什麼我的數據庫更新沒有刷新沒有加載

我覺得這是角度的真正好處,但我對角度和MySQL也很新,所以我真的不知道我要去哪裏錯誤或如何使它正常工作。這似乎是我所有的數據庫調用。

只是爲了澄清,我在上面的頁面上使用ng-include來引導bootstrap,angular和jquery。

感謝任何人誰可以指出我在正確的方向。

-html

<div class="widget-box" id="recent-box" ng-controller="ideasController"> 
    <div class="widget-header header-color-blue"> 
     <div class="row"> 
      <div class="col-sm-6"> 
       <h4 class="bigger lighter"><i class="glyphicon glyphicon-align-justify"></i>&nbsp;Idea MANAGER</h4> 
      </div> 
     </div> 
    </div> 
    <div class="widget-body "> 
     <form id="newIdeaForm" class="add-idea"> 
     <div class="form-actions"> 
     <div class="input-group"> 
      <input type="text" class="form-control" name="comment" ng-model="ideaInput" placeholder="Add New Idea" > 
     <div class="input-group-btn"> 
      <button class="btn btn-default" type="submit" ng-click="addIdea(ideaInput)"><i class="glyphicon glyphicon-plus"></i>&nbsp;Add New Idea</button> 
     </div> 
     </div> 
     </div> 
     </form> 


     <div class="idea"> 
      <div style="width: 80%; margin-left:auto; margin-right:auto;"> 
       <table class="table table-hover"> 
        <tbody> 
         <tr ng-repeat="idea in ideas | orderBy:'idea'"> 
          <td>{{idea.IDEA}} <a ng-click="deleteIdea(idea.ID)" style="float:right;"><button class="btn btn">Delete</button></a></td> 
         </tr> 
        </tbody> 
       </table> 
      </div> 
     </div> 
    </div> 
</div> 

-php

//addIdea.php 

    <?php 
    require_once 'db.php'; // The mysql database connection script 
    if(isset($_GET['idea'])){ 
    $task = $_GET['idea']; 
    $status = "0"; 
    $created = time(); 
    $query=mysql_query("INSERT INTO ideas(idea,status,created_at) VALUES ('$idea', '$status', '$created')") or die(mysql_error()); 
    } 
    ?> 

//getIdea.php 

    <?php 
    require_once 'db.php'; // The mysql database connection script 
    $status = '%'; 
    if(isset($_GET['status'])){ 
    $status = $_GET['status']; 
    } 
    $query=mysql_query("select ID, IDEA, STATUS from ideas where status like '$status' order by status,id desc") or die(mysql_error()); 

    # Collect the results 
    while($obj = mysql_fetch_object($query)) { 
     $arr[] = $obj; 
    } 

    # JSON-encode the response 
    echo $json_response = json_encode($arr); 
    ?> 

-js

//Define an angular module for our app 
var app = angular.module('myApp', []); 

app.controller('ideasController', function($scope, $http) { 
    getIdea(); // Load all available ideas 
    function getIdea(){ 
    $http.get("ajax/getIdea.php").success(function(data){ 
     $scope.ideas = data; 
     }); 
    }; 
    $scope.addIdea = function (idea) { 
    $http.get("ajax/addIdea.php?idea="+idea).success(function(data){ 
     getIdea(); 
     $scope.ideaInput = ""; 
     }); 
    }; 

回答

0

您需要刷新頁面或者頁面的部分拉離新信息數據庫。您可以使用header通過您的addIdea.php重新加載頁面。

header('Location: thispage.html'); 

您可以重新載入繼INSERT,新數據將在表的頁面。

<?php 
    require_once 'db.php'; // The mysql database connection script 
    if(isset($_GET['idea'])){ 
    $task = $_GET['idea']; 
    $status = "0"; 
    $created = time(); 
    $query=mysql_query("INSERT INTO ideas(idea,status,created_at) VALUES ('$idea', '$status', '$created')") or die(mysql_error()); 
    } 
     header('Location: idea.html'); 
    ?> 
+0

$查詢=請求mysql_query( 「INSERT INTO思想(觀念,地位,created_at)VALUES( '$理念', '$狀態', '$創建')」)或死亡(mysql_error()); header('location:idea.html'); –

+0

我添加了它,但它似乎沒有刷新,我仍然需要在瀏覽器中手動點擊刷新。 –

+0

@MVC_Future_Guru我的php並不是非常尖銳的,但是您可能需要使用單引號而不是雙引號。 – sealz