【问题标题】:JQuery: Populate textarea with ajax responseJQuery:使用 ajax 响应填充文本区域
【发布时间】:2013-12-23 10:59:27
【问题描述】:

我有一个动态创建的下拉列表,我可以在其中从我的 MySQL 数据库中选择新闻通讯。重点是当我选择时事通讯时,数据库中的内容会插入到文本区域中。

下拉列表是这样的:

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

所以它应该做的是将响应添加到文本区域中。然而问题是 ajax 请求似乎没有做任何事情。当我运行它并从下拉列表中选择一个时事通讯时,我将收到前 2 个警报,但之后它什么也不做。我还要提到我基本上没有使用 Jquery 的经验,但其他人建议我应该使用它并帮助我像当前状态一样获得它。如果有人能看到问题所在或提出另一种方法,那就太好了!

注意: 我知道我不应该使用 mysql_* 并且稍后我将更改为 PDO!

【问题讨论】:

    标签: jquery populate


    【解决方案1】:

    我不确定,如果这是一个问题,但我会像这样重写代码:

    request = $.ajax({ 
    url: "/get_my_contents.php", 
    type: "GET", 
    data: serializedData
    success: function(data)
        {
        //populate the TextArea 
        alert(data);
        $("textarea[name='content']").html(data); 
        }
    }); 
    

    【讨论】:

    • 如果我使用它,它甚至不会再显示其他警报了。
    • 你检查过,你从 MySQL 查询中得到了什么?
    • 是的,get_my_contents.php 上的 MySQL 查询工作正常,并且会得到正确的内容。
    • 试着写“exit;” "打印 urldecode($row["Content"]);" 之后
    【解决方案2】:
    var myTextareaVal = $('#message-textarea').val();
    
    $.ajax({                                       
          type: "GET",
          url: "myPhpFile.php",           
          data: "text=" + myTextareaVal,           
          cache: false,
          dataType: "html",           
          success: function(data) {   
                $('.modal_content').find('.message:last').before(data);
          }
    }); 
    

    【讨论】:

      【解决方案3】:

      'data' 可能有换行符

      $("textarea[name='content']").html(data); 
      

      =

      $("textarea[name='content']").html('line number1
      line number2
      
      line number3 with a previous blank line');
      

      而换行导致js出错

      你只需要修复换行符。

      【讨论】:

        猜你喜欢
        • 2012-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-06
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多