如何在PHP中的一个函数中使用这两个变量

时间:2017-08-21 06:31:48

标签: javascript php ajax

我使用AJAX实时传递数据。到目前为止,这是我的代码:

function myFunction() {
    var packages = $('#packageselect').val();
    if (packages == '6') {
        $('.opttnx').css('display', 'block');
    } else {
        $('.opttnx').css('display', 'none');
    }

    var email = $('#exampleInputEmail1').val();
    var tnxpack = $('#tankselect').val();
    var nameClient = $('#custname').val();
    var transac = $('#transac').val();
    var amount = $('#amount').val();
    var date = $('#date').val();
    var chooseweb = $('#chooseweb').val();
    var datum = {
        email: email,
        packages: packages,
        tnxpack: tnxpack,
        username: nameClient,
        transac: transac,
        amount: amount,
        date: date,
        chooseweb: chooseweb
    }

    $.ajax({  
        type: 'GET',
        url: 'email/viewEmail', 
        data: datum,
        success: function(response) {
            $('#viewemail').html(response);
        }
    });
} 

这是我的PHP代码:

public function viewEmail() {
    $username = $_GET['username'];
    $packages = $_GET['packages'];
    $tnxpack = $_GET['tnxpack'];
    $amount = $_GET['amount'];
    $date = $_GET['date'];
    $chooseweb = $_GET['chooseweb'];
    $transac = $this->search_model->getTransac($_GET['transac'],$date);
    $message = $this->search_model->getPackageView($packages, $username, $this->ion_auth->user()->row()->type, $tnxpack, $transac, $amount, $chooseweb);
    echo $message;
}

这就是我在viewer.php中所说的:

public function getTransac($transacChose) {
    $transaction = "";
    $transaction2 = "";

    if ($transacChose == '1') {
        $transaction = "and sample";
        $transaction2 = "and sample2";
    }
}

 ``` //this is how i call my transac.
 This is a sample sentence ". $transac . "; ```

有人能帮助我吗?这对我来说有点让人困惑,因为我没有使用数组在我的方法中存储两个变量。

我应该这样做吗?

This is a sample sentence ". $transac[1] .";

2 个答案:

答案 0 :(得分:0)

声明函数外的所有var

示例:

var packages;

function myFunction(){
   packages = $('#packageselect').val();
}

答案 1 :(得分:0)

通过在我的ajax中添加另一个变量

var transac = $('#transac').val();

和我的php

$transac2 = $this->search_model->getTransac2($_GET['transac']);

就是这样。哈哈哈,我知道我知道这是一个新手,但仍然有效:)