Checking the values of the form-codeigniter

Checking the values of the form-codeigniter


We need to ensure that all of the form fields have been filled in. We can do this by
simply using the empty()PHP function.
Before we do this, we want to assign the value of the form fields to variables.
This makes it easy for us by saving us from having to type out
$this->input->post('name')every time. Here's the code for this; it should be placed inside
the ifstatement where the comment // process data herewas.
$name = $this->input->post('name');
$email = $this->input->post('email');
$subject = $this->input->post('subject');
$message = $this->input->post('message');
With that out of the way, we can check to see if any of the fields were left blank, and
show an error if they were.
if(empty($name) OR empty($email) OR empty($subject) OR
empty($message))
{
show_404("The form submitted left fields blank, all fields are
required. Please go back and fill in all of the fields.");
}
Let me explain this code. What we do in the if statement is say "If the name is
empty, or the email is empty, or the subject is empty or the message is empty:
show this error". I've used ORin place of ||in this instance as it's more readable,
and is recommended by the CodeIgniter Style Guide.

Validate the e-mail
The next step that we need to take is to ensure that the email is correctly formatted.
The Email Helper gives us an easy solution. It contains a function that checks
whether a string is in the format email@domain.com. Here's how we check
the e-mail:
if(!valid_email($email))
{
show_404("The email address provided is not a valid email. Please go
back and fill in all of the fields.");
}

Related Posts:
  • Codeigniter count_all for pagination work on count_all condition.. you can use below method to find out total number of rows.. public function count_all() { $this->db->select ( 'COUNT(*) AS `numrows`' ); $this->db->where ( array ( … Read More
  • Checking the values of the form-codeigniter Checking the values of the form-codeigniter We need to ensure that all of the form fields have been filled in. We can do this by simply using the empty()PHP function. Before we do this, we want to assign the value of th… Read More
  • e-mail parameters-Codeigniter The next thing that we will need to do is to set our e-mail parameters, the sender, the recipient, any e-mail address to send a carbon copy or blind carbon copy to, the subject of our e-mail, and the message body. Take a l… Read More
  • CodeIgniter config.php The  config.php filecontains a series of configuration options all of them stored in a PHP array called, appropriately enough, $config) that CodeIgniter uses to keep track of your application ’ s  information and… Read More
  • codeigniter-Creating loops Creating loops in view files has been a stumbling block for a few developers. By passing a multidimensional array to a view file, you can easily establish a loop in any of your view files. Let's take a look at an example.<… Read More
  • what is CodeIgniter Helpers? Helpers, as their name implies, help you with specific tasks. Unlike libraries, helpers are not object -oriented but procedural in nature. Each helper contains one or more functions, each focusing on a specific task, with ze… Read More
  • Retrieving data-Codeigniter Retrieving POST data Toretrieve post data, you should use the function shown next. The first parameter is the name of the POST item that you are looking for.$this->input->post('some_field'); This function returns… Read More
  • CodeIgniter system Folder The system/ folder is where all the action happens. This folder contains all the CodeIgniter code of consequence, organized into various folders: application —  The   application foldercontains the applicat… Read More
  • Codeigniter Database Library The  Database librarycontains a series of helpful functions that make it easy for you to create and run queries and process the result sets from those queries. The first thing to note about the Database library in Co… Read More
  • CodeIgniter models All CodeIgniter models have the same initial structure: < ?phpclass Page_model extends Model{function Page_model(){parent::Model();}}? > The first function is the code that retrieves the home page < ?phpclass P… Read More
  • Top codeigniter interview question and answers codeigniter interview question  What is codeigniter? Codeigniter is open source , web application framework.Its is for building websites using php.Codeigniter is loosely based on MVC pattern.Most simple framework in ph… Read More
  • Mysql Join query Codeigniter Mysql Join query Codeigniter code loads and initializes the database class based on your configuration settings. $query = $this->db->query('SELECT name, title, email FROM my_table'); foreach ($query->result() … Read More
  • codeigniter routes page The  routes.php filelets you remap URI requests to specific controller functions. For example, you may have a controller named sitewith a function named index. The URI for this controller/function combination might be… Read More
  • Let's Make A Master Template codeigniter A simple CSS file, it ’ s time to create a generic Template view that you can reuse throughout your application. In this master template, which you can name template.php, are a series of embedded PHP calls that load certain … Read More