2012-12-21 229 views
0

我有phpbb論壇,我想在註冊頁面添加一個字段...修改是爲了讓用戶選擇他們的角色..例如成爲一名主持人..或者成爲普通會員或..我實際上使論壇有點私密,我正在將用戶分組,以便他們有權限訪問某些論壇。反正我有HTML模板文件,我想從一個PHP文件中的代碼PHP代碼在HTML頁面

在這裏得到下拉菜單列表是我的PHP

<?php 
include '/blah/config.php'; 
$con = mysql_connect($dbhost, $dbuser, $dbpasswd); 
if(!$con) 
{ 
    die("Couldn't Connect: ".mysql.error()); 
} 

mysql_select_db($dbname, $con); 
$result = mysql_query("SELECT group_name FROM phpbb_groups"); 
$array = array(); 
while($row = mysql_fetch_array($result)) 
{ 
    $array[] = $row[0]; 
} 

mysql_close($con); 

foreach($array as $group_name) 
{ 
    echo $group_name."</br>"; // I want to put this value in the dropdown list in the html file 
} 
?> 

這裏的代碼是我想要的HTML代碼的一部分編輯

<dt><label>GroupID:</label></dt> 
    <dd><select name="group_id" id="group_id"" tabindex="7" class="autowidth">I want to get the data from php in here</select></dd> 

PS:模板文件是HTML文件,不能重命名爲PHP的

+3

我會告訴你自從加入SO後我就被告知了什麼。 '停止使用mysql_'並遷移到PDO這裏是一個很好的tut:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – samayo

+1

爲了更清楚起見,'mysql_ *'函數將在PHP 5.5中被棄用。不建議編寫新代碼,因爲它將來會被刪除。取而代之的是,無論是[庫MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php)和[是一個更好的PHP開發人員](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –

+1

[**請不要在新代碼**中使用'mysql_ *'函數](http://bit.ly/phpmsql)。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – Neal

回答

0

好吧,抱歉,最後的答案,didnt看了你的PS有...

下面是一個jQuery解決方案,使用JSON作爲PHP文件的輸出,然後是填充下拉列表的異步請求。

PHP代碼

// The rest of your code, then output your $array this way 
header("Content-type: application/json"); 
$array = array("banana", "apple", "eggroll"); 
echo json_encode($array); 
die; 

你的HTML(例如)

請確保您添加的jQuery到你的頭上,如果你不已經擁有了它。

<dt><label>GroupID:</label></dt> 
<dd><select name="group_id" id="group_id"" tabindex="7" class="autowidth"></select></dd> 
<script> 
    $(document).ready(function() { 
     $.get('sefesf.php', {"q":"1"}, function(data) { 
      if(data.length > 0) 
      { 
       for(var i = 0; i < data.length; i ++) 
       { 
        $('select#group_id').append('<option>' + data[i] + '</option>'); 
       } 
      } 
     }); 
    }); 
</script> 

一定要改變sefesf.php到PHP文件中使用JSON輸出{「Q」:「1」} {}個,如果你不需要任何發送GET參數。

+0

一切都很好,但你的意思是你的頭添加jquery ..我很抱歉,我沒有使用jQuery之前.. –