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

在一个js函数中将变量javascript传递给php,php,yii,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

我有下面这样的函数,如何将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; 

}
函数addPembayaran()
{
$(“#tbangsuran网格表tbody tr”)。单击(函数()
{
$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();

有两种方法可以做到这一点:

  • 在javascript会话中保存该变量,并从php获取该变量
  • 用于要
    $no_loan
    的位置,该位置必须是全局变量

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

  • 您还必须编写一个函数来设置
    $no\u loan

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

    CHtml将对给定的url执行请求

    正在发送的参数位于:

    'data'=> "js:$(this).serialize()",
    
    这些被传递到这里:

    'url'=>array('Tbpembayaran/create'),
    
    因此,如果要传递更多变量,可以向
    数据添加更多变量:

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

    或:


    谢谢你的回答,我已经尝试了方法一来使用sessionStorage.setItem(“noloan”,“firstColVal”);但是我无法获得会话。你能给出,如何使用js创建会话吗?我得到错误“TbangsuranController类的对象无法转换为字符串”,tksI的意思是
    $(this)。find('td:first child')).text()将出错亲爱的,我不知道您试图分配给另一个变量的是什么,或者您的html标记是什么!只需先检查fireBug或其他东西,如果
    $(this)。查找('td:first child')。text()
    将选择所需的值或不选择。如果不选择,请尝试其他选择器,对吗?
    'data' => "js:{no_loan:$no_loan}"
    
    'url' => array('Tbpembayaran/create','no_loan'=>$no_loan)