好吧,为了让一切都清楚,我将尽可能清楚地解释一切并包括图像。这可能是一个很长的问题,但我只是想让一切都清楚。
嗯,我目前有一个“文本框”(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 加载到文本区域中。我自己尝试了几件事,但我就是不知道如何管理它。
我是否需要编写另一个查询? 我是否必须将 dorpdown 列表放入另一种表单中才能使用提交按钮加载文本区域中的数据?
我将在下面发布其余的代码和我的数据库表结构,因为您可能需要它们^^如果您有任何其他问题,请在评论中询问他们,任何帮助都会很棒!
注意: 我知道我不应该使用 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
最佳答案
您可以按如下方式进行操作,您的表单的 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"];
?>
希望它能解决您的问题。
编辑 你可以尝试这个 fiddle (只是客户端代码,Ajax 不起作用) http://jsfiddle.net/jjWdF/
关于php - 使用动态下拉列表从 MySQL 数据库加载文本区域中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15634939/