0

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...

Enora TP
  • 5
  • 6

1 Answers1

0

The submitted form (first view) has no field with name 'liste', so $this->input->post('liste') (in your controler's public function sendJson()) returns null.


Update:

Also you do not need two view files. Javascripts and form could be in the same file:

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

<body>
<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>
</body>
</html>

Then, edit your main js script so it will send the posted data (read this answer: Submitting HTML form using Jquery AJAX).

Another option:

If you just want to pass the data from the controller to the form, you do not need to make it via ajax. You can simply pass it straight to the view.

Community
  • 1
  • 1
Geo Halkiadakis
  • 300
  • 1
  • 7
  • Sure, but isn't it built and sent by the controller between the first and the second page ? (rechercheJardin method) – Enora TP Aug 18 '16 at 12:54
  • in your ajax you do not post any value to receive $this->input->post('liste'); this this post request so first you should add the parameter in your ajax. – Junaid Aug 18 '16 at 13:45
  • Alright but since I get said data from php, how could I put it in my ajax? Do I need to post it right before I load the view? But sending the same data twice...? (Sorry if my questions sound stupid, I just don't understand the logic in this) – Enora TP Aug 18 '16 at 14:15
  • A reply method to an ajax call does not need to load a view (and that is the the way you programmed it in `function sendJson()`). You also do not not need to send the data twice, just handle the posted data into `function sendJson()`. – Geo Halkiadakis Aug 18 '16 at 14:28
  • I think there is another mistake on your code. You do not need two view files; one is enough. Have a look at the updated answer. – Geo Halkiadakis Aug 18 '16 at 14:46
  • I actually do need two separate files since the first view is my main view (aka welcome message sort of) and the second one will eventually display the data as markers on a map. I tried to handle said data in sendJson() but when I try to get the values I need from the form in the first page they appear as null... Thanks though for the help, it's really appreciated! – Enora TP Aug 18 '16 at 15:06
  • you can get the posted data through php `$_POST` array. For example try: `public function sendJson() { echo json_encode($_POST); }` – Geo Halkiadakis Aug 18 '16 at 15:22
  • That's what I thought too, which is why I tried `$_POST` as well as `$this->input->post()` but they always come back as null. – Enora TP Aug 18 '16 at 15:50
  • First edit your javascript to post the form data correctly (read this: [http://stackoverflow.com/questions/16323360/submitting-html-form-using-jquery-ajax]). Ajax call should be made _on form submit_ and you need to `preventDefault()` event. – Geo Halkiadakis Aug 18 '16 at 15:56
  • I tried that too but didn't manage to make it work, so eventually I took another option (echoing my script in my header), which is far for being a solution I am fine with but I need to keep going. Thanks anyway for the leads! I may try them again once the whole project's complete! – Enora TP Aug 19 '16 at 08:20