2013-03-26 126 views
0

好的,爲了讓一切都清楚我會盡我所能解釋一切,幷包含圖像。這可能會是一個相當長的問題,但我只想把一切都弄清楚。使用動態下拉列表從MySQL數據庫中加載數據到textarea

嗯,我curently有一個 「文本框」(TinyMCE的AJAX文件管理器),這是從一個文本文件顯示HTML這樣的:

enter image description here 這是代碼到該ATM:

Bericht:</td><td align="left"><textarea name="content" cols="50" rows="15"><?php echo "$show"?></textarea></td></tr> 

和:

<?php 
if (file_exists("prwyswig.txt")){ 
$show = file_get_contents('prwysiwyg.txt'); 
} 
else{ 
$show = file_get_contents('tabel.txt'); 
} 
?> 

我想要的是不再使用TEXTFILES但我想選擇哪個存儲在我的HTML代碼SQL數據庫。我做了一個動態的下拉列表,其中包括「通訊」的名稱。

所以我想要的是從下拉列表中選擇一個通訊,然後加載屬於該標題的html到textarea。我自己嘗試過幾件事,但我無法弄清楚如何管理這件事。

我是否必須寫另一個查詢? 我是否必須以另一種形式放置dorpdown列表才能使用提交按鈕加載textarea中的數據?

我會在下面發佈其餘代碼和我的數據庫表結構,因爲您可能需要它們^^如果您有任何其他問題,只需在評論中詢問他們,任何幫助都會很棒!

注: 我知道我不應該使用mysql_ *但在這裏不是問題。我稍後將改爲PDO!

連接到DB +查詢來選擇正確的數據:

<?php 
mysql_connect('localhost','root','root'); 
mysql_select_db('NAW') or die (mysql_error()); 
$strSQL  = "SELECT Content, Titel FROM NAW.Mail"; 
$sql_result  = mysql_query($strSQL); 
?> 

動態下拉列表:

<td valign=top>Nieuwsbrief:</td> 
<td> 
<?php 
echo "<select name=\"show\">"; 
echo "<option size =30 selected>Select</option>"; 
if(mysql_num_rows($sql_result)) 
{ 
while($row = mysql_fetch_assoc($sql_result)) 
{ 
echo "<option>$row[Titel]</option>"; 
} 

} 
else { 
echo "<option>No Names Present</option>"; 
} 
?> 

數據庫表:

ID Content     Datum  Titel 
1 (lots of encoded html) 18-03-13 test 
2 (lots of encoded html) 18-03-13 test2 
4 (lots of encoded html) 18-03-13 alles weer testen 
5 (lots of encoded html) 20-03-13 testje 
6 (lots of encoded html) 21-03-13 Statusupdate week 6 
+0

編碼和解碼HTML? – hjpotter92 2013-03-26 10:46:14

+0

[請不要在新代碼中使用'mysql_ *'函數](http://stackoverflow.com/q/12859942/1190388)。他們不再被維護,並[正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到紅色框?改爲了解準備好的語句,然後使用[tag:PDO]或[tag:MySQLi]。 – hjpotter92 2013-03-26 10:47:15

+0

ment編碼^^,我知道我不應該使用mysql_ *,看看我的筆記.. – Daanvn 2013-03-26 10:48:08

回答

1

你能做到如下,你的表單的HTML和PHP代碼就像這樣。

<?php 
mysql_connect('localhost','root','root'); 
mysql_select_db('NAW') or die (mysql_error()); 
$strSQL  = "SELECT Titel FROM NAW.Mail"; 
$sql_result  = mysql_query($strSQL); 
?> 


    <form id="myform"> 
     <td valign=top>Nieuwsbrief:</td> 
     <td> 
     <?php 
     echo "<select id=\"NieuwsbriefSelect\" name=\"show\">"; 
     echo "<option size =30 selected>Select</option>"; 
     if(mysql_num_rows($sql_result)) 
     { 
     while($row = mysql_fetch_assoc($sql_result)) 
     { 
     echo "<option value=\"$row[Titel]\">$row[Titel]</option>"; 
     } 

     } 
     else { 
     echo "<option>No Names Present</option>"; 
     } 
     ?> 

    Bericht:</td><td align="left"><textarea name="content" cols="50" rows="15"></textarea></td></tr> 

</form> 

使用jQuery添加一個onChange事件<Select>這樣

<script> 
// variable to hold request 
var request; 
$(document).ready(function() { 

$('#NieuwsbriefSelect').change(function() { 
    //send Ajax call to get new contents 
    var selected_text = $(this).val(); 
    // setup some local variables 
    var $form = $("#myform"); 
    // let's select and cache all the fields 
    var $inputs = $form.find("input, select, button, textarea"); 
    // serialize the data in the form 
    var serializedData = $form.serialize(); 

    // fire off the request to /get_my_contents.php 
    request = $.ajax({ 
    url: "/get_my_contents.php", 
    type: "post", 
    data: serializedData 
    }); 


    // callback handler that will be called on success 
    request.done(function (response, textStatus, jqXHR){ 
    //populate the TextArea 
    $("textarea[name='content']").html(response); 
    }); 


}); 

}); 
</script> 

最後你get_my_contents.php會是這樣

<?php 
$title = $_POST["show"]; 
mysql_connect('localhost','root','root'); 
mysql_select_db('NAW') or die (mysql_error()); 
$strSQL  = "SELECT Content from NAW.Mail where Titel = '$title' "; 
$sql_result  = mysql_query($strSQL); 

$row = mysql_fetch_assoc($sql_result) 
print $row["Content"]; 
?> 

希望它能夠解決您的問題。

編輯 你可以嘗試這個小提琴(只是客戶端代碼,阿賈克斯將無法正常工作) http://jsfiddle.net/jjWdF/

+0

Thnx,我會盡快嘗試,但我在一些分鐘,所以我可能會嘗試它tommorow! – Daanvn 2013-03-27 13:14:48

+0

我只是試圖使用這個,但它似乎並沒有工作。下拉列表仍然正常工作,但是當我選擇什麼都沒有發生時。它不會給出任何錯誤,它只是無所作爲。由於我不熟悉JQuery,因此我的工作至今沒有奏效。這可能是一個選項,你可以看看我的整個代碼? – Daanvn 2013-03-28 09:22:56

+0

我假設你已經在你的服務器上添加了文件'get_my_contents.php'。將警報置於'request.done'中,並檢查它是否被觸發。當然,我可以幫助你處理這部分代碼。 – 2013-03-28 09:28:03