I am trying to create a data-entry,with controller which inputs the data, displays it and confirm for any edit then uses a method to submit it to the model.
The problem is that in the load the model in the function post_data() the value of $this->input->post() return an empty array.
I am entering the data returning to the get_data function and then displaying it in the data.php in the view.
using
data.php in view post the data to the post_data method.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $this->input->post("xyz") ?>" />
the controller is
protected $arr;
public function index(){
$this->load->view('index/index');
// $this->load->library('Controllerlist');
// print_r($this->controllerlist->getControllers());
}
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
public function post_data(){
$this->load->model("form1","form",TRUE);
print_r($this->input->post());
$blue=$this->form->insert_data($this->arr);
print_r($blue);
if($blue){
echo "Successfully added to database";
}
}}
Why does print_r() method return an empty array? $this->input->post() in the post_data method return empty array. if Iam right $this->input->post() should is global to all the method in Controller CI class.
-
Kindly clarify which $this->input->post() is not working.Kennedy Nyagah– Kennedy Nyagah2015年08月30日 07:42:53 +00:00Commented Aug 30, 2015 at 7:42
-
the one in the post_data method.AminoAcid– AminoAcid2015年08月30日 07:47:03 +00:00Commented Aug 30, 2015 at 7:47
-
Are you sure, that $this->input->post("xyz") has a value? Have you tried to just set a fixed value?Joerg– Joerg2015年08月30日 09:40:04 +00:00Commented Aug 30, 2015 at 9:40
2 Answers 2
Your input tag in form doesn't have a name attribute.
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input name="xyz" type="text" value="<? php echo $this->input->post("xyz") ?>" />
</form>
Edit:
Also the input tag is closed in a wrong way (closing before the value attribute).
<input type="text name="xyz" value="<? php echo $this->input->post("xyz") ?>" />
4 Comments
If you are loading the view data.php in get_data:
public function get_data(){
echo "matoercod";
$this->load->view("index/data");
}
Then how are you accessing the get_data url? If you are just typing it in the browser, then you are not POSTing you are GETing and so $this->input->post is always going to be an empty array.
It's not entirely clear to me how you are calling your method get_data, but you should probably try to alter it so that it more carefully constructs the data you want to be displayed in any view that it loads
public function get_data(){
$view_data = array(
"xyz" => "here is some value" // you could get this value from anywhere
);
$this->load->view("index/data", $view_data);
}
Then in your view data.php you would want to refer to just $xyz instead of $this->input->post("xyz"):
<form id='form' action="<?php echo base_url("welcome/post_data"); ?>" method="POST" style="display:inline;">
<input type="text" name="xyz" value="<?php echo $xyz; ?>" />
Note that second parameter to the view loading function. It's an array and each associative key in the array will be expanded into a variable within your view.