在jQuery .post中检索$ _POST

时间:2016-09-08 23:57:07

标签: javascript php jquery post

我有一些带有一些值的选择。

当选择一个选项时,我需要在jQuery中使用.post将所选值发送到PHP函数以获取PHP上的$_POST变量。

然后我可以使用该变量来评估PHP上的条件。

我使用CakePHP 2创建一个URL。

将帖子网址绑定到numberOption变量。

                $( "#selectFilter" ).change(function() {
  var numberOption = "<?php echo $this->Html->url(array('action' => 'returnNumberOption/' ));?>" + 
  "/" + $( "#selectFilter option:selected" ).val();

  $.post(numberOption, function(data){
    console.log("Here is the number value of the select "+data);
    var phpVariable = "<?php echo $_POST?>"//THIS DONT WORK!
  });
}); 

使用此代码我收到unexpected identifier错误。

问题

如何在jQuery $_POST调用中获取$.post变量?

1 个答案:

答案 0 :(得分:0)

    var url = "<?php echo $this->Html->url(array('action' => 'returnNumberOption/' ));?>"; 
    var value = $( "#selectFilter option:selected" ).val();

     $.post(url,{data: value})
        .done(function(data){
        console.log("Here is the number value of the select "+data);
        var phpVariable = "<?php echo $_POST?>"//THIS DONT WORK!
    });

<?php
public function returnNumberOption() {
    if ($this->request->is('post')) {
        $data = $this->request->data;
    }
}