Welcome Guest, Not a member yet? Register   Sign In
Please help, loading a view from controller does not work!!!
#1

[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!
#2

[eluser]phpwebdev[/eluser]
Hi smnirven

Check this
http://ellislab.com/codeigniter/user-gui...ation.html
#3

[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!
#4

[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!
#5

[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.
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB