Cannot post radio value in Codeigniter - php

view:
<?php
$attr_form = array('class' => 'form-horizontal');
echo form_open('login/create', $attr_form); ?>
<div class="form-group">
<label class="col-sm-4 control-label">Gender</label>
<div class="col-sm-4">
<?php echo form_radio('gender', 'M'); ?>
<?php echo form_radio('gender', 'F'); ?>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<?php
$data_submit = array('class'=> 'btn btn-info', 'name' => 'submit', 'value'=> 'Create Account');
echo form_submit($data_submit);?>
</div>
</div>
controllers:
function create ()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('gender', 'Gender', 'required');
if($this->form_validation->run() == FALSE)
{
$this->signup();
}
else
{
$this->load->model('parents_model');
if($query = $this->parents_model->create_member())
{
$data['main_content'] = 'successful';
$this->load->view('include/template', $data);
}
else
{
$this->signup();
}
}
}
model:
function create_member ()
{
$new_parents = array(
'gender' => $this->input->post('gender')
);
$insert = $this->db->insert('parents', $new_parents);
return $insert;
}
I just simplified the other form inputs.
Can anyone figure out whats wrong with my code? It cannot go to successful page.
Or do I use wrong form_radio code ?
I have tried many times! Please help me....

You have a syntax error.
$this->input->post->('gender')
should be:
$this->input->post('gender')

try this
$gender = $this->input->post('gender')['M'] ? 'M':'F';
$new_parents = array('gender' => $gender);

After I restarted my localhost, everything works again.
Cause I have created a column in the table without restarting!

Related

How to call javascript function in callback function of codeigniter controller?

I want call the javascript function into callback function of codeigniter controller.
Controller: Callback function
public function email_exist($id) {
$this->db->where('email', $id);
$query = $this->db->get('login');
if (!$query->num_rows() > 0) {
$this->form_validation->set_message(__FUNCTION__, $this->email_validate());
return FALSE;
} else {
return TRUE;
}
Javascript: Alert function
function email_validate(){
alert('Invalid Email Id');
}
An uncaught Exception was encountered
Type: Error
Message: Call to undefined method Login::email_validate()
Filename: C:\wamp64\www\CodeIgniterProject1\application\controllers\login.php
Line Number: 46
login_form.php
<?php echo validation_errors(); ?>
<?php echo form_open_multipart(); ?>
<form name="form">
<div class="loginform">
<?php echo form_label('LOGIN', 'login'); ?>
</div>
<div class="email">
<?php echo form_input('email', 'E-MAIL', set_value('email')); ?>
</div>
<div class="password">
<?php echo form_password('password', 'PASSWORD', set_value('password')); ?>
</div>
<div class='checkbox'>
<?php
echo form_checkbox(array(
'name' => 'remember',
'id' => 'remember',
'value' => 'REMEMBER ME',
'checked' => FALSE,
'style' => 'margin:1px'
)) . form_label('REMEMBER ME', 'remember');
?>
</div>
<div class='btnlogin'>
<?php echo form_submit('login', 'LOGIN'); ?>
</div>
</form>
you cant do that in Codeigniter, use Codeigniter default form validation library
View here
if you want to add css for validation errors then try set_error_delimeters()
if you have bootstrap then use
alert alert-danger
class, else create one class
$this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
see
[other suggestions]
instead of putting all error in the top, you can also try
<?php
echo form_error('email');
form_input('email', 'E-MAIL', set_value('email'));
?>
other options is jquery validation or js validation, write js in view or external .js file
<?php
data = array(
'name' => 'username',
'id' => 'username', //use the id for jquery validation $('#username').on('blur', function() { });
'value' => 'johndoe',
'maxlength' => '100',
'size' => '50',
'style' => 'width:50%'
'onBlur' => 'email_validate();' //use this for javascript validation
);
echo form_input($data);
?>
im unable to comment, thats why posting as answer

CodeIgniter login page load and show login page again on local server but working on live server

When I login admin panel locally it redirects and load login page again, not open dashboard of my admin panel but when I am moving my file to live server admin panel works perfectly.
My Login page Code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div class="login-box-body" class="text-center" style="margin:0 auto;padding:30px;background:#f0f0f0;border-radius:10px 10px 0 0;">
<div style="margin:0 auto;" class="text-center">
<img src="<?php echo base_url('upload/images/logo.png'); ?>" style="width:300px;" class="text-center img-responsive;" title="<?php echo $title_lg; ?>" />
</div>
</div>
<div class="login-box-body">
<p class="login-box-msg"><?php echo lang('auth_sign_session'); ?></p>
<div class="text-danger"><?php echo $message;?></div>
<?php echo form_open('auth/login');?>
<div class="form-group has-feedback">
<?php echo form_input($identity);?>
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<?php echo form_input($password);?>
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<!-- <label>
<?php /*?> <?php echo form_checkbox('remember', '1', FALSE, 'id="remember"'); ?><?php echo lang('auth_remember_me'); ?><?php */?>
</label>-->
</div>
</div>
<div class="col-xs-4">
<?php /*?><?php echo form_submit('submit', lang('auth_login'), array('class' => 'btn btn-primary btn-block btn-flat'));?><?php */?>
<input type="submit" name="submit" value="Login" class="btn btn-primary btn-block btn-flat" style="background-color: #2f76bb;border-color: #2f76bb;">
</div>
</div>
<?php echo form_close();?>
above code is my login page code
My Authentication Code
class Auth extends MY_Controller {
function __construct()
{
parent::__construct();
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
$this->lang->load('auth');
}
function index()
{
if ( ! $this->ion_auth->logged_in())
{
redirect('auth/login', 'refresh');
}
else
{
redirect('/', 'refresh');
}
}
function login()
{
if ( ! $this->ion_auth->logged_in())
{
/* Load */
$this->load->config('admin/dp_config');
$this->load->config('common/dp_config');
/* Valid form */
$this->form_validation->set_rules('identity', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
/* Data */
$this->data['title'] = $this->config->item('title');
$this->data['title_lg'] = $this->config->item('title_lg');
$this->data['auth_social_network'] = $this->config->item('auth_social_network');
$this->data['forgot_password'] = $this->config->item('forgot_password');
$this->data['new_membership'] = $this->config->item('new_membership');
if ( $this->form_validation->run() == TRUE)
{
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
{
if ( ! $this->ion_auth->is_admin())
{
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/', 'refresh');
}
else
{
/* Data */
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
/* Load Template */
redirect('admin', 'refresh');
}
}
else
{
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('auth/login', 'refresh');
}
}
else
{
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['identity'] = array(
'name' => 'identity',
'id' => 'identity',
'type' => 'email',
'value' => $this->form_validation->set_value('identity'),
'class' => 'form-control',
'placeholder' => lang('auth_your_email')
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password',
'class' => 'form-control',
'placeholder' => lang('auth_your_password')
);
if($this->session->flashdata('message'))
{
$this->data['forgot_password'] = TRUE;
}
/* Load Template */
$this->template->auth_render('auth/login', $this->data);
}
}
else
{
redirect('/', 'refresh');
}
}
This is my Authentication Code.
Thanks For advance
Please ensure that the following are updated:
The PHP version (on the local server)
The Codeigniter version
The Ion_Auth library
Hope it helps :)

Codeigniter form validation rules order

I've set this rules on the controler
public function checkLogin()
{
$this->load->helper('form');
$this->load->library('form_validation');
$this->form_validation->set_rules(
'username',
'nombre de usuario',
'required',
array('required' => 'Debes introducir un %s.')
);
$this->form_validation->set_rules(
'pass',
'contraseña',
'required',
array('required' => 'Debes introducir una %s.')
);
$this->form_validation->set_rules(
'bad_login',
'login',
'callback_checkCredentials'
);
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('login/login_view');
$this->load->view('footer');
} else {
$this->session->set_userdata('logged',true);
$this->session->set_userdata('user',$this->input->post('username'));
redirect('main');
}
}
public function checkCredentials(){
if($this->input->post('username') == $this->user && $this->input->post('pass') == $this->pass){
return true;
}else{
$this->form_validation->set_message('checkCredentials', 'El usuario/contraseña introducido no es correcto');
return false;
}
}
and the view
<div class="container">
<div class="col-md-4">
<?php $this->load->helper('form'); ?>
<?php echo form_open('checkLogin'); ?>
<?php echo form_error('bad_login'); ?>
<?php echo form_error('username'); ?>
<div class="form-group">
<?php echo form_label('Usuario:'); ?>
<?php echo form_input(array('id' => 'username', 'name' => 'username', 'class'=>'form-control', 'placeholder'=>"Nombre de usuario")); ?>
</div>
<?php echo form_error('pass'); ?>
<div class="form-group">
<?php echo form_label('Contraseña:'); ?>
<?php echo form_password(array('id' => 'pass', 'name' => 'pass', 'class'=>'form-control', 'placeholder'=>"Contraseña")); ?>
</div>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Enviar', 'class'=>'btn btn-primary')); ?>
<?php echo form_close(); ?>
</div>
</div>
It's a login form with and input to the user and another to the password , but if the pass or the username are empty there is no need to check if pass and the username are correct(and show the message error), I'm not sure how to do that
You can simply do this:
$username = trim($this->input->post('usename'));
$password = trim($this->input->post('password'));
if(!empty($username) && !empty($password))
{
// do you validation
}
else
{
// maybe show error message
}

Add get parameter to URL, using form_open function?

I tried to implement standard PHP logic for add, edit articles to blog, so i have add method:
public function add()
{
$this->load->model('admin/Blog_Model');
if (!empty($this->input->post())) {
$user = $this->Blog_Model->addPost($this->input->post());
}
$this->getForm();
}
edit method:
public function edit($id = '')
{
$this->load->model('admin/Blog_Model');
if (!empty($this->input->post())) {
var_dump($id); exit;
$user = $this->Blog_Model->editPost($this->input->post(), $id);
}
$this->getForm($id);
}
and getForm method:
public function getForm($id = '')
{
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit';
} else {
$data['action'] = 'admin/blog/add';
}
$data['formTitle'] = array(
'name' => 'title',
'id' => 'content-title',
'value' => isset($post['title']) ? $post['title'] : '',
'placeholder' => 'Заглавие',
'class' => 'form-control'
);
$data['formContent'] = array(
'name' => 'content',
'id' => 'content-blog',
'value' => isset($post['content']) ? $post['content'] : '',
'placeholder' => 'Съдържание',
);
$data['formButton'] = array(
'type' => 'submit',
'content'=> 'Изпрати',
'class'=> 'btn btn-primary btn-block btn-flat'
);
$data['head'] = $this->load->view('admin/head', NULL, TRUE);
$data['left_column'] = $this->load->view('admin/left_column', NULL, TRUE);
$this->load->view('admin/header', $data);
$this->load->view('admin/blog_form', $data);
$this->load->view('admin/footer');
}
and blog_form view with form:
<div class="box-body pad">
<?php echo form_open($action); ?>
<div class="box-body">
<div class="form-group">
<label for="content-title">Заглавие:</label>
<?php echo form_input($formTitle); ?>
</div>
<div class="form-group">
<label for="content_blog">Съдържание:</label>
<?php echo form_textarea($formContent); ?>
</div>
<div class="col-xs-12 col-md-3 pull-right">
<?php echo form_button($formButton); ?>
</div>
</div>
<?php echo form_close(); ?>
</div>
So .. everything works perfect, but i have problem with this part:
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit';
} else {
$data['action'] = 'admin/blog/add';
}
if it's edit i want to send id like GET parameter. I think the problem is there than i does not use standard GET parameters instead of site_url function, when i show all articles here:
<?php foreach ($posts as $post) { ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['title']; ?></td>
<td><?php echo $post['content']; ?></td>
<td><div class="btn-group">
Редактирай
Изтрий
</div></td>
</tr>
<?php } ?>
Try to change
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit';
} else {
$data['action'] = 'admin/blog/add';
}
to
if (!empty($id)) {
$post = $this->Blog_Model->getPost($id);
$data['action'] = 'admin/blog/edit/'.$id; // pass $id to edit controller
} else {
$data['action'] = 'admin/blog/add';
}

Simple File Upload for FuelPHP

Does anyone have experience of uploading a series of files to a web server with FuelPHP?
My current setup adds content to a database from a Form, but I'd like to process images at this point too - so basically move them to my web server when submitting a form.
Is this simple to do?
I have my 'action_add()' method in my controller, but not sure how to update it to loop through all my file fields and move files.
public function action_add()
{
$val = Model_Article::validate('add_article');
if ($val->run())
{
$status = (Input::post('save_draft') ? 0 : 1);
if ( ! $val->input('category_id'))
{
$category_id = null;
}
else
{
$category_id = $val->validated('category_id');
}
$article = new Model_Article(array(
'user_id' => $this->user_id,
'category_id' => $category_id,
'title' => $val->validated('title'),
'body' => $val->validated('body'),
'published' => $status,
));
if ($article->save())
{
Session::set_flash('success', 'Article successfully added.');
}
else
{
Session::set_flash('error', 'Something went wrong, '.
'please try again!');
}
Response::redirect('articles/add');
}
$this->template->title = 'Add Article';
$this->template->content = View::forge('articles/add')
->set('categories', Model_Category::find('all'), false)
->set('val', Validation::instance('add_article'), false);
}
My Form:
<h2>Add an Article</h2>
<p>Publish a new article by filling the form below.</p>
<div class="options">
<div class="option">
<?php echo Html::anchor('articles', 'View Articles'); ?>
</div>
<div class="option">
<?php echo Html::anchor('categories/add', 'Add a Category'); ?>
</div>
</div>
<?php echo $val->show_errors(); ?>
<?php echo Form::open(array('enctype' => 'multipart/form-data')); ?>
<?php $select_categories = array(null => 'Uncategorized'); ?>
<?php foreach ($categories as $category): ?>
<?php $select_categories[$category->id] = $category->name; ?>
<?php endforeach; ?>
<div class="input select">
<?php echo Form::label('Category', 'category_id'); ?>
<?php echo Form::select('category_id', e($val->input('category_id')),
$select_categories); ?>
</div>
<div class="input text required">
<?php echo Form::label('Title', 'title'); ?>
<?php echo Form::input('title', e($val->input('title')),
array('size' => '30')); ?>
</div>
<div class="input textarea required">
<?php echo Form::label('Body', 'body'); ?>
<?php echo Form::textarea('body', e($val->input('body')),
array('rows' => 4, 'cols' => 40)); ?>
</div>
<div class="input textarea required">
<?php echo FORM::file('filename'); ?>
</div>
<div class="input submit">
<?php echo Form::submit('add_article', 'Publish'); ?>
<?php echo Form::submit('save_draft', 'Save Draft'); ?>
</div>
<?php echo Form::close(); ?>
Many thanks for any pointers.
Okay I can give you some instruction.
First fuelphp upload documentation
Hope it helps sorry if there are typos in it
public function action_add()
{
$val = Model_Article::validate('add_article'); //<-- maybe its just me but I never saw any similar to this in fuelphp sorry about this if I'm wrong
// if your form validation is okay than continue with everyhing else
if ($val->run())
{
$article = Model_Article::forge();
// Custom configuration for this upload
$config = array(
'path' => DOCROOT.DS.'foldername/tomove/your/images',
'randomize' => true,
'ext_whitelist' => array('img', 'jpg', 'jpeg', 'gif', 'png'),
);
Upload::process($config);
// if a valid file is passed than the function will save, or if its not empty
if (Upload::is_valid())
{
// save them according to the config
Upload::save();
//if you want to save to tha database lets grab the file name
$value = Upload::get_files();
$article->your_file_input_name = $value[0]['saved_as'];
}
$status = (Input::post('save_draft') ? 0 : 1);
if ( ! $val->input('category_id'))
{
$category_id = null;
}
else
{
$category_id = $val->validated('category_id');
}
$article->user_id = $this->user_id;
$article->category_i = $category_id;
$article->title = $val->validated('title');
$article->body = $val->validated('body');
$article->published = $status;
if ($article->save())
{
Session::set_flash('success', 'Article successfully added.');
}
else
{
Session::set_flash('error', 'Something went wrong, '.
'please try again!');
}
Response::redirect('articles/add');
}
$this->template->title = 'Add Article';
$this->template->content = View::forge('articles/add')
->set('categories', Model_Category::find('all'), false)
->set('val', Validation::instance('add_article'), false);
}

Categories