CodeIgniter Forums
Please help, loading a view from controller does not work!!! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Please help, loading a view from controller does not work!!! (/showthread.php?tid=11591)



Please help, loading a view from controller does not work!!! - El Forum - 09-15-2008

[eluser]smnirven[/eluser]
Hello all,

I am having a problem loading a view within one of my controllers. Basically, I have an insert function that is the handler for a form of mine. Inside the insert function I check that the posted values from the form are not null or blank. If I find a required field that is null or blank, I want to return to the form and pass it an error message to display on the form. Here is the code that is not working:
Code:
// First check required fields
if (!isset($_POST['noteSubject']) || $_POST['noteSubject'] == '')
{
    $sql = "INSERT INTO debug_info (text, timestamp) VALUES ('WTF1', NOW())";
    $this->db->query($sql);
    $data['error'] = "Subject is a required field";
    $this->load->view('create_note_view', $data);
}

$sql = "INSERT INTO debug_info (text, timestamp) VALUES ('WTF2', NOW())";
$this->db->query($sql);

When I submit the form without entering anything into the ‘noteSubject’ field, it never makes it back to the form (create_note_view) like I wanted. Also, when I look at my debug_info table, I see both the “WTF1” and “WTF2” message. Can anyone explain this and help me out how to fix this?

Thanks!


Please help, loading a view from controller does not work!!! - El Forum - 09-15-2008

[eluser]phpwebdev[/eluser]
Hi smnirven

Check this
http://ellislab.com/codeigniter/user-guide/libraries/validation.html


Please help, loading a view from controller does not work!!! - El Forum - 09-15-2008

[eluser]smnirven[/eluser]
Excellent, I wasn't aware of this validation library but I will try it now and hopefully it will solve my problem. I'm still curious though why the code I have doesn't work, could you maybe shed some light on that?

Thanks much!


Please help, loading a view from controller does not work!!! - El Forum - 09-15-2008

[eluser]smnirven[/eluser]
wow, this is one of those "I'm an idiot" moments. Turns out by encasing the validation in an if...else solved the problem. The validation library is also very slick and works just as I need it. Thanks for the heads up!


Please help, loading a view from controller does not work!!! - El Forum - 09-15-2008

[eluser]Tom Glover[/eluser]
It will continue to execute the rest of the function unless you tell it to return in that if not statement.


Please help, loading a view from controller does not work!!! - El Forum - 09-15-2008

[eluser]smnirven[/eluser]
Yep, just figured that out! Thanks for the help!