this is my first question ever over here. I have been going through many other websites, tutorials and questions without being able to find the answer to my issue, that eventually I decided to post it, hoping to find a solution and hopefully helping other people facing the same dificulty.
So here it is: I am currently trying to use a JSON created by one of my controllers after a database query. I get the data I need to create my JSON after using the form on my first view (see after). The submit of the form in this first view makes the controller load the second one that requires, after loading, the controller to send it the JSON thanks to the data passed when loading.
Unfortunatelly I can't seem to get the JSON in my second view. I tried building a sample JSON and it works (both the alerts appear), but as soon as I try to get it from the input, it does not.
Has anyone faced the same issue before? Is there something I am doing wrong? Thanks for your help!
My controller: (Recherche.php)
class Recherche extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Recherche_Model');
}
public function rechercheJardins() {
$data['liste'] = $this->Recherche_Model->get();
$this->load->view('my_view', $data);
}
public function sendJson() {
$array = $this->input->post('liste');
$liste = $this->jardinToArray($array);
echo json_encode($liste);
}
}
I simplified the rechercheJardins method here as I know it gets the job done and returns the correct data. The jardinToArray method simply turns my data into an array (obviously...) and also works fine.
My JS script: (my_script.js)
jQuery(document).ready(function () {
jQuery.ajax({
type: 'post',
url: 'http://localhost/garden/Recherche/sendJson',
dataType: 'json',
success: function (data, statut) {
alert('Success');
alert(data);
}
});
});
My first view:
<div id="recherche-accueil">
<?php
$formRecherche = form_open('Recherche/rechercheJardins');
$formRecherche.= form_input('ville', null, 'placeholder="Où souhaitez-vous aller ?"');
$formRecherche.= form_date('arrivee', null, 'placeholder="Date d\'arrivée"');
$formRecherche.= form_date('depart', null, 'placeholder="Date de départ"');
$formRecherche.= form_submit('rechercher', 'Rechercher');
$formRecherche .= form_close();
echo $formRecherche;
?>
</div>
My second view head: (Since I'm only trying to alert the result for now) (my_view.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Project garden</title>
<?php
echo '<link rel="stylesheet" href="' . base_url('public/css/style.css') . '">';
echo '<script type="text/javascript" src="' . base_url('public/js/jquery-3.1.0.js') . '"></script>';
echo '<script type="text/javascript" src="' . base_url('public/js/my_script.js') . '"></script>'
?>
</head>
EDIT: After quite a few more trials, it seems that the problem definitely comes from the "$array = $this->input->post('liste');" line in the sendJson method. I tried to replace it with a sample of what it is supposed to return to see if it worked and it did. I can't seem to figure out why yet though...