I am using CodeIgniter to pass some parameters to my PHP page through $_POST request, and in the PHP page I am reading.
$foo = $this->input->post('myParam');
If the myParam parameter is present in the $_POST request, then $foo will be assigned the myParam value. How do I check if myParam is not passed in the $_POST request?
I Googled 'codeigniter input post'.
First result is this.
From that document:
$this->input->post('some_data');
The function returns FALSE (boolean) if the item you are attempting to
retrieve does not exist.
So you need to do:
if ($foo===false) {
// do something if it's not set
}
I think the best way to do this would be to use the form validation class to do pre-processing on your data. This is documented here.
You'd do something like:
function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('myParam', 'myParam', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
If your validation fails it will send you back to the form and you'll have to re-populate it with data, there is a way to do this (see the doc). If it passes you can then be sure that $this->input->post('myParam'); will return a value.
Related
Hello all i was using form_validation Library in codeigniter inside my application. I am making a password retrieving function. I have made a submit form of the email . Now, on the email input field i want to apply these validations.
required
valid_email
check email exist or not.
For the 3rd one i am using call back function to check the database and it worked fine. But with the call back function valid_email is not functioning. This is my controller functions.
public function password_retrieve()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback__email_exists');
if ($this->form_validation->run() == false) {
$this->load->view('login_header');
$this->load->view('password_retrieve');
$this->load->view('login_footer');
} else {
}
} else {
$this->load->view('login_header');
$this->load->view('password_retrieve');
$this->load->view('login_footer');
}
}
function _email_exists($email)
{
$this->load->model('users_model');
$result = $result = $this->users_model->check_email_is_exist($email);
if (!$result) {
$this->form_validation->set_message(__FUNCTION__, 'This %s address does not exist!');
return false;
} else {
return true;
}
}
It should checked the valid_email rather than the going to the callback function.
In other mean i want to know what is the order of the rules. Is callback rule runs before the valid_email?
Try to remove "trim" and for check if email exist don't use another function. But use "is_unique[table_name.email]".
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[table_name.email]');
By Searching the official documentation and git repository i have found out that there is not a particular order in which a function will run in codeigniter. This means $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback__email_exists');
In this case valid_email will run after the callback__email_exists. There is not a order in which first trim, then required and then valid_email will run.
So what i have done is to make a new function in my callback function which runs after the required but before the check_email.
Answering this question so that in future people can get the benefit from it. Cheers!
I just wondered if there is a very easy way to determine whether the request is a $_POST or a $_GET request.
So does Codeigniter have something like this?
$this->container->isGet();
I've never used codeigniter, but for this I check the $_SERVER['REQUEST_METHOD'].
Looking at the docs maybe something like:
if ($this->input->server('REQUEST_METHOD') === 'GET') {
//its a get
} elseif ($this->input->server('REQUEST_METHOD') === 'POST') {
//its a post
}
If you're going to use it a lot then it's simple to roll your own isGet() function for it.
For CodeIgniter 3 users: the docs state the input class has a function to get the request method:
echo $this->input->method(TRUE); // Outputs: POST
echo $this->input->method(FALSE); // Outputs: post
echo $this->input->method(); // Outputs: post
In codeigniter 4:
$this->request->getMethod() // Returns get, post, or whatever the method is
The correct answer is:
if ($this->input->post()) {
//have post
}
I'm relatively new to CodeIgniter and this may be an obvious answer, but I am unable to find anything in the documentation.
I know how to use form_validation->run() to validate a form and run some code when it is submitted successfully. What if I wanted to do something with a form that did not require any sort of validation? Is there a form->run() equivalent that returns true when a user submits the form? Something like:
page.php
public function update($id)
{
$this->load->helper('form');
if($this->form->run())
{
// do some stuff after user has submitted form
}
}
I don't think there exists such method, but you can do it manually
For example:
if ( ! isset($_POST['something'])) // OR $this->input->post('something');
{
return false //maybe?
}
else
{
//$something = $_POST['something'];
return true //maybe?
}
A statement like this should work. You just need to check if any post data exists.
if ($_POST)
{
// do something
}
How do I get the CodeIgniter form validation to validate the $_SESSION if there is no passed form data? I tried manually setting the $_REQUEST variable, but it doesn't seem to work.
i.e. I have a function search in the controller which validates the form input passed, and either returns you to the previous page with errors, or else moves you onto the next page. But I want this function to also work if you previously filled out this page, and the info is stored in the $_SESSION variable.
function search () {
$this->load->library("form_validation");
$this->form_validation->set_rules("flightID", "Flight Time", "required|callback_validFlightID");
$this->form_validation->set_rules("time", "Flight Time", "required|callback_validFlightTime");
$this->setRequest(array("flightID", "time"));
// adding session check allows for inter-view navigation
if ($this->form_validation->run()) {
// some application logic here
$this->load->view("seats", $data);
} else {
$this->logger->log($_REQUEST, "request");
// redirect back to index
$this->index();
}
}
function setRequest () {
// make sure none of the parameters are set in the request
foreach ($vars as $k) {
if (isset($_REQUEST[$k])) {
return;
}
}
foreach ($vars as $k) {
if (isset($_SESSION[$k])) {
$_REQUEST[$k] = $_SESSION[$k];
}
}
}
You can store the form post info in a session using the following codeigniter functions
$formdata = array(
'flightID' => $this->input->post('flightID'),
'time' => $this->input->post('time')
);
$this->session->set_userdata($formdata);
and the information can be retrieved with the following
$this->session->userdata('flightID')
$this->session->userdata('time')
form_validation works directly with $_POST, so use that instead of $_REQUEST.
What you're trying to do is setting Post values manually which is not natively
supported by CodeIgniter. So what we're doing first is extending the core.
Create a new file (MY_Input.php) and paste the following contents into it:
class MY_Input extends CI_Input{
function set_post($key, $value)
{
$_POST[$key] = $value;
}
}
That's a very basic implementation of your purpose but it's enough to test around. You might want to extend it to make it fit your needs (f.e. allowing the input of arrays).
So now. In your controller you can check if something has been posted by a user. If not you'll be just setting the post variable manually with your new method of the Input class.
class Some_Controller extends CI_Controller{
public function index()
{
// The user hasn't filled out a field?
if(!$this->input->post('a_key'))
{
// Let's set the postfield to the value of a session key
$this->input->set_post('a_key', $this->session->userdata('mystoredkey'));
}
}
}
After having set your postfield manually, it can be handled by the form validation library as it is meant to be.
That should be your way to go :)
You can really do some pretty things if you're not afraid of hacking the core. Many people are, don't be one of them!
Happy coding
New to CodeIgniter and new to Ocular so bear with me.
I used to code in the following way when running form validation (where the index() method contains the form loading code):
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->load->view('view_name', $data);
}
However I'm now trying to use the Ocular Template Library and the above code no longer works see example below:
if ($this->form_validation->run() == FALSE)
{
$this->index();
}
else
{
$this->template->set($data);
$this->template->render();
}
The above code does not run through the index method as it used to without Ocular and I was wondering what the best practice is to correct this or even if my previous code was best practice?
Regards,
Numan1617
Sometimes it's hard to determine best practice with Codeigniter because it's convention-less nature, so all I can really tell you is how I've found best in my experience...
I assume you're form view is being served up via index() and then you're submitting your form to this (seperate) controller function that validates and processes the form data, and re-displays the view with errors if there was a problem...
I would clean this up by consolidating it all into a single function...
public function form()
{
//if this is a post request
if ($_POST)
{
//check the validation
if ($this->form_validation->run())
{
//process it
//and then redirect if you want to send to a "success" page
redirect('uri/to/success');
}
else
{
//load up $data values to re-display form
}
}
//load up any $data values needed for standard view
$this->load->view('view', $data);
// or $this->template stuff...
}
It always seemed to me to be a bad route to go down calling controller functions internally, ex. $this->index()