简体   繁体   中英

Update textarea From User Input by jQuery and Ajax

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 is as simple as:

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

and here is the 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
            }
        });
});
});

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

Update 在此处输入图片说明

Send data like this

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

you js file becomes

 $.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
     }
 });


or

 $(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 } }); }); }); 


To get value in PHP

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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