how does one load a div using its id to a view?
this is my view
<div id="register" class="animate form">
<section class="login_content">
<form <?php echo form_open('User/register'); ?>
<h1>Create Account</h1>
<div class="inputlogin">
<div class="form-group">
how will i pass it to
if ($this->form_validation->run() === false) {
// validation not ok, send validation errors to the view
$index['title']="Welcome to Hoovie";
$index['mainContent']="login";
$index['results'] = $this->users_model->get_category();
$this->load->view('includes/redirect', $index, $data);
First, form_open tag makes <form> so you can't echo it inside of form it would look like <form <form>>
here is example of login form inside of view:
echo form_open('path/to/controller');
echo form_input(array(
'name' => 'username',
'class' => 'form-control input-lg',
'placeholder' => 'username'
));
echo form_password(array(
'name' => 'password',
'class' => 'form-control input-lg',
'placeholder' => 'password'
));
echo form_submit(array(
'name' => 'LoginSubmit',
'class' => 'btn btn-lg btn-info',
'value' => 'Login'
));
echo form_close();}
After that inside of your controller you have to activate form_validation library if it is not auto-loaded and then you have to set rules for your inputs
example :
$dugme = $this->input->post('LoginSubmit');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if(isset($dugme) && $this->form_validation->run()){
//if everything ok, do something
}
else {
//if validation did not pass load view
$this->load_view('login'); }
and after that inside of view you have to echo errors
//I am using bootstrap so alert-danger will show this div with red background
<?php echo validation_errors('<div class="alert alert-danger">','</div>');?>
you can check more about form validation here :
Codeigniter 3: https://www.codeigniter.com/userguide3/libraries/form_validation.html
Codeigniter 2: https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
Related
I have installed xampp with upgraded php 7.4.10 version because of the higher version installed captcha is not showing image showing blank (code is blank in white background).
enter image description here
Please find below screenshot of code
Javascript of html file login.phtml
if(document.frm_login.password.value!="")
{
var hash = CryptoJS.HmacSHA256(document.frm_login.password.value, "");
$('#passwords').text('');
document.frm_login.password.value=mixUpValues(hash.toString(),
document.frm_login.keyy.value,document.frm_login.keyy1.value);
}
if(document.frm_login.vercode.value==""||document.frm_login.vercode.value==null)
{
$('#captcha-code').text('Please enter code!');
$('#vercode').focus();
return false;
}
if(document.frm_login.vercode.value!=""||document.frm_login.vercode.value!=null)
{
$('#captcha-code').text('');
}
Html code of login.phtml
<div class="form-group captcha">
<?php echo $this->form->vercode; ?>
<p><span><img src="<?php echo $this->baseUrl('cool_capcha/captcha.php'); ?>"
id="captcha" /></span>
<a onclick="document.getElementById('captcha').src='<?php echo $this-
>baseUrl('cool_capcha/captcha.php'); ?>?'+Math.random();
document.getElementById('vercode').focus();"id="change-image" class="menu4
refresh" style="cursor:pointer">
<img src="./images/refress.png" class ="refresh" id="refresh" /></a></p>
<div class="clearfix"></div><span class="require" id="captcha-code">
</span>
</div>
Forms Auth.php
$this->addElement('text', 'vercode', array(
//'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'class' => 'form-control captchain',
'placeholder' => 'Captcha',
'autocomplete' => 'off',
'decorators'=>Array(
'ViewHelper',
'Errors',
),
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
Controller AuthController function loginAction()
$captcha = new Zend_Session_Namespace('captcha');
$captcha->captcha = session_id();
if($dataform['vercode'] != $_SESSION["vercode"]){
$msg="Please enter a correct code!";
$this->view->assign('msg', $msg);
return false;
}
I have rule in my model like this
public function rules()
{
return [
[['tbl_data_induk_mahasiswa_id'], 'required'],
[['tbl_data_induk_mahasiswa_id'], 'integer'],
[['nama'], 'file','extensions'=>'png,jpg','maxSize' => 1024000,'tooBig' => 'Size maksimum adalah 1 MB'],
[['nama'], 'string', 'max' => 300],
[['tbl_data_induk_mahasiswa_id'], 'unique'],
[['tbl_data_induk_mahasiswa_id'], 'exist', 'skipOnError' => true, 'targetClass' => TblDataIndukMahasiswa::className(), 'targetAttribute' => ['tbl_data_induk_mahasiswa_id' => 'id']],
];
}
i have form like this
<?php
use yii\helpers\Html;
//use yii\widgets\ActiveForm;
echo Html::beginForm(
['mahasiswa-foto-biodata/update'],
'post',
['enctype' => 'multipart/form-data'] //if you want to upload file with post
); ?>
<div class="form-group form-file-upload form-file-multiple">
<?= Html::activeFileInput(
$model,
'nama',
['class' => 'inputFileHidden', 'multiple' => '']
); ?>
<div class="input-group">
<?= Html::activeTextInput(
$model,
'nama',
[
'class' => 'form-control inputFileVisible',
'placeholder' => 'Single File',
]
); ?>
<span class="input-group-btn">
<button type="button" class="btn btn-fab btn-round btn-primary">
<i class="material-icons">attach_file</i>
</button>
</span>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?= Html::endForm(); ?>
I can not use activeForm widget because i must create html form that suit with my template, the form is work, the file succesfull uploaded, the problem is every tipe file is success uploaded, not just only png or jpg, but if i change the max rule for nama to [['nama'], 'string', 'max' => 2],then i upload a file that have name's length more than two the file can not be uploaded.
Any help?
Please check your form, you have 02 types of input with the same name ("nama"). One is "file", one is "text"
I think it is the reason that make your form does not works correctly!
<div class="form-group">
<?php echo form_textarea(array('name' => 'txtdescription', 'id' => 'desc', 'class' => "ckeditor", 'value' => set_value('txtdescription', htmlspecialchars_decode($desc)))); ?>
</div>
This is my view code when we get data from db
Add 3 argument for set_value.
Try this:
<div class="form-group">
<?php echo form_textarea(array('name' => 'txtdescription', 'id' => 'desc', 'class' => "ckeditor", 'value' => set_value('txtdescription', $desc,false))); ?>
</div>
Hello i want to reuse a form in codeigniter but problem is the update form is populated one the page has load. Here is a sample form.
<?php
$attributes = array('class' => 'form-horizontal', 'id' => 'addPersonnel');
echo form_open($form_submission, $attributes);
?>
<input type="text" name="name" value="<?php echo set_value('name')?"> >
<input type="text" name="rank" value="<?php echo set_value('rank')?"> >
<button type="submit" class="btn btn-primary">Save</button>
<?php echo form_close()?>
My problem here lies in the set_value() method of the form_validation class since it will have conflict with populating the form if updating.
In your controller update function add a condition like:
if ($this->input->server('REQUEST_METHOD') === 'POST') {
$this->_get_userinfo_form_fields();
} else {
$this->_get_userinfo_form_fields($user_data); //$user_data from database
}
For add function you can directly call - $this->_get_userinfo_form_fields();
Then declare a function as below:
protected function _get_userinfo_form_fields($user_data = array()) {
$this->data['rank'] = array(
'placeholder' => 'Rank',
'name' => 'rank',
'id' => 'rank',
'class' => '',
'value' => isset($user_data['rank']) ? $user_data['rank'] :set_value('rank',$this->input->post('rank',TRUE)),
'style' => '',
);
//Add rest of the fields here like the above one
}
In view file add code like:
<?php echo form_input($rank);?>
I am having trouble bypassing the $this->form_validation->set_rules("blah") even when i'm writing the correct inputs. (Using data exisiting in the databases). What happens is that whatever input i do, the form_validation->run() seems to always return false. Please Help Me :(
The View Part (The Form that i'm using):
<div class="navbar-form navbar-right">
<?php
$user = array('name' => 'username', 'class' => 'form-control', 'placeholder' => 'Username', 'autocomplete' => 'off');
$pass = array('name' => 'password', 'class' => 'form-control', 'placeholder' => 'Password', 'autocomplete' => 'off');
$button = array('id' => 'loginbutton', 'class' => 'form-control');
echo form_open('bigphloginc/validateUser');
?>
<div class="form-group">
<?php echo form_input($user); ?>
</div>
<div class="form-group">
<?php echo form_password($pass); ?>
</div>
<div class="form-group">
<?php
echo form_submit($button,'Login');
echo form_close();
?>
</div>
</div>
The Controller i'm Using:
public function validateUser(){
//Gets the posted values
$tempUsername = $this->input->post('username');
$tempPassword = $this->input->post('password');
//Set the rules for forms
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|md5');
if($this->form_validation->run()==FALSE){ //If the form data isn't accepted, loads back to login
redirect(bigphloginc/index);
}else{ //If form data is accepted, checks the database
if(isset($this->session->userdata($tempUsername))){
redirect(bigphloginc/index);
}else{
$this->load->model('bigphuser');
$query = $this->bigphuser->login($tempUsername,$tempPassword);
if($query==FALSE){ //If the form data doesn't exist in db, loads back to login
$this->load->view('warning');
$this->load->view('ilogin');
}else{ //If the form data exist on db, then continues to their respective pages
$data = array(
'username' => $tempUsername,
'password' => $tempPassword,
'type' => $query[0]->type,
'employeeNumber' => $query[0]->employeeNumber,
'loggedIn' => true
);
$this->session->set_userdata($data); //Sets the data to the session
if($query[0]->type=="admin"){ //if the user is an admin
redirect('bigphadmin/home');
}else if($query[0]->type=="employee"){
redirect('bigphemployee/home'); //if the user is an employee
}//Query Type
}//Query false
}
}//validation false
}
xss_clean is no longer part of form validation in codeigniter 3. The alternative is not to use it, as xss_clean is doing sanitization and not validation.
xss_clean is part of security helper.You can use it as
$this->load->helper('security');
$value = $this->input->post('formvalue', TRUE); //TRUE enables the xss filtering
Check this link for more detail