Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 864425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:28:32+00:00 2026-05-15T09:28:32+00:00

Ok, I’m trying to validate a form in codeigniter as a first step I

  • 0

Ok, I’m trying to validate a form in codeigniter

as a first step I want to make all fields in my form required to make an entry…
but I can’t make it work

here is the code:

myBlog.php

    <?php

    class MyBlog extends Controller{


       function MyBlog(){
           parent::Controller();
           $this->load->helper('url'); //here we load a url class that we use later
           $this->load->helper('form');// here we load form class


           $this->load->scaffolding('entries');  //scaffolfing is a feature that lets you add or remove elements from the database



            $this->load->library('form_validation');//load validation class used to validate our forms...
       }

      function index(){

          $data['title'] = "My Blog Title"; //the title of my blog
          $data['query'] = $this->db->get('entries'); //here we make a small query to entries table


          $this->load->view('myBlog_view', $data); ///load all data variables on myBlog_view.php
         //this is also for the form validation


            $this->form_validation->set_rules('title', 'Title', 'required');
            $this->form_validation->set_rules('body', 'Body', 'required');
            $this->form_validation->set_rules('author', 'Author', 'required');





          if ($this->form_validation->run() == FALSE)
            {
                //$this->load->view('myBlog_view');
            }
            else
            {
                $this->load->view('formSuccess_view');
            }

 }






     function myBlog_insert(){

           $this->db->insert('entries', $_POST);

           redirect('myBlog/');
           }


    } 

    ?>

also myBlog_view.php file:

<html>
<head>

<title><?php echo $title; ?></title>
</head>
<body>




<?php foreach($query->result() as $row): ?>
 <div class='curvebox'>
<h3><?=$row->title?></h3>


<p class="bodyText"><?=$row->body?></p>
<div class="author"><?="by: ".$row->author." on ".date("D d M Y h:i:s A", strtotime($row->date_time))?></div>
<p class="comments"><?=anchor('myBlog/comments/'.$row->id, 'Comments');?></p>
</div>


<?php endforeach; ?>



<div class="theForm">

<?php echo $this->form_validation->error_string; ?>

<?=form_open('myBlog/myBlog_insert');?>


<label for="title">Title:</label>

<input type='text' name="title" size="40" id="title" />
<p>
<label for="body">Body:</label>
<textarea name="body" rows = "10" cols="60" id="body"></textarea>
</p>
<p>
<label for="author">Author:</label>
<input type="text" name="author" size="40" id="author"/>
</p>
<p><input type="submit" value="Submit New Post"/></p>
</form>
</div>
</body>
</html>

everything works fine except the fact that it would accept entries with by a form half-filled

sorry for the long code….
any help would be appreciated

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:28:33+00:00Added an answer on May 15, 2026 at 9:28 am

    You want to have your form post to itself. Right now it’s skipping the validation.

    All of your validation functions are in the index method.
    Change this line in myBlog_view.php:

    <?=form_open('myBlog/myBlog_insert');?>

    to:

    <?=form_open('myBlog');?>

    And then you’ll need to call the insert method after the validation, so in your controller, before this line: $this->load->view('formSuccess_view');

    you’ll need to add in this:

    $this->myBlog_insert();

    See if that works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 446k
  • Answers 446k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The solution is to add runAllManagedModulesForAllRequests="true" to the configuration>system.webServer>modules tag… May 15, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer Bad The below code catches an exception that could easily… May 15, 2026 at 7:08 pm
  • Editorial Team
    Editorial Team added an answer The best solution is to make the classes you want… May 15, 2026 at 7:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.