I am new in ajax.I want to send html form values along with some other variables to php using ajax,so i serialize all input values and send to php.But the problem offset occurred, when i try to send multiple variables.
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)
Here is my Ajax Code:
<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>
I think php is not getting all the variables.I also follow this link AJAX - multiple data and try with data: {values,flag}
and data: {values:values , flag:flag}
or values={values:values , flag:flag}
. But still same offset problem.
here is my php code:
<?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;
?>
Please help me and let me know for any further information.Thanks.