Question

I'm building a basic website in CI. I've constructed a form using the form helper and am doing something along these lines:

View (from create_form.php):

<?php $this->load->helper('form'); ?>
<?php echo form_open('site/create_article'); ?>
<?php echo form_label('Title:', 'title'); ?><br />
<?php echo form_input('title'); ?><br /><br />
<?php echo form_label('Body:', 'text'); ?><br />
<?php echo form_textarea('text'); ?><br /><br />
<?php echo form_submit('submit', 'Post Article'); ?>

Controller (from site.php):

function create_article()
{
    $this->load->model('site_model');
    $this->load->helper('form');

    $post_check = $this->input->post('submit');

    if ($post_check === TRUE)
    {
        $this->site_model->create_article($post_check);
        $this->load->view('created');
    }
    else
    {
        $this->load->view('create_form');
    }
}

Model:

function create_article($post_check)
{
    $this->load->helper('date');

    $data = array(
                  'title' => $post_check['title'],
                  'text' => $post_check['text'],
                  'created' => now()
                 );

    $this->db->insert('articles', $data);
}

When I submit the form it just reloads "create_article.php" (which contains the form) rather than the confirmation page "created.php". Presumably $post_check is not getting any data passed to it, but I'm not sure why since refreshing the page post-submission triggers a POST data notification - something's definitely getting though! Any suggestions welcome.

Était-ce utile?

La solution

 form_submit('submit', 'Post Article');
// Would produce:

 <input type="submit" name="submit" value="Post Article" />


 $post_check = $this->input->post('submit');


 if ($post_check == 'Post Article')
  {
    $this->site_model->create_article($post_check);
    $this->load->view('created');
  }
   else
  {
    $this->load->view('create_form');
   }

Autres conseils

$post_check = $this->input->post('submit'); // THIS return array()

$post_check === TRUE; // THIS IS INVALID !!! array() !== TRUE !!

// You should do 

if ( count($post_check) ){ }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top