简体   繁体   English

无法使用 javascript 将 textarea 的值传递给 PHP 脚本 ECHO

[英]unable to pass value of textarea using javascript to PHP script ECHO

I am trying to get the value of textarea using JavaScript to pass it to PHP without reloading the page.我正在尝试使用 JavaScript 获取textarea的值以将其传递给 PHP,而无需重新加载页面。

Please where might I have the issue here ?请问我这里哪里有问题?

This the Javascript to get the value from textarea and transfer it to PHP to be used这是用于从 textarea 获取值并将其传输到 PHP 以供使用的 Javascript

function getValue(s){    
    $.post(
        "reply.php", 
        {      
            getTxt: s,
        },    
        function(data,status){
             $("#ReplyTextField").html(data);
        }
    );
}

here is the HTML Textarea with reply button这是带有回复按钮的 HTML Textarea

<!-- reply popup -->
<div class="sitemodal fade" id="reply-popup">
    <div class="sitemodal-dialog">

      <!-- siteModal content-->
      <div class="sitemodal-content">
        <div class="sitemodal-header">
          <button type="button" class="close-popup">&times;</button>
          <h4 class="sitemodal-title">Reply</h4>
        </div>
        <div class="sitemodal-body">
          <form enctype="multipart/form-data" method="Post" action="<?php echo "reply.php?message=" . $row['id'] . "'"; ?>" name="msgform">
            <textarea id="ReplyTextField" placeholder="Give your Reply" name="textarea1"></textarea>
          </form>
        </div>
        <div class="sitemodal-footer">
          <button type="button" class="popup-btn reply" id="replyButton" name="sendmsg" onclick="getValue()">Reply</button>

        </div>
      </div>

    </div>
</div>

This is the php script in reply.php file i am trying to transfer the data to这是reply.php文件中的php脚本,我试图将数据传输到

<?php
/*include("functions.php");
include("session.php");
require("connection.php");*/

$getTxt= $_REQUEST['getTxt'];
echo $getTxt;

?>

Your PHP is fine.你的 PHP 没问题。 It is the HTML that is the issue.问题在于 HTML。 You were not sending the value which throws up an error in php.您没有发送在 php 中引发错误的值。 You have to send the variable in the onclick funtion and then it works.您必须在 onclick 函数中发送变量,然后它才能工作。

Here is the Updated HTML file:这是更新后的 HTML 文件:

 function getValue(s){ $.post("reply.php", {getTxt: s,}, function(data,status){ $("#ReplyTextField").html(data);}); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p></p><div class="sitemodal fade" id="reply-popup"> <div class="sitemodal-dialog"> <!-- siteModal content--> <div class="sitemodal-content"> <div class="sitemodal-header"> <button type="button" class="close-popup">&times;</button> <h4 class="sitemodal-title">Reply</h4> </div> <div class="sitemodal-body"> <form enctype="multipart/form-data" method="Post" action="<?php echo `reply.php?message=` . $row['id'] . `'`; ?>" name="msgform"> <textarea id="ReplyTextField" placeholder="Give your Reply" name="textarea1"></textarea> </form> </div> <div class="sitemodal-footer"> <button type="button" class="popup-btn reply" id="replyButton" name="sendmsg" onclick="getValue(document.getElementById('ReplyTextField').value)">Reply</button> </div> </div> </div> </div>

The issue is that you are not passing any parameter to the getValue() function.问题是您没有将任何参数传递给getValue()函数。 I would avoid using the HTML onclick attribute and use a jQuery event handler instead:我会避免使用 HTML onclick 属性,而是使用 jQuery 事件处理程序:

$('#replyButton').on('click', function() {
    var textareaValue = $('#ReplyTextField').val();
    getValue(textareaValue);
};

You should also use the .val() method to set the value of the textarea.您还应该使用.val()方法来设置 textarea 的值。 https://api.jquery.com/on/ https://api.jquery.com/val/ https://api.jquery.com/on/ https://api.jquery.com/val/

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

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