将PHP变量添加到JQuery Post传递参数

时间:2013-10-14 12:32:26

标签: javascript php jquery

我有一个Jquery帖子,我想传递一些我在php中的变量值。

这是jquery:

<script type="text/javascript">
      $("#button1").submit(function(event) {
         $.post('file.php?do=getLastFile',null,function(attachment){
           //
        },'json');

        });

  </script> 

我有一个php变量... myPhpVariable我想添加到:

file.php?do=getLastFile

这样的事情:

file.php?do=getLastFile&somevar=$myPhpVariable  

我该怎么办?

2 个答案:

答案 0 :(得分:2)

首先将您的php变量分配给JS,然后使用它。

   var phpvar = '<?php echo $myPhpVariable;?>'; //use it like this
   $("#button1").submit(function(event) {
     $.post('file.php?do=getLastFile&somevar='+phpvar,null,function(attachment){
       //
   },'json');

   });

答案 1 :(得分:0)

Value.php

<?php
   $value = 1;

?>


//This would be the page that would use the value you are trying to pass into jquery
<?php

    include('Value.php');


?>


Usevalue.php page
//Then in your javascript you could collect the variable in the Value.php page
<script>
var value = "<?php echo $value; ?>";

$(document).ready(function (){

   //then you can call the value in your jquery here  
});
</script>