在一个js函数中将变量javascript传递给php

时间:2014-01-01 05:04:19

标签: php yii

我有如下所示的函数,如何将js中的变量firstColVal的值发送到php中的变量$ no_loan,它在一个函数js中?我试过尝试,但我仍然无法获得价值?

请帮助我,并在此之前感谢

function addPembayaran()
{
$("#tbangsuran-grid table tbody tr").click(function()
       {
            $this=$(this);
            var firstColVal= $this.find('td:first-child').text();
            var secondColVal= $this.find('td:nth-child(2)').text();
            var lastColVal= $this.find('td:last-child').text();


    <?php 
         $no_loan =$_POST['firstColVal'];
            echo CHtml::ajax(array(
            'url'=>array('Tbpembayaran/create'),
           'data'=> "js:$(this).serialize()",
            //'data'=> array('noloan'=>'20'),
            // data: {ad_id:"hello"},
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
                if (data.status == 'failure')
                {
                    $('#dialogPembayaran div.divForForm').html(data.div);
                          // Here is the trick: on submit-> once again this function!
                    $('#dialogPembayaran div.divForForm form').submit(addPembayaran);
                }
                else
                {
                    $('#dialogPembayaran div.divForForm').html(data.div);
                    setTimeout(\"$('#dialogPembayaran').dialog('close') \",3000);
                }

            } ",
            ))


            ?>;
   // return false; 

}

3 个答案:

答案 0 :(得分:0)

执行此操作的两种方法:

  1. 将该变量保存在javascript中的Session中,并从php
  2. 获取
  3. 使用$.post()到你想要$no_loan的地方,那里必须是一个全局变量

    $ .post(“Tbpembayaran / save_no_loan /”+ $('input [name =“firstColVal”]')。val(),function(){     });

  4. 此外,您还必须编写一个函数来设置$no_loan

    save_no_loan($fromjs)
    {
    $this->no_loan=$fromjs;
    }
    

答案 1 :(得分:0)

CHtml会向给定的网址发送请求,

正在发送的参数位于:

'data'=> "js:$(this).serialize()",

并将这些传递到此处:

'url'=>array('Tbpembayaran/create'),

因此,如果您想传递更多变量,可以向data添加更多内容:

'data'=> "js:$(this).serialize() + '&anotherVariable=' $(this).find('td:first-child').text()", // I think this should do the trick

答案 2 :(得分:0)

一:

'data' => "js:{no_loan:$no_loan}"

或:

'url' => array('Tbpembayaran/create','no_loan'=>$no_loan)
相关问题