我是ajax中的新手。我想使用ajax将html表单值和其他一些变量一起发送到php,所以我序列化所有输入值并发送到php.But问题偏移发生,当我尝试发送多个变量
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;
?>
请帮助我,让我知道任何进一步的信息。谢谢。
答案 0 :(得分:1)
对应于您的Ajax代码,PHP脚本获取 2 $ _POST参数: 值 和 标志 强>
values 参数因此必须由PHP脚本拆分和解释。下面的snipplet显示了如何做到这一点。
注意:
<?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