如何通过从ajax正确发送多个变量来避免任何问题

时间:2016-01-27 06:02:32

标签: php ajax

我是ajax中的新手。我想使用ajax将html表单值和其他一些变量一起发送到php,所以我序列化所有输入值并发送到php.But问题偏移发生,当我尝试发送多个变量

enter image description here

var_dump($_POST);
array (size=2)
  'values' => string 'loanid=123&loan_name=Bank+Loan&description=FRB+Bank+Loan&amount=25000&textdatetimepicker1=27%2F01%2F2016&rate=12.5' (length=114)
  'flag' => string '2' (length=1)

这是我的Ajax代码:

<script type="text/javascript">                                             
                $("form").on("submit", function( event ) {              
                    event.preventDefault();                 
                    var flag = 2;   //want to send this 
                    var values = $( this ).serialize();  //want to send this                        
                    $('#serialize').append(values);                  
                    $.ajax({
                        url: "loan_type_info.php",
                        type: "post",
                        async:true,
                        data:  {values:values , flag:flag},
                        dataType: 'html',
                        contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                        success: function(data) {                           
                            $('#result').append('Response Data:');
                            $('#result').append(data);                          
                        },                  
                        error: function(jqXHR, textStatus, errorThrown) {
                           console.log(textStatus, errorThrown);
                        }
                    });
                });         
</script>

我认为php没有获得所有变量。我也会点击此链接AJAX - multiple data并尝试使用data: {values,flag}data: {values:values , flag:flag}values={values:values , flag:flag}。但仍有相同的偏移问题。

这是我的PHP代码:

<?php
// they all came from serialize data: values
$loanid      = (isset($_POST['loanid'])) ? $_POST['loanid'] : "not";
$loan_name   = (isset($_POST['loan_name'])) ? $_POST['loan_name'] : "not";
$description = (isset($_POST['description'])) ? $_POST['description'] : "not";
$amount      = (isset($_POST['amount'])) ? $_POST['amount'] : "not";
$datestring  = (isset($_POST['textdatetimepicker1'])) ? $_POST['textdatetimepicker1'] : "not";
$rate        = (isset($_POST['rate'])) ? $_POST['rate'] : "not";
$date_arr = explode('/', $datestring);
$date     = date("Y/m/d", strtotime($date_arr[2] . $date_arr[1] . $date_arr[0]));

//  comes from  data: flag
$flag  = (isset($_POST['flag'])) ? $_POST['flag'] : 0;
?>

请帮助我,让我知道任何进一步的信息。谢谢。

1 个答案:

答案 0 :(得分:1)

对应于您的Ajax代码,PHP脚本获取 2 $ _POST参数 标志

values 参数因此必须由PHP脚本拆分和解释。下面的snipplet显示了如何做到这一点。

注意:

  • 我在开头模拟了$ _POST数组。
  • 变量分为爆炸('&amp;',$ _ POST ['values'])
  • 在循环中, key = value 对分开爆炸('=',$ value)部分为url_decoded,因为它来自Ajax调用url_encoded。
  • 最后,部分被分配给名为 key 部分的变量 $$ keyvar [0] = $ keyvar [1] < /强>

代码

    <?php

    $_POST = array(
        'values' => 'loanid=123&loan_name=Bank+Loan&description=FRB+Bank+Loan&amount=25000&textdatetimepicker1=27%2F01%2F2016&rate=12.5',
        'flag' => '2'
    );

    // Code BEGIN ==============================================================

    $values = (isset($_POST['values']) && !empty($_POST['values'])) ? explode('&',$_POST['values']) : false;
    $flag =   (isset($_POST['flag']) && !empty($_POST['flag'])) ? $_POST['flag'] : '0';

    if ($values!==false) foreach ($values as $key => $value) {
        $keyvar = explode('=', $value);
        $keyvar[1] = (isset($keyvar[1])) ? urldecode($keyvar[1]) : '';
        $$keyvar[0] = $keyvar[1];
    }

    // Code END ================================================================

    echo '$loanid=' . ((isset($loanid)) ? $loanid : 'n/a') . '<br />';
    echo '$loan_name=' . ((isset($loan_name)) ? $loan_name : 'n/a') . '<br />';
    echo '$description=' . ((isset($description)) ? $description : 'n/a') . '<br />';
    echo '$amount=' . ((isset($amount)) ? $amount : 'n/a') . '<br />';
    echo '$datestring=' . ((isset($datestring)) ? $datestring : 'n/a') . '<br />';
    echo '$rate=' . ((isset($rate)) ? $rate : 'n/a') . '<br />';
    echo '$textdatetimepicker1=' . ((isset($textdatetimepicker1)) ? $textdatetimepicker1 : 'n/a') . '<br />';

    ?>

输出

    $loanid=123
    $loan_name=Bank Loan
    $description=FRB Bank Loan
    $amount=25000
    $datestring=n/a
    $rate=12.5
    $textdatetimepicker1=27/01/2016