2
JAVASCRIPT:在点击一个按钮,我要做到以下几点:发送JSON和JS可变数据到PHP脚本在一个Ajax请求
var paymentPeriodId = $('#paymentPeriodID').val();
if(paymentPeriodId < 1){alert('Payment period not set.');e.preventPropagation();return false; }
if(checkedRows.length < 1){alert('Supervisor not selected.');e.preventPropagation();return false; }
checkedRows = $.toJSON(checkedRows);
var formData = new FormData();
formData.append("paymentPeriodId", paymentPeriodId);
formData.append("checkedRows", checkedRows);
// alert(JSON.stringify(checkedRows));
alert(paymentPeriodId);
alert(checkedRows);
var qurl = '<?php echo base_url();?>salary_processing/summeriseProduction';
$.ajax({
url: qurl,
type: "POST",
data:formData,
success: function(data){
alert('successfull');
// var data = $.parseJSON(data);
// alert(JSON.stringify(data));
},
});
PHP:此功能将得到的值,并会做一些数据库操作。这里没有提到。
public function summeriseProduction() {
// Unescape the string values in the JSON array
$supervisorIdArray = stripcslashes($this->input->post('checkedRows'));
$paymentPeriodId = $this->input->post('paymentPeriodId');
// Decode the JSON array
$supervisorIdArray1 = json_decode($supervisorIdArray, TRUE);
// $paymentPeriodId1 = json_decode($paymentPeriodId, TRUE);
echo json_encode($supervisorIdArray1);
// echo $paymentPeriodId;
}
https://stackoverflow.com/users/4476402/pekka,它不工作。
我需要在接收后使用php函数中的stripcslashes()和json_decode()。 –
yes like'$ checkedRows =($ _POST ['checkedRows']); $ checkedRowsjson = json_decode(stripslashes($ checkedRows),true);' – guradio
谢谢,会试试看。 –