2

JAVASCRIPT: On clicking a button I want to do the following:

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: This function will receive the values and will do some database operation. It is not mentioned here.

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 ,It is not working.

2 Answers 2

2
var formData = new FormData();
var paymentPeriodId = $('#paymentPeriodID').val();
checkedRows = $.toJSON(checkedRows);
formData.append("paymentPeriodId", paymentPeriodId);
formData.append("checkedRows", checkedRows);
var qurl = '<?php echo base_url();?>salary_processing/summeriseProduction';
$.ajax({
url: qurl,
type: "POST",    
data: formData,         
success: function(data){}

try like this

Sign up to request clarification or add additional context in comments.

8 Comments

Do I need to use stripcslashes() and json_decode() in php function after receiving.
yes like $checkedRows = ($_POST['checkedRows']); $checkedRowsjson = json_decode(stripslashes($checkedRows), true);
Thanks, will try it.
take your time comment if something went wrong il try my best to help
I have edited the question with the formdata but not working
|
0
var formData = new FormData();
var paymentPeriodId = $('#paymentPeriodID').val();
checkedRows = $.toJSON(checkedRows);
formData.append("paymentPeriodId", paymentPeriodId);
formData.append("checkedRows", checkedRows);
var qurl = '<?php echo base_url();?>salary_processing/summeriseProduction';
$.ajax({
url: qurl,
type: "POST",
processData: false,
contentType: false,
data: formData,         
success: function(data){}

It woks like this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.