1

I am using codeigniter and the tutorial from here. I have made a basic blog tool which works fine. However as it stands to add a new post you have to go to a separate page 'create.php' to get to the form. I would like to try and put the form on the same page as the page that will be updated i.e. 'index.php'. If I try to do this at the moment the form simply refreshes and does submit the data.

model

function insert_post($data){
    $this->db->insert('posts', $data);
    return;
}

Current View (admin/create.php)

<?php echo validation_errors(); ?>

<h4>Create A New Post Below</h4>
<form action="" method="post" >
<p>Title:</p>
<input type="text" name="title" size="50"/><br/>       
<p>Summary:</p>
<textarea name="summary" rows="2" cols="50"></textarea><br/>
<p>Post Content:</p>
<textarea name="content" rows="6" cols="50"></textarea><br/>
<input type="submit" value="Save" />

<?php echo anchor('admin','Cancel'); ?>

</form>

View I would like the form to be on (index.php)

<?php
    echo '<p>Welcome '.$username.'! All posts available for edit or deletion is listed below.</p><br/>';

    echo anchor('admin/create','Create New Post');

    $count = count($post['id']);
    for ($i=0;$i<$count;$i++)
    {
        echo '<div class="postDiv">';
        echo '<h4>'.$post['title'][$i];
        echo '<p>'.$post['summary'][$i].'</p>';
        echo '<p>'.$post['content'][$i].'</p>';
        //echo anchor('blog/view/'.$post['id'][$i],' [view]');
        echo anchor('admin/edit/'.$post['id'][$i],' [edit]');
        echo anchor('admin/delete/'.$post['id'][$i],' [delete]</h4>');
        echo '</div>';
    }
?>

Controller

function create(){
    $data['userId'] = $this->tank_auth->get_user_id();
    $data['username'] = $this->tank_auth->get_username();

    $this->form_validation->set_rules('title','title','required');
    $this->form_validation->set_rules('summary','summary','required');
    $this->form_validation->set_rules('content','content','required');

    if($this->form_validation->run()==FALSE)
    {
        $this->load->view('template/admin_html_head',$data);
        $this->load->view('admin/create',$data);
        $this->load->view('template/html_tail',$data);
    } else {
        $data = $_POST;
        $this->posts->insert_post($data);
        redirect('admin');
    }
}

This was straight forward when I used normal php but with codeigniter I am getting lost with the MVC stuff. I know this is probably a fairly basic question so please either explain your answer or give me a link to something which will explain what I need to do as I want to learn from this. I have read the codeigniter docs on validation but I dont think thats my problem?

Kumar V
  • 8,810
  • 9
  • 39
  • 58
EnduroDave
  • 1,013
  • 7
  • 18
  • 37

3 Answers3

2

What you are trying to do is called embedding a view. I will try to explain how but you should also check some links which might prove to be more in depth:

http://net.tutsplus.com/tutorials/php/an-introduction-to-views-templating-in-codeigniter/

Codeigniter: Best way to structure partial views

The crux of what you need to do is change the link on index.php from:

echo anchor('admin/create','Create New Post');

to

$this->load->view('admin/create');

Now this should work, but to help you on the MVC front, it helps to explain why doing it this way is wrong. The idea of MVC is to seperate the functions in your application into their distinct roles. Most people will frown at putting business logic into views unless it is very minimal. The way that we could improve upon your code is to load the view in the controller, and set it to variable.

At the bottom of the codeigniter docs for views it shows how to load into a variable:

http://ellislab.com/codeigniter/user-guide/general/views.html

if the third parameter of load->view is set to true then the function will return your view as a string instead of outputting it to the browser

$data['input_form'] = $this->load->view('admin/create', $data, true);

then in the view that you want to load that form all you need to do is echo input_form

<?php echo $input_form;?>

So that should solve your problem but there are also a few more things you can do in your view file that will improve the readability of your code.

Instead of using a count() and for loop you can use foreach which makes everything much easier

<?php foreach ($post as $post_item):?>
<div>
<h4><?php echo $post_item['title'];?></h4>
</div>
<?php endforeach;?>

It also helps to break your view files up and have more tags. It might seems like it is extra bloat, but when you have larger view files it will be very cumbersome to continue using as many echo's as you have

Community
  • 1
  • 1
David Duncan
  • 1,858
  • 17
  • 21
  • Thanks for the detailed response. The links were very informative, thank you. I guess I need to go away and do some more reading but unfortunately I can't get this to work. If I try the first method you mention (as its least likely for me to screw up) I can see the form but again I have the problem that the form does not submit any data to the database. The same happens if I used the second method that you mention. Could I still be doing something wrong? – EnduroDave Dec 10 '13 at 13:19
  • So I have made this work in a backwards sort of way using using your instruction as guidance. If I tried to put the form on the index page then the form was not submitting because as it stands the index page was not calling the insert_post function. I have put the index page at the bottom of the create page and now everything works. Its not what I wanted but I now understand whats going on so I think I should be able to sort this out. Thanks for your help! – EnduroDave Dec 10 '13 at 19:59
0

just add one method uri_string() in your form action, uri_string will take same url of page put in action you can submit form to same page

<?php echo validation_errors(); ?>

<h4>Create A New Post Below</h4>
<form action="<?=uri_string()?>" method="post" >
<p>Title:</p>
<input type="text" name="title" size="50"/><br/>       
<p>Summary:</p>
<textarea name="summary" rows="2" cols="50"></textarea><br/>
<p>Post Content:</p>
<textarea name="content" rows="6" cols="50"></textarea><br/>
<input type="submit" value="Save" />

<?php echo anchor('admin','Cancel'); ?>

</form>

in controller little chagnes

function create(){
    $data['userId'] = $this->tank_auth->get_user_id();
    $data['username'] = $this->tank_auth->get_username();

    $this->form_validation->set_rules('title','title','required');
    $this->form_validation->set_rules('summary','summary','required');
    $this->form_validation->set_rules('content','content','required');

    if($this->form_validation->run()==FALSE)
    {
        $this->load->view('template/admin_html_head',$data);
        $this->load->view('admin/create',$data);
        $this->load->view('template/html_tail',$data);
    } else {
        $data = $this->input->post();
        $this->posts->insert_post($data);
        redirect('admin');
    }
}
umefarooq
  • 4,540
  • 1
  • 29
  • 38
  • thanks for the answer. I tried this but I get the same result i.e. nothing happens upon submitting the form nothing happens – EnduroDave Dec 10 '13 at 19:23
0

Use session library

check this another stackoverflow thread to know how to use session

In order to use session library, u need to configure encryption_key in config.php

To do that, check this out

Community
  • 1
  • 1
Yogi
  • 1,527
  • 15
  • 21