通过jQuery和Ajax从用户输入更新textarea [英] Update textarea From User Input by jQuery and Ajax

查看:626
本文介绍了通过jQuery和Ajax从用户输入更新textarea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新php文件中的值,并使用jquery和ajax将其显示在textarea中.简而言之,我有:
1-具有用户输入和提交按钮以及文本区域的表单
2-一个名为data.php
的PHP文件 3-和html文件
这是我的密码

I am trying to update a value in my php file and present it in a textarea using jquery and ajax. to make it short, I have:
1- A Form with User Input and Submit button and a Text area
2- A PHP file called data.php
3 -and the html file
Here is my codes

<form>
<input type="text" name="name"><br>
<textarea name="wstxt" id="wstxt" cols="40" rows="5"></textarea>
<input type="button" name="txta-update" id="txta-update" value="Update Textarea"  />
</form>

PHP非常简单:

<?php
$name = "Jordano";
echo $name;

这是jQuery

$(document).ready(function() {
   $('#txta-update').click(function() {

          $.ajax({ 
              type: "GET", 
              url: "data.php",//get response from this file
              success: function(response){ 

               $("textarea#wstxt").val(response);//send response to textarea
            }
        });
});
});

您能告诉我如何将输入值发送到php并从新值更新textarea吗? 谢谢

can you please tell me how I can send the input value to php and update the textarea from new value? Thanks

更新

推荐答案

像这样发送数据

data:{name:$('input[name="name"]').val()}

您的js文件变为

 $.ajax({
     type: "GET",
     url: "data.php", //get response from this file
     data:{name:$('input[name="name"]').val()},
     success: function (response) {

         $("textarea#wstxt").val(response); //send response to textarea
     }
 });


$(document).ready(function () {
    $('#txta-update').click(function () {
        var name_val = $('input[name="name"]').val();
        $.ajax({
            type: "GET",
            url: "data.php", //get response from this file
            data: {
                name: name_val
            },
            success: function (response) {

                $("textarea#wstxt").val(response); //send response to textarea
            }
        });
    });
});


在PHP中获得价值


To get value in PHP

<?php
$name = $_GET['name'];
echo $name;
?>

这篇关于通过jQuery和Ajax从用户输入更新textarea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆