Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 传递文本区域的值_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 传递文本区域的值

Javascript 传递文本区域的值,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我相信这很简单,但对AJAX来说是新的,所以可以使用指针。我看到其他人也有这个问题,但找不到明确的答案 我有一个带有文本区域的表单 <form name='comment_form' method="post"> <textarea name='comment' id="comment" value="" cols="100" rows="3"/></textarea> <input type="button" value=

我相信这很简单,但对AJAX来说是新的,所以可以使用指针。我看到其他人也有这个问题,但找不到明确的答案

我有一个带有文本区域的表单

    <form name='comment_form' method="post">
     <textarea  name='comment' id="comment" value="" cols="100" rows="3"/></textarea>
     <input type="button" value="Send" onClick="process_comment();">
    </form>
然后将其与ajaxrequest一起传递。发送方式如下:

    comment="+encodeURIComponent(comment)
作为字符串的一部分,这适用于表单上的文本框,但文本区域的值为:

   [object HTMLTextAreaElement]
我确信这包含文本区域的值,但我不知道如何显示它。我正在使用PHP:

    $comment = $_POST['comment'];
任何指点都很好

谢谢 好的,所有这些都适用于文本框,我使用相同的方法设置值,例如:

    var comment=document.getElementById("comment").value
ajax发送是:

      ajaxRequest.send("first_name="+encodeURIComponent(first_name)+"&last_name="+encodeURIComponent(last_name)+"&shipping_company="+encodeURIComponent(shipping_company)+"&shipping_address_1="+encodeURIComponent(shipping_address_1)+"&shipping_address_2="+encodeURIComponent(shipping_address_2)+"&shipping_city="+encodeURIComponent(shipping_city)+"&shipping_postcode="+encodeURIComponent(shipping_postcode)+"&shipping_zone="+encodeURIComponent(shipping_zone)+"&shipping_country="+encodeURIComponent(shipping_country)+"&email="+encodeURIComponent(email)+"&order_id="+encodeURIComponent(order_id)+"&order_status_id="+encodeURIComponent(order_status_id)+"&notify="+encodeURIComponent(notify)+"&comment="+encodeURIComponent(comment)+"&date_added="+encodeURIComponent(date_added));
所有其他值都会通过post变量,但文本区域只会显示:

    [object HTMLTextAreaElement]

您可以使用jQuery的ajax方法

$.ajax({
                         type: 'POST',//GET
                         url: "abc.do",
                         data : {"comments":$("#comments").val(),
                         dataType: 'json',
                         cache: true,
                         context:this.id,
                         success: function(data){},
                        error: function() {},
                        beforeSend: function() {},
                        complete: function() {
                        }
                    });

这不是对他的问题的回答。此外-如果要使用此选项,您需要获取
#comments
的值,而不仅仅是传递对象,否则您会遇到同样的问题。不过,这并不是OP问题的解决方案,而是一种替代方案。尝试并修复实际问题。您可以发布javascript代码吗?您的封装在这里是错误的
comment=“+encodeURIComponent(comment)
-我认为这不会导致您遇到的错误,但应该是
comment=encodeURIComponent(comment);
更改此尝试
var comment=encodeURIComponent(document.getElementById(“comment”).value);
然后在ajaxI中传递'comment=comment',这可能是由于您在问题中提供的代码中没有出现某些错误造成的。
$.ajax({
                         type: 'POST',//GET
                         url: "abc.do",
                         data : {"comments":$("#comments").val(),
                         dataType: 'json',
                         cache: true,
                         context:this.id,
                         success: function(data){},
                        error: function() {},
                        beforeSend: function() {},
                        complete: function() {
                        }
                    });