3

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>
4

2 に答える 2

4

あなたの中でこれを試してくださいajax.success

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

そしてあなたのphpで

<?php

   echo 10;

?>
于 2013-10-28T10:28:37.010 に答える
0

これを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);
            }
       });  
});
于 2013-10-28T10:29:03.490 に答える