CodeIgniter Forums
Question about submission form to database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: Question about submission form to database (/thread-74166.html)



Question about submission form to database - ronniebel - 08-07-2019

I'm pretty new at CodeIgniter. I'm now wanting to create my first submission form to my MySQL database. My database settings are correct in the database.php in the config folder. I know that I need a view page, a controller page and a model page for this task, but I need to see some samples that I can use and test.  I found some sample view, controller and model code on this page but I'm getting an error message on my test view page. Below is line 10 in the code. Any ideas what's happening? Or do you know any other places where I can copy the view, controller and model code to try to replicate this on my own site for practice?
PHP Code:
<?php echo form_open('insert_ctrl'); ?>



RE: Question about submission form to database - demyr - 08-07-2019

I would recommend you going through the html form at first.

Like,

View : 

PHP Code:
<form action="POST" method="<?php echo base_url('/your_controller/lets_post_it');?>">
 
  <input type="text" name="member_name" class="blabla" placeholder="Please Insert Your Name"/>
 
  <input type="text" name="member_username" class="blabla" placeholder="Please Insert Your Username"/>

<
buttonSend it </button>

</
form


Controller 

PHP Code:
public function lets_post_it() {

 
 $name $this->input->post('member_name);  //first retrieve from inputs
  $username = $this->input->post('
member_username');

  $data = array(
     '
member_name'=> $name,  // then match with your database
     '
member_username' => $username
);

   $this->load->model('
My_Model');
  $result = $this->My_Model->upload_it($data);  // and send it to model

  if($result){
      $this->session->set_flashdata('
success', 'Data has been inserted successfully');
      redirect($SERVER['
HTTP_REFERER']);
}


MODEL

PHP Code:
public function upload_it($data){
 
 
   $query 
$this->db->insert('table_name'$data);

 
  return $query;




RE: Question about submission form to database - donpwinston - 08-07-2019

You don't have the form helper loaded. Load it in your controller constructor.


RE: Question about submission form to database - dave friend - 08-07-2019

The error that "test view page" displays indicates you have not loaded the "form" helper.

The following line of code is in the example you are working from

PHP Code:
$this->load->library('form_validation'); 

It turns out that "form_validation" will load the "form" helper for you.

If you are not using "form_validation" in your test then explicitly load the "form" helper in your controller with this line.
PHP Code:
$this->load->helper('form'); 



RE: Question about submission form to database - ronniebel - 08-07-2019

(08-07-2019, 02:30 PM)dave friend Wrote: The error that "test view page" displays indicates you have not loaded the "form" helper.

The following line of code is in the example you are working from

PHP Code:
$this->load->library('form_validation'); 

It turns out that "form_validation" will load the "form" helper for you.

If you are not using "form_validation" in your test then explicitly load the "form" helper in your controller with this line.
PHP Code:
$this->load->helper('form'); 
Thanks for the info.  Do I replace in my controller

PHP Code:
$this->load->library('form_validation'); 

with this below?
PHP Code:
$this->load->helper('form'); 



RE: Question about submission form to database - dave friend - 08-07-2019

(08-07-2019, 02:45 PM)ronniebel Wrote:
(08-07-2019, 02:30 PM)dave friend Wrote: The error that "test view page" displays indicates you have not loaded the "form" helper.

The following line of code is in the example you are working from

PHP Code:
$this->load->library('form_validation'); 

It turns out that "form_validation" will load the "form" helper for you.

If you are not using "form_validation" in your test then explicitly load the "form" helper in your controller with this line.
PHP Code:
$this->load->helper('form'); 
Thanks for the info.  Do I replace in my controller

PHP Code:
$this->load->library('form_validation'); 

with this below?
PHP Code:
$this->load->helper('form'); 

If you are not using field validation you can (but do not have to) replace library('form_validation') with helper('form'). One or the other must be used.


RE: Question about submission form to database - Wouter60 - 08-08-2019

You load helpers, libraries and models in the controller where you need them. If you need specific helpers, libraries or models in all your controllers, you can autoload them in application/config/autoload.php.
It's a good idea to autoload the url helper, for instance, and also the database library. If you use session variables all over your application, autoload the session library as well.