繁体   English   中英

使用动态下拉列表从MySQL数据库在textarea中加载数据

[英]Loading data in textarea from MySQL Database using dynamic dropdownlist

好吧,为了使一切变得清晰,我将尽我所能地解释一切,并包括图像。 这样可能会是一个很长的问题,但是我只想澄清所有事情。

好吧,我现在有一个“文本框”(TinyMCE AJAX文件管理器),它显示的是文本文件中的html,如下所示:

在此处输入图片说明 这是此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');
}
?>

我想要的是不再使用文本文件,但我想选择存储在MySQL数据库中的html代码。 我做了一个动态的下拉列表,其中包含“新闻通讯”的名称。

所以我要从下拉列表中选择一个新闻通讯,然后将属于该标题的html加载到textarea中。 我自己尝试了几件事,但是我不知道该如何处理。

我是否必须编写另一个查询? 我是否必须以另一种形式放置dorpdown列表才能使用提交按钮将数据加载到textarea中?

我将在下面发布其余代码和我的数据库表结构,因为您可能需要它们^^如果您有任何其他问题,只需在注释中询问它们,任何帮助都将非常有用!

注意:我知道我不应该使用mysql_ *,但这不是这里的问题。 稍后我将更改为PDO!

连接到DB + query以选择正确的数据:

<?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

您可以按照以下步骤进行操作,表单的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这样为<Select>添加一个onChange事件

<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"];
?>

希望它能解决您的问题。

编辑您可以尝试这个小提琴(只是客户端代码,Ajax无法使用) http://jsfiddle.net/jjWdF/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM