CodeIgniter Forums

Full Version: Blog Form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
BLOG FORM
Date :  May 2016
Su  Mo  Tu  We  Th  Fr  Sa
 
Upload :  [Upload]

Title :  New Title 

Content :   New Content


I try to input New Title and New Content.  But my form does not work.  It cannot input my intended content which is "New Title" and "New Content".  How to code it right so that I can input the form?



views/blog_form.php



PHP Code:
   <h1>BLOG FORM</h1>
 
   <?php if (validation_errors()) : ?>
    <div class='validation-errors'>
        <h2 class='validation-heading'>Please fix the following errors:</h2>
        <?php echo validation_errors(); ?>
    </div>
    <?php endif; ?>
    <?php echo form_open('blog'); ?>
        <fieldset>
            <div class='form-field'>
                <label for='blog_date'>Date:</label>
                <input type='input' name='blog_date' id='blog_date' value="<?php echo set_value('blog_date'); ?>" />
            </div>
            <div class='form-field'>
                <label for='pic'>Upload:</label>
                <input type='file' name='pic' id='pic' />
            </div>        
            <div class='form-field'>
                <label for='username'>Title:</label>
                <input type='input' name='username' id='username' value="<?php echo set_value('username'); ?>" />
            </div>        
            <div class='form-field'>
                <label for='blog_content'>Content:</label>
                <textarea name='text' id='blog_content' rows='8' cols='40'><?php echo set_value('blog_content'); ?></textarea>
            </div>
        </fieldset>
        <fieldset class='form-actions'>
            <input type='submit' name='blog_submit' value='Submit' />
        </fieldset>
    <?php echo form_close(); ?>



models/blog_model.php


PHP Code:
   public function insert_blog()
 
       {
 
               /*
                $this->title    = $_POST['title']; // please read the below note
                $this->content  = $_POST['content'];
                $this->date     = time();
                */
 
                               
                $data 
= array(
                    
'title' => $this->input->post('title'),
                    
'text' => $this->input->post('text')
                );

                return 
$this->db->insert('blog'$data);
 
       }

 
       public function update_blog()
 
       {
 
               $this->title    $_POST['title'];
 
               $this->content  $_POST['content'];
 
               $this->date     time();

 
               $this->db->update('blog'$this, array('id' => $_POST['id']));
 
       
I'm not 100% sure what you are asking, but you need input fields for 'title' and 'content' as a good starting point ie:

<input type='text' name='title' />
<input type='text' name='content' />
Well, please check my code above:  views/blog_form.php

This is the actual view:

BLOG FORM
Date :  May 2016
Su  Mo  Tu  We  Th  Fr  Sa




Upload :  [Upload]

Title :  [       ]

Content :   [        ]

[Submit]

--------------------------------------------------------

If someone click the submit button it will actually show this:

http://localhost/blog/index.php/blog_form

404 Page Not Found
The page you requested was not found.

If I click the submit button it will carries me to success form and store data in the database.

I would like to know if someone input in title form or content it will actually place input in the database.


I also have revised my codes:

views/blog_form.php

PHP Code:
<html>

<
title>Blog Form</title>

<
br>

<
h1>BLOG FORM</h1>

<?
php echo validation_errors(); ?>

<?php echo form_open('blog_form'); ?>

<table>

<tr>
<td>Date  :</td>
<td><?php echo $this->calendar->generate();  ?></td>
</tr>
<tr>
<td>Upload  :<br><br></td>
<td><input type="file" name="pic" id="pic"><br></td>
</tr>
<tr>
<td>Title :</td>
<td><input type="input" name="title" value="" size="30" /></td>
</tr>
<tr>
<td>Content :</td>
<td>
<textarea rows="8" cols="40" name="content">

</textarea>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</form>
</table>


</html>   


controllers/blog.php

PHP Code:
<?php


class Blog extends CI_Controller {

        
// If you want code to run for every method in a controller, you need to create a constructor
 
       
        
public function __construct()
 
       {
                
parent::__construct();
                
                
$this->load->helper('date');                 
                
$this->load->helper('form');  
                $this
->load->library('calendar');                          
                $this
->load->library('form_validation');
 
               $this->load->model('blog_model');
        }        
                
        public function 
index()
        {
            
 
               $data['title'] = $this->blog_model->select_blog();
                
$data['content'] = $this->blog_model->select_blog();
                
$data['date'] = $this->blog_model->select_blog();
                
                
                
$this->load->view('blog'$data);
                                
        }
        
        public function 
blog_form()
        {
                
                
$this->form_validation->set_rules('pic''pic''required');
                
$this->form_validation->set_rules('title''title''required');
                
$this->form_validation->set_rules('content''content''required');
                
                if (
$this->form_validation->run() === FALSE)
                {
                
$this->load->view('blog_form');
                }
                
                else
                {
                    
$this->blog_model->insert_blog();
                    
$this->load->view('blog_form/success');                    
                }
                                
        }
}



?>


models/blog_model.php

PHP Code:
<?php



class Blog_model extends CI_Model {

 
       public $title;
 
       public $content;
 
       public $date;

 
       public function __construct()
 
       {
 
               // Call the CI_Model constructor
 
               parent::__construct();
                
                
$this->load->database();
 
       }

        public function 
select_blog()
        {
            
             
   $query $this->db->query('SELECT * FROM blog');

                return 
$query->result();
                
//  return  'masuk loh';
        
}
        
 
       public function insert_blog()
 
       {
 
                                              
                $data 
= array(
                    
'title' => $this->input->post('title'),
                    
'text' => $this->input->post('text'),
                    
'content' => $this->input->post('content')                    
                );

                return 
$this->db->insert('blog'$data);
 
       }

 
       public function update_blog()
 
       {
 
               $this->title    $_POST['title'];
 
               $this->text  $_POST['text'];
                
$this->content $_POST['content'];
 
               $this->date     time();

 
               $this->db->update('blog'$this, array('id' => $_POST['id']));
 
       }

}



?>
Hi - hey you are making some very basic mistakes. So first - do the tutorial in the manual.
http://www.codeigniter.com/user_guide/tu...index.html