2016-12-01 32 views
-1

我試圖設置它,所以我可以包括我的一個PHP文件基於下拉選擇。我在這裏找到另一個答案,似乎這樣做,但當我嘗試它時,我得到一個錯誤「未定義函數」。包含基於下拉選擇的PHP文件

原來答案就在這裏Original

這裏是我的代碼:

<?php 


$includes=array(
     'phpfile1'=>'phpfile1.php', 
     'phpfile2'=>'phpfile2.php' 
     ); 

if(isset($_POST['cmtype']) && array_key_exists($_POST['cmtype'], $includes)) : 
    get_template_part($includes[$_POST['cmtype']]); 
endif; 
?> 


<form id="selection_form" action="" method="post"> 
<select name="cmtype" id="cmtype"> 
<option value="default"> </option> 
<option value="phpfile1">PHP file 1</option> 
<option value="phpfile2">PHP File 2</option> 
</select> 
<input type="submit" name="select_mode_submit" value="Get File"> 
</form> 

當我選擇的選項,並按下按鈕,我得到的錯誤「get_template_part」。我認爲它與IF語句中的函數有關,但我不知道如何解決它。

+1

get_template_part()定義在哪裏? – markdwhite

+2

完整的錯誤將是有用的 –

+0

你甚至有一個文件,該函數'get_template_part'? –

回答

0
`get_template_part() 

是WordPress內置函數,並不是PHP內置函數。您的WordPress安裝可能存在問題。

0

我沒有意識到「get_template_part」從其他答案是特定於wordpress,我沒有使用。所以相反,我試圖改變它包括(),而不是按照巴特伯格曼建議,並且似乎工作正常。

謝謝大家!

0

很明顯,您將根據您所做的選擇選擇文件名。但是你應該清楚在你做出選擇之後你會做什麼。在你的代碼中你已經使用了get_template_part()函數,但是沒有像這樣的內置函數。如果你在wordpress中看到過,你應該知道函數是在某個地方定義的。所以你應該嘗試在你的代碼中定義。

0

只需使用include語句即可。

<?php 
$includes = array(
    'foo'=>'/tmp/foo.php', 
    'bar'=>'/tmp/bar.php' 
); 

if(
    $_SERVER['REQUEST_METHOD'] === 'POST' 
    && isset($_POST['cmtype']) 
    && array_key_exists($_POST['cmtype'], $includes) 
) { 
    include $includes[$_POST['cmtype']]; 
} 
?> 
<form action="" method="post"> 
    <select name="cmtype"> 
     <option value="foo">Foo</option> 
     <option value="bar">Bar</option> 
    </select> 
    <input type="submit" value="Include file"> 
</form>