I have not understood how Ajax works with CodeIgniter. I explain you the problem.
In the view "Articles" i initially load all the articles. I have a select box too. When i perform an "onChange" i execute a getData() Ajax call with the classic "xmlhttp". The result would be to view only articles which have the needed tipology. "Tipology" is the connector field behind the View (where i see all articles) and the Ajax file.
The error that Firefox console displays me is the following:
GET http://localhost/CodeIgniter/index.php?c=ajax_controller?tipologia=Recensioni 404 Not Found 13ms
I don't know if it's the right way to do it, but here i attach you my code, explaining the process:
Main articles view:
<select onChange="getResults()" name="tipologia" id="tipologia">
<option>--Seleziona--</option>
<option>Recensioni</option>
<option>News</option>
</select>
In the Main articles view, here is the ajax call
var xmlhttp;
function getResults() {
xmlhttp = new XMLHttpRequest();
var tipologia = document.getElementById("tipologia").value;
var url = "./index.php?c=ajax_controller?tipologia=" + tipologia;
xmlhttp.onreadystatechange = callback;
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function callback() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
console.log(xmlhttp.responseText);
}
}
The response should replace (innerHTML = responseText) the div that actually contains this code (however, the not filtered View and the filtered View should execute the same foreach):
foreach ($contacts as $contact) {
echo $contact->id;
echo " -> ";
echo $contact->artist;
echo " : ";
echo $contact->album;
echo " -> ";
echo "<a href='/CodeIgniter/index.php?c=new_controller id=".$contact->id."'>Dettagli</a>";
echo "</br>";
}
This always happens inside the Main article view.
In the model i have this function
function get_ajax_results() {
$tipologia = $_REQUEST['tipologia'];
$query = $this->db->query("SELECT artist, album, cover FROM contacts WHERE tipologia = '".$tipologia."' ");
return $query->result();
}
In the Ajax controller...
public function index() {
$this->load->model("new_model");
$data['contacts'] = $this->new_model->get_ajax_results();
$this->load->view('ajax_view', $data);
}
Where is the error?