Welcome Guest, Not a member yet? Register   Sign In
Posting a form that is included in a page
#1

[eluser]JamieBarton[/eluser]
Hi guys,

Fairly new with CodeIgniter - hence the simple question (simple for most of you). Would appreciate it very much if you guys could give me some help.

I have a controller for my Dashboard. The index() of the Dashboard includes my header, footer, sidebar, and a general div for some static text. Also included is a 'wall' view.

The view is:
Code:
<h1>Wall Posts</h1>
    <p>Posts below have been made by your family:</p>
    
    &lt;?=form_open('wall/create')?&gt;
        
        &lt;?=form_error('message')?&gt;
        &lt;textarea name="message" rows="8" cols="40"&gt;&lt;?=set_value('message');?&gt;&lt;/textarea&gt;&lt;br />
        
        &lt;input type="submit" name="submit" value="Post it" id="submit" /&gt;
        
    &lt;?=form_close()?&gt;
    
    &lt;?php if ($recent_wall_posts->num_rows() > 0) { ?&gt;
    
        &lt;?php foreach($recent_wall_posts->result() as $p) {?&gt;

            <p>
                <strong>
                    <a >member_id?&gt;">&lt;?=$p->member_forename?&gt; &lt;?=$p->member_surname?&gt;</a>
                </strong>
                 &lt;?=$p->wallPost_message?&gt;
            </p>
            <p class="posted_on">
                Posted &lt;?=relative_time($p->wallPost_posted_on);?&gt;
            </p>

        &lt;?php } ?&gt;
    
    &lt;? } else { ?&gt;
        
        <strong>No wall posts to display</strong>
        
    &lt;? } ?&gt;

As you can see it posts to wall/create - Controller/Function.

The create function checks for validation and if it passes validation it will insert the form data into the Database.

How, if there is an error, it takes me to wall/create and shows the error. I would like it to remain at /dashboard controller only and not forward to wall/create.

What's the best way to doing a form, I'm thinking perhaps have another function that uses the result of 'create' to do something.

Could someone help me out here?


Thank you very much,

Jamie
#2

[eluser]Flemming[/eluser]
Hi Jamie,

there's no reason why your dashboard controller can't handle the posted form, in exactly the same way as your /wall/create controller does. It does mean code duplication but you can keep that to a minimum be reusing your views as much as possible. For instance, your controllers can set the form action so you can reuse the same form view in both wall/create and dashboard.

dashboard controller:
Code:
$data['form_action'] = '/dashboard';

dashboard view:
Code:
&lt;?=form_open($form_action)?&gt;

So your dashboard can include the form view (with the form action dynamically set by the controller, in this case to /dashboard) ... on initial page load and validation failure you can display the form along with the rest of the data from the dashboard just as you are currently doing. Just copy your validation rules into /dashboard. On success you can redirect wherever you want.

Alternatively you could use Javascript validation, so that the dashboard form won't get posted to the wall/create method until it has passed client-side validation.

Hope that helps!?!
#3

[eluser]JamieBarton[/eluser]
Hey Flemming,

Thanks for replying. I'm wanting to reduce code duplication much as possible. I'm new to this whole thing and even MVC.

In the end I want to be able to post the form using AJAX/jQuery and make it submit without refreshing the page. As for validation - I was thinking of disabling the submit button until the validation is met.

I'm just not 100% sure what goes in the M, V and C. Mainly the M and C.

Could you kindly elaborate more what would go in each to reduce less use of code. Without AJAX/jQuery. I thought in the /create function I could put in a redirect, but, it won't take the form validation errors back with it.


Regards,

Jamie
#4

[eluser]Flemming[/eluser]
Hi Jamie,

A quick and dirty summary of the MVC architecture:

Model - all your database queries

Contollers - logic, as much as possible of it

Views - HTML, with as little logic as possible ideally


A controller won't send validation messages through a redirect, which is why I suggested you post the dashboard form to the dashboard controller. If it fails validation then you WILL get the validation error data sent back to the view in validation_errors().

If you're using AJAX though, it doesn't matter where you post the data! So you can either start your AJAX work now (definitely use jQuery and it's AJAX or $post features) ... or you can spend a bit of time getting more familiar with CI and MVC first (sounds like a good idea) and then move on to AJAX.

Keep asking questions and I or someone else will be happy to answer them - but don't forget to search the forums too for answers if you think your questions are obvious/common ones!
#5

[eluser]JamieBarton[/eluser]
Thanks for that. I've always thought of MVC as View -> Controller -> Model, well in fact it's like View <- Controller -> Model.

I guess my question originally could be similar to this post. Look at the bottom of the post, the Fast Reply button. That fast reply box, where will the controller to submit a fast reply be? In the viewthread controller?


Regards,

Jamie
#6

[eluser]JamieBarton[/eluser]
Forgot to mention that when the form fails validation, i've included the form again, but it doesn't show the header i've included, it just shows the form that ive included.

What would be a way around that?
#7

[eluser]Flemming[/eluser]
If you post the form using AJAX you can post it to any controller you like. That controller will send back a success (or fail) status to Javascript. The path to the controller is irrelevant, but you might want to keep it in a logical place, like /wall/add

You might want to post your controller here so we can have a look?

here's a basic controller that handles a form submission. It might help you, I don't know?

Code:
&lt;?php

class login extends Controller {
    
    function __construct()
    {
        parent::Controller();
        $this->load->model('administrators_model');
        $this->load->library('form_validation');
    }

    function index()
    {
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');

        $this->form_validation->set_error_delimiters('<li>', '</li>');

        if($this->form_validation->run())
        {
            // validation passed, so do something with the data and then probably redirect
        }
        else
        {
            $this->load->view('header_view',$data);
            $this->load->view('login_view');
            $this->load->view('footer_view');
        }
    }
}
?&gt;
#8

[eluser]JamieBarton[/eluser]
Hi,

That helps a lot. Thanks for that, however, my dashboard will have multiple forms, for status update and wall post.

That's what had me struggling to see how to do that.

my controller: (I've moved the function from wall.php controller to the dashboard controller inside a function called post_to_wall() not sure if it was right to do that though.

Code:
&lt;?php

class Dashboard extends Controller {

    function Dashboard()
    {
        parent::Controller();
        $this->load->helper('date');
        $this->load->model("modelwall", "wall");
    }
    
    function index()
    {
        
        $data['recent_wall_posts'] = $this->wall->get_wall_posts(5);
        
        $this->load->view('header');
        $this->load->view('dashboard/wall', $data);
        $this->load->view('footer');
    }
    
    function post_to_wall()
    {
            $this->form_validation->set_rules('message', 'Message', 'trim|required|min_length[7]|xss_clean');
            
            if($this->form_validation->run() == TRUE) {
                
                $member_id = 1;
                $family_id = 1;
                $message = $this->input->post('message');
            
                $this->wall->insert_wall_post($family_id, $member_id, $message);
                
            } else {
                
                $data['recent_wall_posts'] = $this->wall->get_wall_posts(5);
                $this->load->view('dashboard/wall', $data);
                
            }
        
    }
    
}

/* End of file Dashboard.php */
/* Location: ./application/controllers/Dashboard.php */


Thanks again,

Also, seeing your profile - Your website - You're based in Gateshead, I live in Prudhoe, so not far from you. Small world!
#9

[eluser]JamieBarton[/eluser]
and my Model:

Code:
function insert_wall_post($family_id, $member_id, $message)
    {
        $data = array(
            'wallPost_family_id'    => $family_id,
            'wallPost_member_id'    => $member_id,
            'wallPost_message'        => $message
        );
        
        $this->db->insert('wallPosts', $data);
        return true;
    }
#10

[eluser]Flemming[/eluser]
Hi James,

yep small world indeed! Amazing!

Your model looks fine. For your controller I had meant that your index method would handle the posted form ... but I realise now that you're wanting to handle more than 1 form on a single view. Off the top of my head I think you're going to have to use AJAX to achieve that ... maybe someone else has other suggestions???

Or ... you can still have multiple forms posting to different controllers, just don't let them post until they've passed validation client-side. That would get around the problem.

Are you familiar with jQuery's AJAX features?




Theme © iAndrew 2016 - Forum software by © MyBB