1

I am dynamically adding table rows by selecting options from dropdown and then I am trying to send html table rows to php function using ajax as array in json format. However php function does not print all rows in the console log, when I am submitting more than one row. I got the desired output like once or twice. I think I am missing something in the php function. Please check once. If anymore information about the code is required, let me know, I will update.

Javascript:

function storeClgValues() {
  var CollegeData= new Array();
  $('#collegetable tr').each(function(row, tr){
    CollegeData[row]={
        "college" : $(tr).find('td:eq(0)').text()
    }
  }); 
  CollegeData.shift();
  return CollegeData;

}

$('#submit').click(function() { 

 CollegeData=storeClgValues();
 CollegeData=$.toJSON(CollegeData);                     
 $.ajax({
       type:"POST",
       url: '<?php echo base_url();?>ajaxController/insertcollege',
       data: "CollegeData=" + CollegeData,
       success: function(msg) {
          console.log(CollegeData);
          console.log(msg);             
       }
  });

});

PHP function in AjaxController class:

 public function insertcollege()
 {   
    $data=array();
    $data=stripcslashes($_POST['CollegeData']);
    $data=json_decode($data, TRUE);
    //echo $data[0]['college'].'<br>';
    //echo $data[1]['college'].'<br>';
    if (is_array($data) || is_object($data))
    {
        foreach ($data as $key => $item) {
            echo $item['college'].'<br>';
        }
    }
 }

Output in console in three tries:

[{"college":"College of Agriculture"}]
College of Agriculture

[{"college":"College of Agriculture"},{"college":"College of Business"}]
College of Agriculture 
College of Business

[{"college":"College of Agriculture"},{"college":"College of Business"}, {"college":"College of Comm & Educati"}]
<!--nothing gets printed-->
8
  • What does print_r or var_dump look like of $data after $data=json_decode($data, TRUE); when you've selected multiple compared to when you selected a single row? Commented Jan 21, 2017 at 1:32
  • Also what does print_r of $_POST look like when you've selected multiple rows? Commented Jan 21, 2017 at 1:38
  • This is really weird. I do not know what is causing the problem. But sometimes I get the output sometimes I do not get the response back. Print_r on $data printed like this. Array ( [0] => Array ( [college] => College of Agriculture ) [1] => Array ( [college] => College of Business ) ) Commented Jan 23, 2017 at 21:21
  • Do you have more than one element on the page where the id="submit"? Commented Jan 23, 2017 at 21:37
  • Or multiple rows where id="collegetable"? Also thinking might be a possibility that the Ajax post is happening before storClgValues has finished doing what it needs to do. Commented Jan 23, 2017 at 21:48

1 Answer 1

1

Try like this...

<?php
$json = '[{"college":"College of Agriculture"},{"college":"College of Business"}, {"college":"College of Comm & Educati"}]';
$data = json_decode($json,TRUE);
//print_r($data);
if (is_array($data) || is_object($data))
    {
        foreach ($data as $key => $item) {
            $output[]=$item['college'].'<br>';
        }
    }
echo json_encode($output);

?>

OR

<?php
$json = '[{"college":"College of Agriculture"},{"college":"College of Business"}, {"college":"College of Comm & Educati"}]';
$data = json_decode($json,TRUE);
//print_r($data);
if (is_array($data) || is_object($data))
{
    foreach ($data as $key => $item) {
        foreach($item as $value){
            $output[] = $value."</br>";
        }
    }
}   
echo json_encode($output);
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. Sometimes I get the output sometimes I do not. I do not know what is causing the problem,

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.