2012-07-01 19 views
2

PHP變量所以,我有一個有趣的問題jQuery的.load()打破加載到HTML的身體

我有jQuery的.load()函數的問題。我有一個文件結構如下

index.php 
| 
|---header.html 
|---body.html 
|---footer.html 

index.php文件設置了三個變量,所以$variable_one = new Database('passedQuery');

了header.html,body.html和footer.html然後被納入的index.php

在body.html我有它通過一個通用的jQuery函數提交的body.html類似

<div class="span4 well"> 
     <h3>Current Statistics</h3> 
     <hr> 
     <table class="table table-striped"> 
      <thead> 
       <tr> 
        <th>Users</th> 
        <th>Event One</th> 
        <th>Event Two</th> 
        <th>Event Three</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td><?php echo $var1->result[0][0]; ?></td> 
        <td><?php echo $var2->result[0][0]; ?></td> 
        <td><?php echo $var3->result[0][0]; ?></td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 

東西,我也有一個形式,所以

$(document).ready(function(){ 
    $('.btn').bind('click', function(e){ 
     e.preventDefault(); 
     var form = $(this).parents('form'); 
     var vals = $(form).serialize(); 
     var form_id = $(form).attr('id'); 
     $.ajax({ 
      url: $(form).attr('action'), 
      type: $(form).attr('method'), 
      data: vals, 
      dataType: 'json', 
      success: function(data){ 
       console.log("Success"); 
       $('#information').html('<div class="alert alert-success">Great Success! :)</div>'); 
       setTimeout(function() { 
        $('#content').load('views/partials/body.html'); 
       }, 5000); 
      }, 
      error: function(a, b, c){ 
       console.log("Error"); 
       $('#information').html('<div class="alert alert-error">Epic Failure</div>'); 
       setTimeout(function() { 
        $('#content').load('views/partials/body.html'); 
       }, 5000); 
      } 
     }); 
    }); 
}); 

當我提交表單,並重新加載的身體現在在HTML中回顯的PHP變量都走了,我也得到類似以下

[dateTime] [error] [client IP] PHP Notice: Undefined variable: variable in /path/to/body.html on line 133, referer: myReferrer 

我問了一個Apache錯誤很少有我的朋友對此感到難過,就像Freenode的#jquery一樣。有人有主意嗎?

更新 - 表單代碼和index.php文件CODE 形式

<div class="span4 well"> 
     <form class="form-horizontal" id="admin_add" name="admin_add" method="post" action="routes/to/add_admin.php"> 
      <fieldset> 
       <legend>Add Administrator</legend> 
       <div class="control-group"> 
        <label class="control-label" for="select03">Select a user</label> 
        <div class="controls"> 
         <select id="select03" name="user"> 
          <?php 
           foreach($var1->result as $key => $value) 
            echo "<option>".$value[0]."</option>"; 
          ?> 
         </select> 
        </div> 
       </div> 
       <div class="form-actions"> 
        <button type="submit" class="btn btn-primary form-submit-button">Submit</button> 
       </div> 
      </fieldset> 
     </form> 
    </div> 

指數

$var2 = new Database('query', 'SELECT COUNT(uid) FROM attendees WHERE eid = 1'); 
$var3 = new Database('query', 'SELECT COUNT(uid) FROM attendees WHERE eid = 2'); 

/** 
* List users to add to database 
*/ 
$var1 = new Database('query', 'SELECT name FROM users WHERE admin=0'); 

/** 
* Include all the visuals 
*/ 
include('views/partials/header.html'); 
require_once('views/partials/body.html'); 
include('views/partials/footer.html'); 
+0

你可以發表你的表格代碼以及 – Kishore

+0

什麼是行133在body.html? –

+0

@DaveM它只是指我在哪裏回顯一個變量,您可以在我的更新示例中看到 – samdunne

回答

3

當你第一次加載body.html時,我猜你正在用php的include()函數加載它。那麼,這是「連接」的PHP所以在服務器端它就像它的一個長的PHP文件。這適用於你的變量,因爲它好像代碼沒有中斷。

當你使用jQuery的load()功能,這發生在客戶端。發生的事情是,你得到JUST body.html的html輸出,並且你動態地(在客戶端)將該內容放入客戶頁面。

您遇到的問題是PHP不再連接在一起。 load()以其獨特性取body.html並將其放入頁面。

另一種看待它的方法是,當有人去index.php解析php時,所有echo'd變量都變成bog標準文本/ html併發送到瀏覽器。一旦他們不再是PHP變量,他們只是文本/ HTML。通過javascript向body.html發出新請求意味着您所引用的變量不再可用。他們已經被轉換爲HTML併發送到瀏覽器,然後PHP停止,這是一個新的請求。

我不知道你要如何解決這個說實話。您可能想要像smarty一樣查看模板系統。或者你可能想把你的函數放到一個名爲functions.php的文件中,將它包含到你的body.html中,然後在你的body.html中引用這些函數,而不是依賴索引頁中的代碼。

即你body.html看起來就像這樣:

<?php include "functions.php"; ?> 
<div ...> 
<?php 
    $var1 = getVar1(); // function in functions.php 
    echo $var1->result[0][0]; 
    // ... etc 
?> 
</div> 

然後,當它被從JavaScript調用,然後將其發送給瀏覽器,將被評估的PHP這樣就可以了。

希望我解釋過這個好人。 :)

+0

試圖實現似乎不適用於我。我遇到了與我的數據庫類路徑有關的問題。我想知道我是否接受了你的建議,將它們命名爲* .tpl.php,然後在文件上做一個require_once(),這將停止文件的大規模聚合,並且仍然達到相同的結果? – samdunne

+0

我認爲你需要重新考慮你的應用程序的整個結構。例如你的情況下,你有index.php,它有(例如)文本文本<?php include「body.html」; ?> text text text「right?if you go to index.php you see」text text text content of body.html text text text。「如果你去body.html,你只會看到」body.html的內容「。因此,如果body.html的內容依賴來自index.php的信息,那麼調用body.html將會失敗(以您找到的方式)。 –

+0

所以,這種方法不能依賴DOWN的層次結構文件。即body.html不應該依賴於從另一個文件中調用。body.html應該調用OUT來獲取它所需要的信息。這就是函數.php文件的想法。如果你需要某人的用戶名在body.html中,把'function getUsername(){return「Thomas Clayson」;}'放在functions.php中,然後你需要從body.html'include「functions.php」'中引用OUT到functions.php。進一步在body.html中,你可以調用'echo getUsername()'。 –

0

當您的jQuery請求獲取到服務器時,它會爲您服務回body.html沒有任何PHP處理,因此您在index.php中設置的變量不存在。

相關問題