JQuery:用ajax响应填充textarea
我有一个动态创建的下拉列表,可以从MySQL数据库中选择新闻稿。注意,当我选择一份时事通讯时,数据库中的内容会被插入一个文本区域 下拉列表如下所示:JQuery:用ajax响应填充textarea,jquery,populate,Jquery,Populate,我有一个动态创建的下拉列表,可以从MySQL数据库中选择新闻稿。注意,当我选择一份时事通讯时,数据库中的内容会被插入一个文本区域 下拉列表如下所示: <?php echo "<select id=\"NieuwsbriefSelect\" name=\"show\">"; echo "<option size =30 selected>Select</option>"; if(mysql_num_rows($sql_result
<?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>";
}
?>
jquery是这样的:
<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();
alert(serializedData);
// fire off the request to /get_my_contents.php
request = $.ajax({
url: "/get_my_contents.php",
type: "GET",
data: serializedData
});
alert("ajax call done");
// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
//populate the TextArea
alert(response);
$("textarea[name='content']").html(response);
});
});
});
</script>
get_my_contents.php:
<?php
$title = $_REQUEST["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 urldecode($row["Content"]);
?>
因此,它应该做的是将响应添加到textarea中。然而,问题是ajax请求似乎没有做任何事情。当我运行此功能并从下拉列表中选择一个时事通讯时,我将收到前两个警报,但在这之后,它什么也不做。我还将提到,我基本上没有使用Jquery的经验,但有人建议我应该使用它,并帮助我像当前状态一样使用它。如果有人能看到什么是错误的或提出另一种方式,那将是伟大的
注:
我知道我不应该使用mysql,我以后会改用PDO 我不确定这是否是一个问题,但我会像这样重写代码:
request = $.ajax({
url: "/get_my_contents.php",
type: "GET",
data: serializedData
success: function(data)
{
//populate the TextArea
alert(data);
$("textarea[name='content']").html(data);
}
});
“数据”可能有换行符
$("textarea[name='content']").html(data);
=
换行符会导致js中出现错误
您只需修复换行符。如果我使用此选项,它甚至不会再显示其他警报。您是否检查过,您从MySQL查询中得到了什么?是的,get_my_contents.php上的MySQL查询工作正常,将获得正确的内容。请尝试编写退出;打印后urldecode$行[内容];
$("textarea[name='content']").html(data);
$("textarea[name='content']").html('line number1
line number2
line number3 with a previous blank line');