0

test.php

<script src="https://code.jquery.com/jquery-1.11.1.js"></script>  
<script>
function testing(){
 $.ajax({
                        url: 'test.php',
                        type: 'GET',
                         data: { va: "answer" },
                         success: function(data) {
                             // do something;
alert("answer")  ;                       }
                     });



}
</script>

<textarea><?php 
$compare= $_GET['va'];
echo $compare; ?>
</textarea>  

<button onclick="testing();">click</button>     

Am getting an alert message when I click the button but am getting nothing into the textarea.unable to get the javascript variable into php code. What's wrong here?

vidhya
  • 449
  • 7
  • 22
  • It's working but you are not refreshing the page. Textarea it's rendendered at time 0. Then you have Ajax call at time 1. Textarea it's already rendered so it's not refreshed. – MrPk Nov 28 '14 at 13:15
  • http://stackoverflow.com/questions/9150977/load-text-into-textarea-via-ajax-call – N4pst3r Nov 28 '14 at 13:17

2 Answers2

2

AJAX and PHP are different.

PHP is server based and AJAX is browser based.

You are getting data in:

You need to update this data with javascript itself.

As page is not refreshing PHP, will not update here.

success: function(data) {
  $("textarea").val(data); // You need update the value with Javacript.
}
Pupil
  • 23,834
  • 6
  • 44
  • 66
1

You never update the textarea after doing the ajax call. You need to add id attribute to the textarea so it's clear which textarea you're referring to, let's say the id is answer

<textarea id="answer"><?php 
$compare= $_GET['va'];
echo $compare; ?>
</textarea> 

then set the value of the textarea in the success function using $('#answer').val(data)

function testing(){
    $.ajax({
            url: 'test.php',
            type: 'GET',
            data: { va: "answer" },
            success: function(data) {
                         // do something;
                         alert("answer");
                         $('#answer').val(data);
                      }
    });
}
ekad
  • 14,436
  • 26
  • 44
  • 46