2017-01-21 5 views
1

Ich bin dynamisch Hinzufügen von Tabellenzeilen durch Auswahl von Optionen aus Dropdown und dann versuche ich, HTML-Tabellenzeilen zu PHP-Funktion mit AJAX als Array im JSON-Format zu senden. Die PHP-Funktion druckt jedoch nicht alle Zeilen im Konsolenprotokoll, wenn ich mehr als eine Zeile einreiche. Ich habe die gewünschte Ausgabe wie ein- oder zweimal bekommen. Ich denke, ich vermisse etwas in der PHP-Funktion. Bitte einmal prüfen. Wenn weitere Informationen über den Code erforderlich sind, lassen Sie es mich wissen, ich werde aktualisieren.Post JSON-Objekt zu PHP mit Ajax

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-Funktion in AjaxController Klasse:

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>'; 
     } 
    } 
} 

Ausgabe in der Konsole in drei Versuchen:

[{"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--> 
+0

Wie sieht print_r oder var_dump nach $ data = json_decode ($ data, TRUE) aus $ data aus? 'Wenn Sie im Vergleich zu einer einzelnen Zeile mehrere ausgewählt haben? – mrjamesmyers

+0

Auch wie sieht print_r von $ _POST aus, wenn Sie mehrere Zeilen ausgewählt haben? – mrjamesmyers

+0

Das ist wirklich komisch. Ich weiß nicht, was das Problem verursacht. Aber manchmal bekomme ich die Ausgabe manchmal bekomme ich die Antwort nicht zurück. Print_r auf $ Daten gedruckt wie folgt. Array ( [0] => Array ( [College] => Hochschule für Landwirtschaft ) [1] => Array ( [College] => College of Business ) ) – jstandshigh

Antwort

1

wie die Sie interessieren ...

<?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); 

?> 

ODER

<?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); 
?> 
+0

Danke für die Antwort. Manchmal bekomme ich die Ausgabe manchmal nicht. Ich weiß nicht, was das Problem verursacht, – jstandshigh