2016-05-16 30 views
-1

我有我的$dbConnection變量設置在頁面的最頂部。並且我在同一頁面上有聯繫表單。聯繫表格工作正常。未定義的變量,實際定義的

但是,提交時,它給了我一個未定義的錯誤;這很奇怪,因爲我199%確定我有正確設置的變量。

表單正在經歷一個腳本。

function ubbreplace($text){ 
    if (strpos($text, "[contact-form]") !== false) { 
     ob_start(); 
     include("contactform.php"); 
     $replace = ob_get_contents(); 
     ob_end_clean(); 
     $text = str_replace("[contact-form]", $replace, $text); 
    } 
    return $text; 
} 

我的猜測是這個腳本阻止了連接的連接。那可能嗎?

我已經定義了$dbConnection作爲一個全局變量,把這些``添加到SQL等等等等都沒有成功。錯誤消失,而$dbConnection被定義爲全局數據,但不會將數據放入數據庫。

+0

哪裏是定義爲一個全球性的'$ dbConnection'內;你的'ubbreplace()'函數在哪裏告訴PHP它應該訪問全局'$ dbConnection'? –

+0

@MarkBaker我之前刪除了這些,因爲我認爲它不會起作用。 –

+0

如果您的函數中沒有'global $ dbconnection',那麼PHP不會將其識別爲函數中的現有值,因爲它不在函數範圍 –

回答

0

新增global $dbConnection;ob_start()

<?php 
function ubbreplace($text){ 
    if (strpos($text, "[contact-form]") !== false) { 
     ob_start(); 
     global $dbConnection; // <-- added 
     include("contactform.php"); 
     $replace = ob_get_contents(); 
     ob_end_clean(); 
     $text = str_replace("[contact-form]", $replace, $text); 
    } 
    return $text; 
} 
?> 
0

儘量讓你的世界你$dbConnection函數內部

function ubbreplace($text){ 
    global $dbConnection; 
    if (strpos($text, "[contact-form]") !== false) { 
     ob_start(); 
     include("contactform.php"); 
     $replace = ob_get_contents(); 
     ob_end_clean(); 
     $text = str_replace("[contact-form]", $replace, $text); 
    } 
    return $text; 
}