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.
所以这就是:我目前正在尝试使用我的一个控制器在数据库查询后创建的JSON。在第一个视图上使用表单后,我得到了创建JSON所需的数据(见后)。在第一个视图中提交表单使得控制器加载第二个,在加载之后,控制器将其发送给JSON,这要归功于加载时传递的数据。
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.
不幸的是,我似乎无法在第二个视图中获得JSON。我尝试构建一个示例JSON并且它可以工作(两个警报都出现了),但是一旦我尝试从输入中获取它,它就不会。
Has anyone faced the same issue before? Is there something I am doing wrong? Thanks for your help!
以前有人遇到过同样的问题吗?有什么我做错了吗?谢谢你的帮助!
My controller: (Recherche.php)
我的控制器:(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.
我在这里简化了rechercheJardins方法,因为我知道它可以完成工作并返回正确的数据。 jardinToArray方法只是将我的数据转换为一个数组(显然......),并且工作正常。
My JS script: (my_script.js)
我的JS脚本:(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)
我的第二个观点:(因为我现在只想提醒结果)(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...
编辑:经过相当多的试验,似乎问题肯定来自“$ array = $ this-> input-> post('liste');” sendJson方法中的行。我试图用它应该返回的样本替换它以查看它是否有效并且确实有效。我似乎无法弄明白为什么......
1 个解决方案
#1
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.
提交的表单(第一个视图)没有名称为“liste”的字段,因此$ this-> input-> post('liste')(在你的控制器的公共函数sendJson()中)返回null。
Update:
Also you do not need two view files. Javascripts and form could be in the same file:
此外,您不需要两个视图文件。 Javascripts和表单可以在同一个文件中:
<?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).
然后,编辑主js脚本,以便发送已发布的数据(阅读此答案:使用Jquery AJAX提交HTML表单)。
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.
如果您只想将数据从控制器传递到表单,则无需通过ajax创建数据。您可以直接将其传递给视图。
#1
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.
提交的表单(第一个视图)没有名称为“liste”的字段,因此$ this-> input-> post('liste')(在你的控制器的公共函数sendJson()中)返回null。
Update:
Also you do not need two view files. Javascripts and form could be in the same file:
此外,您不需要两个视图文件。 Javascripts和表单可以在同一个文件中:
<?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).
然后,编辑主js脚本,以便发送已发布的数据(阅读此答案:使用Jquery AJAX提交HTML表单)。
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.
如果您只想将数据从控制器传递到表单,则无需通过ajax创建数据。您可以直接将其传递给视图。