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

1 Answer 1

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;
?>
Sign up to request clarification or add additional context in comments.

9 Comments

Hi Tushar, thanks for your comment, well the first script didnt worked and I got this in teaxtarea when I used the second code: event.returnValue is deprecated. Please use the standard event.preventDefault() instead.
in the first method I am getting "Uncaught SyntaxError: Unexpected token ; " error on data:{name:$('input[type="name"]').val();},
@MonaCoder Use data:{name:$('input[type="name"]').val()} removed ; and read stackoverflow.com/questions/20045162/…
Ok thanks but it is returning bunch of tags insted of input! like : <br /> <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'> ....
Can you please take a look at update above? I just post the result image
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.