2013-03-26 121 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