0

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 enter image description here

Mona Coder
  • 6,212
  • 18
  • 66
  • 128

1 Answers1

1

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;
?>
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107