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.");
}