简体   繁体   English

如何发布和获取与jQuery的PHP变种?

[英]how to post and get php vars with jquery?

i have a few php vars: 我有几个php vars:

$test = 12345;
$test1 = "test1";
$test2 = "test2";

and i have a jquery function: 我有一个jQuery函数:

onlike:function(response){
    $('input#helpdiv').trigger('click');    
}

what i want is to pass those php vars through a jquery post to another test.php file. 我想要的是通过一个jQuery帖子将那些php vars传递给另一个test.php文件。

I was thinking on something like this: 我在想这样的事情:

var test = <?php echo $test; ?>; 
var test1 = <?php echo $test1; ?>; 
var test2 = <?php echo $test2; ?>; 

onlike:function(response){
    $('input#helpdiv').trigger('click');
    $.post("test.php", { test, test1, test2 } );
}

and then how do i get them in the test.php ? 然后如何在test.php获取它们? just like $_GET["test"], ... 就像$_GET["test"], ...

any ideas on how to put this together? 关于如何将它们组合在一起的任何想法?

thanks 谢谢

<script language='javascript'> 
 var test = "<?php echo $test; ?>"; 
  var test1 = "<?php echo $test1; ?>"; 
  var test2 = "<?php echo $test2; ?>"; 

  onlike:function(response){
      $('input#helpdiv').trigger('click');
      $.post("test.php", { test:test, test1:test1, test2:test2  } );
        }
 </script>

in test.php you can access them as $_POST['test'],$_POST['test1'],$_POST['test2'] 在test.php中,您可以通过$ _POST ['test'],$ _ POST ['test1'],$ _ POST ['test2']访问它们

EDIT: 编辑:

to avoid problems caused by quotes in between the variable values, as explained in: how to post and get php vars with jquery? 为了避免由变量值之间的引号引起的问题,如中所述: 如何使用jquery发布和获取php vars?

  var test = "<?php echo json_encode($test); ?>"; 

and the values may be accesed in test.php as 并且这些值可以在test.php中作为

 $test1 = json_decode($_POST['test'])
var test = '<?php echo $test; ?>';

You need to put quotes around a value in JS 您需要在JS中的值周围加上引号

Also do you have those variables available when page loads ? 页面加载时还具有那些可用变量吗? As javascript will be rendered on page load it will take existing values at that time. 由于javascript将在页面加载时呈现,因此届时将采用现有值。

Also check http://api.jquery.com/jQuery.post/ for making call more clearner to handle response. 还要检查http://api.jquery.com/jQuery.post/,以使呼叫更清晰以处理响应。

Just to conclude the comments I made above: 1st quote the string literal vars eg 总结一下我在上面所做的评论:第一个引用字符串文字vars,例如

    $test = "test";
var test = '<?php echo $test; ?>';

Then add them as key value pairs to the object you're passing to the post function eg 然后将它们作为键值对添加到要传递给post函数的对象,例如

{ test: test }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM