Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I read this post and assumed the technique in the answer would work with ajax calls. I have my ajax and php code below but it does not work.The client does not recognize the 'passed' variable. I do not know why nor how to remedy this.

Javascript

var irrelevant = 'irrelevant';

   $('body').click(function(){


            $.ajax({
            type: 'POST',
            url: 'test.php',
            data: {mydata: irrelevant},    
            success: function(){

            console.log('worky');

            alert(myvar); // NOT worky!

                    }

            });

    });

PHP File

<?php


$thing = 10;


?>


<script>

var myvar = "<?php echo $thing; ?>";

</script>
share|improve this question
1  
there is no accepted answer in the question you mention. –  Michał Rybak Oct 28 '13 at 10:28
 
Sorry I was reading the comments and didn't look for the green check –  TaoistWA Oct 28 '13 at 10:29
add comment

2 Answers

up vote 2 down vote accepted

try this in your ajax.success

success: function(data){
   console.log('worky');
   alert(data); // It should now, worky!
}

and in you php

<?php

   echo 10;

?>
share|improve this answer
add comment

try this in php

<?php  $thing = 10; ?>


<script>

var myvar = "<?php echo $thing; ?>";

</script>

javascript

$('body').click(function(){

            $.ajax({
            type: 'POST',
            url: 'test.php',
            data: {mydata: irrelevant},    
            success: function(data){
                $("#hiddendiv").html(data);
                alert(myvar);
            }
       });  
});
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.