Codeigniter doesn't take value from input - php

I have a problem with codeigniter:
I have in my view
<input type="text" name="code" style="width:310px;">
My controller
$code = $this->input->get('code');
and after i call a function from my models:
$this->model->add($code);
The problem is codeigniter don't take value in controller(i make a test with $this->model->add('342432'); and it's works. So i think the problem is between view and controller.
Has anybody a solution? Thanks!.

It depends upon form submit method.
From your code: $code = $this->input->get('code'); it is get.
If you want form with post method, use
$code = $this->input->post('code');

If you are using get method in Codeignitor make your
$config['allow_get_array'] = TRUE;
In your config.php file
$config['allow_get_array'] is FALSE(default is TRUE), destroys the global GET array.

In View
<form action="<?php echo base_url()?>/conteroller/method" method="post">
<input type="text" name="code" style="width:310px;">
<input type="submit" name="submit" >
</form>
In conteroller
if(isset($_POST['submit']))
{
$code = $_POST['code'];
$result = $this->model_name->add($code);
print_r($result);//to check the output
}
Note :
to use base_url() function, in config/autoload.php
$autoload['helper'] = array('url');
In conteroller load model in __construct
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('Model_name');//Ex: Product_Model
}

Related

Codeigniter form validation return always false

In my codeigniter controller function call $this->form_validation->run(), that return always false, and my validation_errors() not showing error, probably because not receive datas in post method...
my controller
class Reminder extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('reminder_model');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->library('email');
$this->load->library('session');
if(!$this->session->auth_ok) {
redirect('auth/login');
}
}
public function index(){
$data['title'] = 'Reminder';
$data['section'] = 'reminder';
$data['reminders'] = $this->reminder_model->getReminders();
$data['operatori'] = $this->reminder_model->getOperators();
$this->form_validation->set_rules('selectUser','selectUser', '');
if($this->form_validation->run() === FALSE) {
$this->load->view('common/header2', $data);
$this->load->view('reminder/index', $data);
$this->load->view('common/footerReminder');
echo validation_errors();
}else{
echo "<pre>";
print_r($this->input->post());
die();
}
}
my view
<?php echo form_open('reminder/index'); ?>
<div class="form-group">
<label for="selectUser" style=" width: 30%">Utente: </label>
<select class="form-control" name="selectUser" id="selectUser" style="width: 30%">
<?php foreach($operatori as $operatore): ?>
<option value="<?php echo $operatore['ID']?>" <?php echo $r = ($operatore['ID']==$this->session->auth_user['ID']) ? 'selected' : '' ?>><?php echo $operatore['nome']." ".$operatore['cognome'] ?></option>
<?php endforeach; ?>
</select>
</div>
<button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> View</button>
<?php echo form_close(); ?>
In order to get the entire $_POST array using CodeIgniters built-in methods, you have to set the first parameter as NULL and the second parameter as TRUE
Like this:
$this->input->post(NULL, TRUE);
Also, you have not set any rules for validation..
In CodeIgniter, you set rules in the third parameter of the set_rules method within the form_validation object.
Like this:
$this->form_validation->set_rules($FIELD_NAME, $FIELD_NAME(for error messages), $RULES);
You would substitute the first $FIELD_NAME with the value of the name attribute on the HTML element you are looking to validate.
You would substitute the second $FIELD_NAME with the name you would like to use for the field when displaying an error message to the user.
You would substitute $RULES with the validation rules such as: 'required|min_length[#]|max_length[#]'
Hope this helps!
If you are not setting rules (which makes it rather pointless to use $this->form_validation->set_rules()) the form validation will fail as it's missing a required parameter.
If you don't need to validate a field, don't set a rule.
Try updating your set_rules instruction to $this->form_validation->set_rules('selectUser','selectUser', 'required'); to see if it behaves correctly. You can verify by filling something in the form (validation will pass) or leaving the field blank (validation will fail)
Just remember, if you won't set at least one validation rule for a field, don't instantiate the set_rules method for that field

Insert Data into Database and display it with CodeIgniter

I am new to CodeIgniter
I have the following code in my Model:
public function saveVar() {
$variable_limit=$this->input->post('var');
$data = array(
'variable_limit'=>'$variable_limit'
);
$this->db->insert('mytable',$data);
}
this should fetch Data from html form and insert a row 'variable_limit' into the table 'mytable', the code doent work I see no change in the mysql database.
Assumed that the first code works, I want to display this data to the view, I wrote this Code in my Controller:
function postVar(){
$this->load->model('tool');
$this->tool->saveVar();
}
I am trying to transfer the data to the model calling 'tool' by calling the saveVar() which is implemented in my model, does this Code work?
Edit: this is the Code in my View:
<form id="myform" action="<?php echo base_url()."tools/file";?>" method="post" onsubmit="return validateData();">
<div> Variable : <input type="number" id="var" name="var" value="<?php echo isset($_POST['var']) ? $_POST['var'] : '' ?>" /></div>
Thanks, Elmoud
Do like this:-
Controller:-
public function postVar(){
$variable_limit=$this->input->post('var');
$data = array(
'variable_limit'=>$variable_limit
);
$this->tool->saveVar($data);
}
Model:-
public function saveVar($data) {
$this->load->model('tool');
$this->db->insert('mytable',$data);
}
Controller:
function postVar(){
$this->load->model('tool');
$this->tool->saveVar();
}
Model
public function saveVar() {
$variable_limit=$this->input->post('var');
$data = array(
'variable_limit'=>$variable_limit
);
$this->db->insert('mytable',$data);
}
It need not use quotes for variables.Just use $variable_limitas such.
Controller:
function postVar(){
$this->load->model('tool');
$this->tool->saveVar();
}
Model:
public function saveVar() {
$data = array(
'variable_limit'=>$this->input->post('var')
);
$this->db->insert('mytable',$data);
}

Codeigniter: change URL after loading a View

I am working on a project which deals with lots of forms. I searched and tried the search results, but none of them worked.
My code view code "uprofile.php" is as follows:
<form class="form-horizontal" method="post" action='<?php echo base_url() . "home/addnewagency" ?>'>
<div class="form-group">
<label for="company" class="col-sm-3 control-label">New Agency Name:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="newagency" name="newagency" placeholder="Plase Enter New Agency Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</div>
</form>
My Controller "home.php" code is as follows:
public function addnewagency() {
$newagency = $this->input->post('newagency');
$this->dis_model->newagency_model(strtoupper($newagency));
$this->loadprofile();
}
public function loadprofile() {
$data['inscompanyname'] = $this->dis_model->getcompany();
$data['agentname'] = $this->dis_model->getagent();
$data['agencyname'] = $this->dis_model->getagency();
$data['clientname'] = $this->dis_model->getclient();
$data['deptname'] = $this->dis_model->getdept();
$this->load->view('uprofile', $data);
}
when I submit the form, the control is passed to addnewagency() data gets inserted in the database and the URL is http://localhost/dis/home/addnewagency. After getting back to the calling function, it goes to loadprofile() and loads the view uprofile. But, the URL still remains the same. But, I want it as http://localhost/dis/home/login and also want to pass the data. Does anyone have any idea regarding how to achieve this?
All positive suggestion are welcomed...
Thanks in advance...
Now you are calling $this->loadprofile() within addnewagency(). If you want to change URL, you need to use redirect() function of CodeIgniter.
Replace
$this->loadprofile()
with below line of code and it will work for you.
redirect(base_url() . 'home/loadprofile');
if you want to change the url use redirect(base_url() . 'home/loadprofile'); in your controller addnewagency(); as suggested by others and refer this question to know whether you can send data in redirect function or not,
Sending data along with a redirect in CodeIgniter
I found a alternative option to get this done.
You can't change URL string with $this->load->view function but you can redirect to target controller function using redirect method. Before you call redirect, set flash session data using $this->session->set_flashdata that will only be available for the next request, and is then automatically cleared. You can read it from official codeiginter documentation .
Example :
class Login extends CI_Controller {
public function index()
{
if ($this->session->login_temp_data) {
$data = $this->session->login_temp_data;
} else {
$data = array();
$data['name'] = 'Guest' ;
}
$this->load->view('login', $data);
}
public function get_name()
{
$data = [];
$data['name'] = 'John Doe';
$this->session->set_flashdata('login_temp_data', $data);
redirect("login");
}
}
You want to use the redirect() function
At the bottom of your addnewagency() method like so:
public function addnewagency() {
$newagency = $this->input->post('newagency');
$this->dis_model->newagency_model(strtoupper($newagency));
redirect(base_url() . 'home/loadprofile');
}

form in codeigniter not posting

I am creating a form in codeignitor and every time I try to submit something the page does nothing in chrome or in firefox gives me this message:
The address wasn't understood
Firefox doesn't know how to open this address, because one of the
following protocols (localhost) isn't associated with any program or
is not allowed in this context.
You might need to install other software to open this address.
In internet explorer it trys to find an application to open the page.
I can access the same address directly but it won't let me do it when I submit the form.
this is the code for the form:
<?php
$hidden = array('account_id' => '1');
echo form_open('post', '', $hidden);
?>
<label for="post">Post:</label>
<input type="text" name="post" id="post"/>
<br/>
<input type="submit" value="post" />
</form>
This is the post controller:
<?php
class Post extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('posts_model');
$this->load->helper('url_helper');
$this->load->helper('form');
}
function index() {
$data['title'] = "posted";
$this->posts_model->add_post();
$this->load->view('templates/header', $data);
$this->load->view('comment/index', $data);
$this->load->view('templates/footer');
}
}
?>
This is the function in the posts_model:
function add_post() {
$data = array('person_acc_id' => $this-> input -> post('account_id'),
'post' => $this -> input -> post('post'),
'deleted' => 0,
'edited' => 0,
'post_time' => date('Y-m-d H:i:s', time()));
$this -> db -> insert('post', $data);
}
Actually you didn't set any method to form. Means your form action is Wrong.
In your function index() you call the view. So Then its load from in that. So if I click form submit <form> come to POST Controller. So it again execute function index() again. This will run like loop til you fix it.
So what you have to do is.
In controller Create new method to receive data
public function validate_form()
{
#your Form validate Code goes here
}
In view <form> should be
echo form_open('post/validate_form', '', $hidden);
this will act like
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/post/validate_form" account_id="1" />
To Know about Form Validation
Just check your config.php in application/config, then change your link like this:
$config['base_url'] = 'http://localhost/yourApp/';
I guess your value before is like this:
$config['base_url'] = 'localhost/yourApp/';
My code was actually fine. Once I put it on a real server it works. It would not work with localhost.

How to use jQuery.validationEngine in ajax field validation with php?

I am using CodeIgniter framework with jQuery.validationEngine.
In my controller:
public function check_email() {
$email = get_post('fieldValue');
$is_unique = User_Model::is_email_unique($email);
$to_js = array(
get_post('fieldId'),
$is_unique
);
echo json_encode($to_js);
}
In my view:
<script>
$(function() {
$('#form_id').validationEngine('attach');
});
</script>
...
...
<input id="email" class="validate[required,custom[email],ajax[check_email_unique]]" type="text" value="" name="email">
But it doesn't work. I don't know where should I put the URL (ie. http://localhost/user/check_email)
I didn't work with validation engine but if your problem is to addressing, do like this:
http://localhost/yourprojectname/index.php/checkMail
If you omitted the index.php using rewriting so just remove it from the URL and of course your default controller must be set as the controller of the checkMail function.

Categories