Ciao, mi sto avvicinando a PHP OOP, MVC e Ajax ma ho un problema.
In una delle mie view c'e una select e in base alla selezione voglio fare una chiamata ad un metodo php e mostrare i dati. ma mi sono perso da qualche parte e non riesco a capire quale sia il percorso giusto che deve fare il codice. Credo sia abbastanza semplice da risolvere ma non capisco che metodo chiamare nella richiesta ajax e soprattutto se mi serve un nuovo metodo nel controller o no.
Grazie 
Questa e la mia view
codice:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Load Cars By Choice</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$('#select').change(function (){
//debug
var model = $(this).val();
var str = "";
$("select option:selected").each(function(){
str += "<br/><div><b>Company Name: </b>" + $(this).text() + "</div><br/>";
var selection = $(this).text();
var carModelUrl = ".../my_mvc/index.php/loadCarsByChoice";
$.getJSON(carModelUrl, selection, function(json){
console.log("JSON: ", json);
str += "<table>";
for (var i = 0; i < json.length; i++) {
str += "<tr><td>" + (i+1) + " - " + json[i].BrandName +
"</td><td>" + json[i].Model +
"</td><td>" + json[i].Colour +
"</td><td><img src='../assets/" + json[i].BrandName + ".jpg'></img></td></tr>";
}
str += "</table>";
document.getElementById("placeholder").innerHTML=str;
});
});
});
})
</script>
</head>
<body>
<h1>Choose a car to see details</h1>
<form>
<select id="select">
<?php
for ($i=0; $i < count($data); $i++) {
echo "<option value='".$data[$i]."'>".$data[$i]."</option>";
}
?>
</select>
</form>
<div id="placeholder"></div>
</body>
</html>
questo e il controller
codice:
function loadCarsByChoice(){
$data = $this->model->getBrandNames();
$this->load->view('loadCarsByChoice', $data);
}
e questi sono due metodi che ho nel model (il primo getBrandNames() serve per mostrare i dati nella select, il secondo deve ricevere la richiesta ajax con il valore selezionato e ritornare i dati)
codice:
public function getBrandNames(){
try{
$sql = "SELECT DISTINCT BrandName FROM Cars";
$stmt = $this->dbHandler->query($sql);
$data = $stmt->fetch();
$result = null;
$i = 0;
while($data = $stmt->fetch()){
$result[$i] = $data['BrandName'];
$i++;
}
}catch(PDOEXception $e){
print new Exception($e->getMessage());
}
$this->dbHandler = NULL;
return $result;
}
public function getCarDetails(){
$carName = $_GET;
try{
$sql = "SELECT * FROM Cars WHERE BrandName = '". $carName . "'";
$stmt = $this->dbHandler->query($sql);
$result = null;
$i = 0;
while($data = $stmt->fetch()){
$result[$i]['BrandName'] = $data['BrandName'];
$result[$i]['Model'] = $data['Model'];
$result[$i]['Colour'] = $data['Colour'];
$i++;
}
}catch(PDOEXception $e){
print new Exception($e->getMessage());
}
$this->dbHandler = NULL;
return json_encode($result);
}